Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new notification send history table #281

Merged
merged 11 commits into from
Sep 18, 2023
31 changes: 31 additions & 0 deletions models/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
adityathebe marked this conversation as resolved.
Show resolved Hide resolved
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
}
39 changes: 39 additions & 0 deletions schema/notifications.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
adityathebe marked this conversation as resolved.
Show resolved Hide resolved
on_delete = NO_ACTION
adityathebe marked this conversation as resolved.
Show resolved Hide resolved
}
}