From 0e2170f6441bc7669b0c1869bc006dc21ceed76f Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Fri, 26 Jul 2024 15:33:58 +0800 Subject: [PATCH] refactor: Update models to include sender email, name, and mail recipients for notification requests --- .../models/v2/notification_request_v2.go | 5 ++ .../models/v2/notification_setting_v2.go | 11 ++-- core/notification/constants.go | 1 + core/notification/service_v2.go | 61 ++++++++++++++----- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/core/models/models/v2/notification_request_v2.go b/core/models/models/v2/notification_request_v2.go index cc2836d7..be53a8d5 100644 --- a/core/models/models/v2/notification_request_v2.go +++ b/core/models/models/v2/notification_request_v2.go @@ -9,6 +9,11 @@ type NotificationRequestV2 struct { Error string `json:"error,omitempty" bson:"error,omitempty"` Title string `json:"title" bson:"title"` Content string `json:"content" bson:"content"` + SenderEmail string `json:"sender_email" bson:"sender_email"` + SenderName string `json:"sender_name" bson:"sender_name"` + MailTo string `json:"mail_to" bson:"mail_to"` + MailCc string `json:"mail_cc" bson:"mail_cc"` + MailBcc string `json:"mail_bcc" bson:"mail_bcc"` SettingId primitive.ObjectID `json:"setting_id" bson:"setting_id"` ChannelId primitive.ObjectID `json:"channel_id" bson:"channel_id"` Setting *NotificationSettingV2 `json:"setting,omitempty" bson:"-"` diff --git a/core/models/models/v2/notification_setting_v2.go b/core/models/models/v2/notification_setting_v2.go index 6f36c3ec..517ae086 100644 --- a/core/models/models/v2/notification_setting_v2.go +++ b/core/models/models/v2/notification_setting_v2.go @@ -21,12 +21,11 @@ type NotificationSettingV2 struct { TriggerTarget string `json:"trigger_target" bson:"trigger_target"` Trigger string `json:"trigger" bson:"trigger"` - HasMail bool `json:"has_mail" bson:"has_mail"` - SenderEmail string `json:"sender_email" bson:"sender_email"` - SenderName string `json:"sender_name" bson:"sender_name"` - MailTo string `json:"mail_to" bson:"mail_to"` - MailCc string `json:"mail_cc" bson:"mail_cc"` - MailBcc string `json:"mail_bcc" bson:"mail_bcc"` + SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"` + SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"` + MailTo string `json:"mail_to" bson:"mail_to,omitempty"` + MailCc string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"` + MailBcc string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"` ChannelIds []primitive.ObjectID `json:"channel_ids,omitempty" bson:"channel_ids,omitempty"` Channels []NotificationChannelV2 `json:"channels,omitempty" bson:"-"` diff --git a/core/notification/constants.go b/core/notification/constants.go index d8de0738..817f476f 100644 --- a/core/notification/constants.go +++ b/core/notification/constants.go @@ -26,6 +26,7 @@ const ( ) const ( + StatusSending = "sending" StatusSuccess = "success" StatusError = "error" ) diff --git a/core/notification/service_v2.go b/core/notification/service_v2.go index 82d8076a..9e19978c 100644 --- a/core/notification/service_v2.go +++ b/core/notification/service_v2.go @@ -51,20 +51,31 @@ func (svc *ServiceV2) SendMail(s *models.NotificationSettingV2, ch *models.Notif mailCc := s.MailCc mailBcc := s.MailBcc + // request + r, _ := svc.createRequest(s, ch, title, content) + // send mail err := SendMail(s, ch, mailTo, mailCc, mailBcc, title, content) if err != nil { log.Errorf("[NotificationServiceV2] send mail error: %v", err) } - go svc.saveRequest(s, ch, title, content, err) + + // save request + go svc.saveRequest(r, err) } func (svc *ServiceV2) SendIM(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, title, content string) { + // request + r, _ := svc.createRequest(s, ch, title, content) + + // send mobile notification err := SendIMNotification(ch, title, content) if err != nil { log.Errorf("[NotificationServiceV2] send mobile notification error: %v", err) } - go svc.saveRequest(s, ch, title, content, err) + + // save request + go svc.saveRequest(r, err) } func (svc *ServiceV2) getContent(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, args ...any) (content string) { @@ -363,24 +374,42 @@ func (svc *ServiceV2) SendNodeNotification(node *models.NodeV2) { } } -func (svc *ServiceV2) saveRequest(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, title, content string, err error) { - status := StatusSuccess - errMsg := "" - if err != nil { - status = StatusError - errMsg = err.Error() - } +func (svc *ServiceV2) createRequest(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, title, content string) (res *models.NotificationRequestV2, err error) { r := models.NotificationRequestV2{ - Status: status, - Error: errMsg, - SettingId: s.Id, - ChannelId: ch.Id, - Title: title, - Content: content, + Status: StatusSending, + SettingId: s.Id, + ChannelId: ch.Id, + Title: title, + Content: content, + SenderEmail: s.SenderEmail, + SenderName: s.SenderName, + MailTo: s.MailTo, + MailCc: s.MailCc, + MailBcc: s.MailBcc, } r.SetCreatedAt(time.Now()) r.SetUpdatedAt(time.Now()) - _, err = service.NewModelServiceV2[models.NotificationRequestV2]().InsertOne(r) + r.Id, err = service.NewModelServiceV2[models.NotificationRequestV2]().InsertOne(r) + if err != nil { + log.Errorf("[NotificationServiceV2] save request error: %v", err) + return nil, err + } + return &r, nil +} + +func (svc *ServiceV2) saveRequest(r *models.NotificationRequestV2, err error) { + if r == nil { + return + } + + if err != nil { + r.Status = StatusError + r.Error = err.Error() + } else { + r.Status = StatusSuccess + } + r.SetUpdatedAt(time.Now()) + err = service.NewModelServiceV2[models.NotificationRequestV2]().ReplaceById(r.Id, *r) if err != nil { log.Errorf("[NotificationServiceV2] save request error: %v", err) }