From 6ca8adaab9ee3cfeff36a423ec4f5a7952cc3c7e Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Fri, 15 Sep 2023 19:42:35 +0545 Subject: [PATCH] feat: new notification send history table --- models/notifications.go | 31 +++++++++++++++++++++++++++++++ schema/notifications.hcl | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/models/notifications.go b/models/notifications.go index 6a3064e6..7e5cc261 100644 --- a/models/notifications.go +++ b/models/notifications.go @@ -32,3 +32,34 @@ func (n *Notification) HasRecipients() bool { func (n Notification) AsMap(removeFields ...string) map[string]any { return asMap(n, removeFields...) } + +type NotificationSendHistory struct { + ID uuid.UUID `json:"id,omitempty" gorm:"default:generate_ulid()"` + NotificationID string `json:"notification_id"` + Body string `json:"body,omitempty"` + Error *string `json:"error,omitempty"` + DurationMs int64 `json:"duration_ms,omitempty" gorm:"column:duration_millis"` + CreatedAt time.Time `json:"created_at" time_format:"postgres_timestamp"` + + timeStart time.Time +} + +func (n NotificationSendHistory) AsMap(removeFields ...string) map[string]any { + return asMap(n, removeFields...) +} + +func (t *NotificationSendHistory) TableName() string { + return "notification_send_history" +} + +func NewNotificationSendHistory(notificationID string) *NotificationSendHistory { + return &NotificationSendHistory{ + NotificationID: notificationID, + timeStart: time.Now(), + } +} + +func (t *NotificationSendHistory) End() *NotificationSendHistory { + t.DurationMs = time.Since(t.timeStart).Milliseconds() + return t +} diff --git a/schema/notifications.hcl b/schema/notifications.hcl index 01ebb001..073ac9de 100644 --- a/schema/notifications.hcl +++ b/schema/notifications.hcl @@ -81,4 +81,43 @@ table "notifications" { on_update = NO_ACTION on_delete = CASCADE } +} + +table "notification_send_history" { + schema = schema.public + column "id" { + null = false + type = uuid + default = sql("generate_ulid()") + } + column "notification_id" { + null = false + type = uuid + } + column "body" { + null = false + type = text + } + column "error" { + null = true + type = text + } + column "duration_millis" { + null = true + type = integer + } + column "created_at" { + null = false + type = timestamptz + default = sql("now()") + } + primary_key { + columns = [column.id] + } + foreign_key "notification_id_fkey" { + columns = [column.notification_id] + ref_columns = [table.notifications.column.id] + on_update = NO_ACTION + on_delete = NO_ACTION + } } \ No newline at end of file