mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +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"`
|
Error string `json:"error,omitempty" bson:"error,omitempty"`
|
||||||
Title string `json:"title" bson:"title"`
|
Title string `json:"title" bson:"title"`
|
||||||
Content string `json:"content" bson:"content"`
|
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"`
|
SettingId primitive.ObjectID `json:"setting_id" bson:"setting_id"`
|
||||||
ChannelId primitive.ObjectID `json:"channel_id" bson:"channel_id"`
|
ChannelId primitive.ObjectID `json:"channel_id" bson:"channel_id"`
|
||||||
Setting *NotificationSettingV2 `json:"setting,omitempty" bson:"-"`
|
Setting *NotificationSettingV2 `json:"setting,omitempty" bson:"-"`
|
||||||
|
|||||||
@@ -21,12 +21,11 @@ type NotificationSettingV2 struct {
|
|||||||
TriggerTarget string `json:"trigger_target" bson:"trigger_target"`
|
TriggerTarget string `json:"trigger_target" bson:"trigger_target"`
|
||||||
Trigger string `json:"trigger" bson:"trigger"`
|
Trigger string `json:"trigger" bson:"trigger"`
|
||||||
|
|
||||||
HasMail bool `json:"has_mail" bson:"has_mail"`
|
SenderEmail string `json:"sender_email,omitempty" bson:"sender_email,omitempty"`
|
||||||
SenderEmail string `json:"sender_email" bson:"sender_email"`
|
SenderName string `json:"sender_name,omitempty" bson:"sender_name,omitempty"`
|
||||||
SenderName string `json:"sender_name" bson:"sender_name"`
|
MailTo string `json:"mail_to" bson:"mail_to,omitempty"`
|
||||||
MailTo string `json:"mail_to" bson:"mail_to"`
|
MailCc string `json:"mail_cc,omitempty" bson:"mail_cc,omitempty"`
|
||||||
MailCc string `json:"mail_cc" bson:"mail_cc"`
|
MailBcc string `json:"mail_bcc,omitempty" bson:"mail_bcc,omitempty"`
|
||||||
MailBcc string `json:"mail_bcc" bson:"mail_bcc"`
|
|
||||||
|
|
||||||
ChannelIds []primitive.ObjectID `json:"channel_ids,omitempty" bson:"channel_ids,omitempty"`
|
ChannelIds []primitive.ObjectID `json:"channel_ids,omitempty" bson:"channel_ids,omitempty"`
|
||||||
Channels []NotificationChannelV2 `json:"channels,omitempty" bson:"-"`
|
Channels []NotificationChannelV2 `json:"channels,omitempty" bson:"-"`
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
StatusSending = "sending"
|
||||||
StatusSuccess = "success"
|
StatusSuccess = "success"
|
||||||
StatusError = "error"
|
StatusError = "error"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -51,20 +51,31 @@ func (svc *ServiceV2) SendMail(s *models.NotificationSettingV2, ch *models.Notif
|
|||||||
mailCc := s.MailCc
|
mailCc := s.MailCc
|
||||||
mailBcc := s.MailBcc
|
mailBcc := s.MailBcc
|
||||||
|
|
||||||
|
// request
|
||||||
|
r, _ := svc.createRequest(s, ch, title, content)
|
||||||
|
|
||||||
// send mail
|
// send mail
|
||||||
err := SendMail(s, ch, mailTo, mailCc, mailBcc, title, content)
|
err := SendMail(s, ch, mailTo, mailCc, mailBcc, title, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[NotificationServiceV2] send mail error: %v", err)
|
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) {
|
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)
|
err := SendIMNotification(ch, title, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[NotificationServiceV2] send mobile notification error: %v", err)
|
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) {
|
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) {
|
func (svc *ServiceV2) createRequest(s *models.NotificationSettingV2, ch *models.NotificationChannelV2, title, content string) (res *models.NotificationRequestV2, err error) {
|
||||||
status := StatusSuccess
|
|
||||||
errMsg := ""
|
|
||||||
if err != nil {
|
|
||||||
status = StatusError
|
|
||||||
errMsg = err.Error()
|
|
||||||
}
|
|
||||||
r := models.NotificationRequestV2{
|
r := models.NotificationRequestV2{
|
||||||
Status: status,
|
Status: StatusSending,
|
||||||
Error: errMsg,
|
SettingId: s.Id,
|
||||||
SettingId: s.Id,
|
ChannelId: ch.Id,
|
||||||
ChannelId: ch.Id,
|
Title: title,
|
||||||
Title: title,
|
Content: content,
|
||||||
Content: content,
|
SenderEmail: s.SenderEmail,
|
||||||
|
SenderName: s.SenderName,
|
||||||
|
MailTo: s.MailTo,
|
||||||
|
MailCc: s.MailCc,
|
||||||
|
MailBcc: s.MailBcc,
|
||||||
}
|
}
|
||||||
r.SetCreatedAt(time.Now())
|
r.SetCreatedAt(time.Now())
|
||||||
r.SetUpdatedAt(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 {
|
if err != nil {
|
||||||
log.Errorf("[NotificationServiceV2] save request error: %v", err)
|
log.Errorf("[NotificationServiceV2] save request error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user