From 53b3a98521f98e06cddc9b2f203d08427dd1e088 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Sat, 15 Dec 2018 22:09:15 -0600 Subject: [PATCH] Deleting maillogs when a campaign is completed to prevent accidental future emails from being sent. Fixes #719 --- models/campaign.go | 6 ++++++ models/campaign_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/models/campaign.go b/models/campaign.go index fd9af73d..b2c1453d 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -591,6 +591,12 @@ func CompleteCampaign(id int64, uid int64) error { if err != nil { return err } + // Delete any maillogs still set to be sent out, preventing future emails + err = db.Where("campaign_id=?", id).Delete(&MailLog{}).Error + if err != nil { + log.Error(err) + return err + } // Don't overwrite original completed time if c.Status == CampaignComplete { return nil diff --git a/models/campaign_test.go b/models/campaign_test.go index 9c399701..ecd69e54 100644 --- a/models/campaign_test.go +++ b/models/campaign_test.go @@ -119,3 +119,17 @@ func (s *ModelsSuite) TestDeleteCampaignAlsoDeletesMailLogs(c *check.C) { c.Assert(err, check.Equals, nil) c.Assert(len(ms), check.Equals, 0) } + +func (s *ModelsSuite) TestCompleteCampaignAlsoDeletesMailLogs(c *check.C) { + campaign := s.createCampaign(c) + ms, err := GetMailLogsByCampaign(campaign.Id) + c.Assert(err, check.Equals, nil) + c.Assert(len(ms), check.Equals, len(campaign.Results)) + + err = CompleteCampaign(campaign.Id, campaign.UserId) + c.Assert(err, check.Equals, nil) + + ms, err = GetMailLogsByCampaign(campaign.Id) + c.Assert(err, check.Equals, nil) + c.Assert(len(ms), check.Equals, 0) +}