fix: Fix errors in creating cron job and clam alerts (#9910)

This commit is contained in:
2025-08-08 20:28:35 +08:00 committed by GitHub
parent 2b7ec9ffe1
commit c49c16cbb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 13 deletions

View file

@ -483,25 +483,53 @@ func (a AlertService) TestAlertConfig(req dto.AlertConfigTest) (bool, error) {
func (a AlertService) ExternalUpdateAlert(updateAlert dto.AlertCreate) error { func (a AlertService) ExternalUpdateAlert(updateAlert dto.AlertCreate) error {
upMap := make(map[string]interface{}) upMap := make(map[string]interface{})
var newStatus string
if updateAlert.SendCount == 0 { if updateAlert.SendCount == 0 {
upMap["status"] = constant.AlertDisable newStatus = constant.AlertDisable
} else { } else {
upMap["status"] = constant.AlertEnable newStatus = constant.AlertEnable
upMap["send_count"] = updateAlert.SendCount upMap["send_count"] = updateAlert.SendCount
} if updateAlert.Method != "" {
upMap["method"] = updateAlert.Method upMap["method"] = updateAlert.Method
alertInfo, _ := alertRepo.Get(alertRepo.WithByType(updateAlert.Type), alertRepo.WithByProject(updateAlert.Project)) }
}
upMap["status"] = newStatus
alertInfo, _ := alertRepo.Get(
alertRepo.WithByType(updateAlert.Type),
alertRepo.WithByProject(updateAlert.Project),
)
if alertInfo.ID > 0 { if alertInfo.ID > 0 {
if err := alertRepo.Update(upMap, alertRepo.WithByProject(updateAlert.Project), alertRepo.WithByType(updateAlert.Type)); err != nil { shouldUpdate := false
if alertInfo.Status != newStatus {
shouldUpdate = true
}
if val, ok := upMap["send_count"]; ok && val != alertInfo.SendCount {
shouldUpdate = true
}
if val, ok := upMap["method"]; ok && val != "" && val != alertInfo.Method {
shouldUpdate = true
}
if shouldUpdate {
if err := alertRepo.Update(
upMap,
alertRepo.WithByProject(updateAlert.Project),
alertRepo.WithByType(updateAlert.Type),
); err != nil {
return err return err
} }
}
} else { } else {
updateAlert.Status = constant.AlertEnable if updateAlert.Method != "" && updateAlert.Title != "" {
err := a.CreateAlert(updateAlert) updateAlert.Status = newStatus
if err != nil { if err := a.CreateAlert(updateAlert); err != nil {
return err return err
} }
} }
}
return nil return nil
} }

View file

@ -192,7 +192,7 @@ func (c *ClamService) Create(req dto.ClamCreate) error {
if err := clamRepo.Create(&clam); err != nil { if err := clamRepo.Create(&clam); err != nil {
return err return err
} }
if req.AlertCount != 0 { if req.AlertCount != 0 && req.AlertTitle != "" && req.AlertMethod != "" {
createAlert := dto.AlertCreate{ createAlert := dto.AlertCreate{
Title: req.AlertTitle, Title: req.AlertTitle,
SendCount: req.AlertCount, SendCount: req.AlertCount,

View file

@ -381,7 +381,7 @@ func (u *CronjobService) Import(req []dto.CronjobTrans) error {
cronjob.Status = constant.StatusDisable cronjob.Status = constant.StatusDisable
} }
_ = cronjobRepo.Create(&cronjob) _ = cronjobRepo.Create(&cronjob)
if item.AlertCount != 0 { if item.AlertCount != 0 && item.AlertTitle != "" && item.AlertMethod != "" {
createAlert := dto.AlertCreate{ createAlert := dto.AlertCreate{
Title: item.AlertTitle, Title: item.AlertTitle,
SendCount: item.AlertCount, SendCount: item.AlertCount,
@ -579,7 +579,7 @@ func (u *CronjobService) Create(req dto.CronjobOperate) error {
if err := cronjobRepo.Create(&cronjob); err != nil { if err := cronjobRepo.Create(&cronjob); err != nil {
return err return err
} }
if req.AlertCount != 0 { if req.AlertCount != 0 && req.AlertTitle != "" && req.AlertMethod != "" {
createAlert := dto.AlertCreate{ createAlert := dto.AlertCreate{
Title: req.AlertTitle, Title: req.AlertTitle,
SendCount: req.AlertCount, SendCount: req.AlertCount,