mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
refactor: Update models to include sender email, name, and mail recipients for notification requests
This commit is contained in:
@@ -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:"-"`
|
||||
|
||||
@@ -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:"-"`
|
||||
|
||||
@@ -26,6 +26,7 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
StatusSending = "sending"
|
||||
StatusSuccess = "success"
|
||||
StatusError = "error"
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user