RezinaLMS-BackEnd/internal/domain/scheduled_notification.go
Yared Yemane 880bdfb4b7 Fix scheduled notification creator FK and default app timezone to EAT.
Point scheduled_notifications.created_by at team_members so admin JWT IDs validate, and set TZ=Africa/Addis_Ababa for the Docker app service.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-16 18:52:09 +03:00

90 lines
3.0 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"`
EmailTemplateVariables json.RawMessage `json:"email_template_variables,omitempty"`
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"`
TargetRaw json.RawMessage `json:"target_raw,omitempty"`
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
}