BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
refactor: update failed recipient handling to include error descriptions and improve data structure
lotkee98195e6415
diff --git a/core/internal/service/abnormal_recipient/abnormal_recipient.go b/core/internal/service/abnormal_recipient/abnormal_recipient.goindex 0916d8e7..a5296394 100644--- a/core/internal/service/abnormal_recipient/abnormal_recipient.go+++ b/core/internal/service/abnormal_recipient/abnormal_recipient.go@@ -5,8 +5,9 @@ import ("billionmail-core/internal/service/maillog_stat""context""fmt"- "github.com/gogf/gf/v2/frame/g""time"++ "github.com/gogf/gf/v2/frame/g")func GetListWithPage(ctx context.Context, page, pageSize int, keyword string, addType int) (total int, list []*entity.AbnormalRecipient, err error) {@@ -227,21 +228,41 @@ func AbnormalRecipientAutoStat(ctx context.Context) {now := time.Now().Unix()overview := maillog_stat.NewOverview()- failedList := overview.FailedList(0, "", lastTime, now)+ failedList := overview.FailedListBounced(0, "", lastTime, now)- recipientSet := make(map[string]struct{})++ recipientDetailsMap := make(map[string]*RecipientDetail)for _, item := range failedList {- if recipient, ok := item["recipient"].(string); ok && recipient != "" {- recipientSet[recipient] = struct{}{}+ recipient, recipientOk := item["recipient"].(string)+ if !recipientOk || recipient == "" {+ continue+ }+++ description, descOk := item["description"].(string)+ if !descOk {+ description = "Unknown error"+ }+++ if _, exists := recipientDetailsMap[recipient]; !exists {+ recipientDetailsMap[recipient] = &RecipientDetail{+ Email: recipient,+ ErrorReason: description,+ }+ } else {+ recipientDetailsMap[recipient].ErrorReason = description}}- var recipients []string- for r := range recipientSet {- recipients = append(recipients, r)++ var recipientDetails []RecipientDetail+ for _, detail := range recipientDetailsMap {+ recipientDetails = append(recipientDetails, *detail)}- if len(recipients) > 0 {- _ = BatchUpsertAbnormalRecipients(ctx, recipients, 2, "Automatic statistics")+ if len(recipientDetails) > 0 {++ _ = BatchUpsertAbnormalRecipientsWithDetails(ctx, recipientDetails, 2, "Automatic statistics")}setLastStatTime(ctx, now)diff --git a/core/internal/service/maillog_stat/overview.go b/core/internal/service/maillog_stat/overview.goindex d7173a82..bd9632c2 100644--- a/core/internal/service/maillog_stat/overview.go+++ b/core/internal/service/maillog_stat/overview.go@@ -6,13 +6,14 @@ import ("billionmail-core/internal/service/public""context""fmt"+ "sort"+ "strings"+ "time"+"github.com/gogf/gf/v2/database/gdb""github.com/gogf/gf/v2/encoding/gjson""github.com/gogf/gf/v2/frame/g""github.com/gogf/gf/v2/util/gconv"- "sort"- "strings"- "time")// Overview maillog data overview structure@@ -675,3 +676,33 @@ func (o *Overview) FailedList(campaignID int64, domain string, startTime, endTimreturn lst}++// description FailedListBounced+func (o *Overview) FailedListBounced(campaignID int64, domain string, startTime, endTime int64) []map[string]interface{} {++ startTime, endTime = o.filterAndPrepareTimeSection(startTime, endTime)++ query := o.buildBaseQuery(campaignID, domain, startTime, endTime)++ query.Fields("sm.recipient")+ query.Fields("sm.status")+ query.Fields("sm.description")+ query.Fields("sm.log_time")++ query.Where("sm.status = ?", "bounced")++ query.OrderDesc("sm.log_time_millis")++ results, err := query.All()+ if err != nil {+ g.Log().Error(context.Background(), err)+ return nil+ }++ lst := make([]map[string]interface{}, 0, len(results))+ for _, item := range results {+ lst = append(lst, item.Map())+ }++ return lst+}