From 63f0549efb8414ef8eb81bc27e9d30441213ece3 Mon Sep 17 00:00:00 2001 From: Andrew Walter Date: Sat, 9 Dec 2023 10:46:31 +1100 Subject: [PATCH] Campaigns: Refactored user assignment --- models/campaign.go | 148 +++++++++++++++++++++++---------------------- 1 file changed, 77 insertions(+), 71 deletions(-) diff --git a/models/campaign.go b/models/campaign.go index debca6ab..c091752f 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -536,77 +536,8 @@ func PostCampaign(c *Campaign, uid int64) error { if err != nil { log.Error(err) } - // Insert all the results - resultMap := make(map[string]bool) - recipientIndex := 0 - tx := db.Begin() - for _, g := range c.Groups { - // Insert a result for each target in the group - for _, t := range g.Targets { - // Remove duplicate results - we should only - // send emails to unique email addresses. - if _, ok := resultMap[t.Email]; ok { - continue - } - resultMap[t.Email] = true - sendDate := c.generateSendDate(recipientIndex, totalRecipients) - r := &Result{ - BaseRecipient: BaseRecipient{ - Email: t.Email, - Position: t.Position, - FirstName: t.FirstName, - LastName: t.LastName, - }, - Status: StatusScheduled, - CampaignId: c.Id, - UserId: c.UserId, - SendDate: sendDate, - Reported: false, - ModifiedDate: c.CreatedDate, - } - err = r.GenerateId(tx) - if err != nil { - log.Error(err) - tx.Rollback() - return err - } - processing := false - if r.SendDate.Before(c.CreatedDate) || r.SendDate.Equal(c.CreatedDate) { - r.Status = StatusSending - processing = true - } - err = tx.Save(r).Error - if err != nil { - log.WithFields(logrus.Fields{ - "email": t.Email, - }).Errorf("error creating result: %v", err) - tx.Rollback() - return err - } - c.Results = append(c.Results, *r) - log.WithFields(logrus.Fields{ - "email": r.Email, - "send_date": sendDate, - }).Debug("creating maillog") - m := &MailLog{ - UserId: c.UserId, - CampaignId: c.Id, - RId: r.RId, - SendDate: sendDate, - Processing: processing, - } - err = tx.Save(m).Error - if err != nil { - log.WithFields(logrus.Fields{ - "email": t.Email, - }).Errorf("error creating maillog entry: %v", err) - tx.Rollback() - return err - } - recipientIndex++ - } - } - return tx.Commit().Error + // Import the users + return UpdateUsers(c, totalRecipients) } //DeleteCampaign deletes the specified campaign @@ -668,3 +599,78 @@ func CompleteCampaign(id int64, uid int64) error { } return err } + +// UpdateUsers updates the specified campaign's user assignments. +func UpdateUsers(c *Campaign, totalRecipients int) error { + resultMap := make(map[string]bool) + recipientIndex := 0 + tx := db.Begin() + for _, g := range c.Groups { + // Insert a result for each target in the group + for _, t := range g.Targets { + // Remove duplicate results - we should only + // send emails to unique email addresses. + if _, ok := resultMap[t.Email]; ok { + continue + } + resultMap[t.Email] = true + sendDate := c.generateSendDate(recipientIndex, totalRecipients) + r := &Result{ + BaseRecipient: BaseRecipient{ + Email: t.Email, + Position: t.Position, + FirstName: t.FirstName, + LastName: t.LastName, + }, + Status: StatusScheduled, + CampaignId: c.Id, + UserId: c.UserId, + SendDate: sendDate, + Reported: false, + ModifiedDate: c.CreatedDate, + } + err := r.GenerateId(tx) + if err != nil { + log.Error(err) + tx.Rollback() + return err + } + processing := false + if r.SendDate.Before(c.CreatedDate) || r.SendDate.Equal(c.CreatedDate) { + r.Status = StatusSending + processing = true + } + err = tx.Save(r).Error + if err != nil { + log.WithFields(logrus.Fields{ + "email": t.Email, + }).Errorf("error creating result: %v", err) + tx.Rollback() + return err + } + c.Results = append(c.Results, *r) + log.WithFields(logrus.Fields{ + "email": r.Email, + "send_date": sendDate, + }).Debug("creating maillog") + m := &MailLog{ + UserId: c.UserId, + CampaignId: c.Id, + RId: r.RId, + SendDate: sendDate, + Processing: processing, + } + err = tx.Save(m).Error + if err != nil { + log.WithFields(logrus.Fields{ + "email": t.Email, + }).Errorf("error creating maillog entry: %v", err) + tx.Rollback() + return err + } + recipientIndex++ + } + } + + return tx.Commit().Error +} \ No newline at end of file