diff --git a/backend/repository/campaignRecipient.go b/backend/repository/campaignRecipient.go index 3dd262a..0491ebe 100644 --- a/backend/repository/campaignRecipient.go +++ b/backend/repository/campaignRecipient.go @@ -269,8 +269,10 @@ func (r *CampaignRecipient) GetByCampaignRecipientID( // GetUnsendRecipients gets all campaign recipients that are not sent // and have been attempted or been cancelled // if limit is larger than 0 it will limit the number of results +// if campaignID is not nil, it will filter by that campaign func (r *CampaignRecipient) GetUnsendRecipients( ctx context.Context, + campaignID *uuid.UUID, limit int, options *CampaignRecipientOption, ) ([]*model.CampaignRecipient, error) { @@ -289,6 +291,12 @@ func (r *CampaignRecipient) GetUnsendRecipients( TableColumn(database.CAMPAIGN_RECIPIENT_TABLE_NAME, "last_attempt_at"), ), ) + if campaignID != nil { + q = q.Where( + fmt.Sprintf("%s = ?", TableColumn(database.CAMPAIGN_RECIPIENT_TABLE_NAME, "campaign_id")), + campaignID, + ) + } if limit > 0 { q = q.Limit(limit) } diff --git a/backend/service/campaign.go b/backend/service/campaign.go index ff24fb9..fc5bc64 100644 --- a/backend/service/campaign.go +++ b/backend/service/campaign.go @@ -2533,13 +2533,14 @@ func (c *Campaign) closeCampaign( if !isAuthorized { return errs.ErrAuthorizationFailed } - // find all recipients that are not sent and cancel them + // find all recipients that are not sent and cancel them for this campaign campaignRecipients, err := c.CampaignRecipientRepository.GetUnsendRecipients( ctx, + id, repository.NO_LIMIT, &repository.CampaignRecipientOption{}, ) - c.Logger.Debugw("found unsent recipients to cancel", "count", len(campaignRecipients)) + c.Logger.Debugw("found unsent recipients to cancel for campaign", "count", len(campaignRecipients), "campaignID", id.String()) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { c.Logger.Errorw("failed to get unsent recipients", "error", err) return errs.Wrap(err)