92 lines
3.2 KiB
Go
92 lines
3.2 KiB
Go
package domain
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type ScheduledNotificationStatus string
|
|
|
|
const (
|
|
ScheduledStatusPending ScheduledNotificationStatus = "pending"
|
|
ScheduledStatusProcessing ScheduledNotificationStatus = "processing"
|
|
ScheduledStatusSent ScheduledNotificationStatus = "sent"
|
|
ScheduledStatusFailed ScheduledNotificationStatus = "failed"
|
|
ScheduledStatusCancelled ScheduledNotificationStatus = "cancelled"
|
|
)
|
|
|
|
type ScheduledNotificationTargetRaw struct {
|
|
Phones []string `json:"phones,omitempty"`
|
|
Emails []string `json:"emails,omitempty"`
|
|
TeamMemberIDs []int64 `json:"team_member_ids,omitempty"`
|
|
TeamRole string `json:"team_role,omitempty"`
|
|
// In-app scheduled notifications may store optional metadata here.
|
|
Level string `json:"level,omitempty"`
|
|
Type string `json:"type,omitempty"`
|
|
}
|
|
|
|
// ParseScheduledTargetRaw decodes team/extra targeting stored in scheduled_notifications.target_raw.
|
|
func ParseScheduledTargetRaw(data json.RawMessage) ScheduledNotificationTargetRaw {
|
|
var raw ScheduledNotificationTargetRaw
|
|
if len(data) > 0 {
|
|
_ = json.Unmarshal(data, &raw)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
// EnrichScheduledTargets copies team targeting from target_raw onto the scheduled notification for API responses.
|
|
func EnrichScheduledTargets(sn *ScheduledNotification) {
|
|
if sn == nil {
|
|
return
|
|
}
|
|
raw := ParseScheduledTargetRaw(sn.TargetRaw)
|
|
if len(raw.TeamMemberIDs) > 0 {
|
|
sn.TargetTeamMemberIDs = raw.TeamMemberIDs
|
|
}
|
|
if raw.TeamRole != "" {
|
|
sn.TargetTeamRole = raw.TeamRole
|
|
}
|
|
}
|
|
|
|
type ScheduledNotification struct {
|
|
ID int64 `json:"id"`
|
|
Channel DeliveryChannel `json:"channel"`
|
|
Title string `json:"title,omitempty"`
|
|
Message string `json:"message"`
|
|
HTML string `json:"html,omitempty"`
|
|
|
|
EmailTemplateSlug string `json:"email_template_slug,omitempty"`
|
|
// Free-form template variable object. Keys are defined by the selected email template.
|
|
EmailTemplateVariables json.RawMessage `json:"email_template_variables,omitempty" swaggertype:"object"`
|
|
|
|
ScheduledAt time.Time `json:"scheduled_at"`
|
|
Status ScheduledNotificationStatus `json:"status"`
|
|
|
|
TargetUserIDs []int64 `json:"target_user_ids,omitempty"`
|
|
TargetRole string `json:"target_role,omitempty"`
|
|
TargetTeamMemberIDs []int64 `json:"target_team_member_ids,omitempty"`
|
|
TargetTeamRole string `json:"target_team_role,omitempty"`
|
|
// Optional extra targets: phones and emails arrays, team_member_ids array, team_role, level, and type strings.
|
|
TargetRaw json.RawMessage `json:"target_raw,omitempty" swaggertype:"object"`
|
|
|
|
AttemptCount int32 `json:"attempt_count"`
|
|
LastError string `json:"last_error,omitempty"`
|
|
|
|
ProcessingStartedAt *time.Time `json:"processing_started_at,omitempty"`
|
|
SentAt *time.Time `json:"sent_at,omitempty"`
|
|
CancelledAt *time.Time `json:"cancelled_at,omitempty"`
|
|
|
|
CreatedBy int64 `json:"created_by"` // team_members.id of the staff actor who scheduled the job
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type ScheduledNotificationFilter struct {
|
|
Status string
|
|
Channel string
|
|
After *time.Time
|
|
Before *time.Time
|
|
Limit int
|
|
Offset int
|
|
}
|