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) +}