From 6d025d2dc79c52c453446eb324405748898891cd Mon Sep 17 00:00:00 2001 From: Ronni Skansing Date: Thu, 11 Jun 2026 22:34:41 +0200 Subject: [PATCH] fix bug with deleting groups anonymizing campaigns and bad recipient count Signed-off-by: Ronni Skansing --- backend/repository/campaignRecipient.go | 7 ++- backend/service/recipientGroup.go | 55 ++----------------- .../src/routes/recipient/group/+page.svelte | 6 +- 3 files changed, 12 insertions(+), 56 deletions(-) diff --git a/backend/repository/campaignRecipient.go b/backend/repository/campaignRecipient.go index bedaa7f..868f687 100644 --- a/backend/repository/campaignRecipient.go +++ b/backend/repository/campaignRecipient.go @@ -273,8 +273,11 @@ func (r *CampaignRecipient) GetByCampaignRecipientID( return ToCampaignRecipient(&dbCampaignRecipient) } -// GetUnsendRecipients gets all campaign recipients that are not sent -// and have been attempted or been cancelled +// GetUnsendRecipients gets campaign recipients that were never attempted and are +// not already cancelled (cancelled_at IS NULL AND last_attempt_at IS NULL). Used +// at campaign close to cancel only sends that never started; recipients that were +// attempted (sent, failed, or still in flight) are left untouched so their +// existing state is not overwritten. // 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( diff --git a/backend/service/recipientGroup.go b/backend/service/recipientGroup.go index c1f426a..7a88f1e 100644 --- a/backend/service/recipientGroup.go +++ b/backend/service/recipientGroup.go @@ -518,37 +518,14 @@ func (r *RecipientGroup) RemoveRecipients( if existingGroup.IsDynamic.IsSpecified() && !existingGroup.IsDynamic.IsNull() && existingGroup.IsDynamic.MustGet() { return validate.WrapErrorWithField(errors.New("cannot remove recipients from a dynamic group"), "group") } - // anonymize recipients in any recipient-campaign data + // cancel any pending sends for these recipients in active campaigns; the + // recipients are kept and their campaign history is left intact for _, recpID := range recipientIDs { - // if the recipient is in any active campaign, cancel the recipient sending err = r.CampaignRecipientRepository.CancelInActiveCampaigns(ctx, recpID) if err != nil { r.Logger.Errorw("failed to cancel campaign recipient", "error", err) return err } - anonymizedID := uuid.New() - err = r.RecipientService.CampaignRecipientRepository.Anonymize( - ctx, - nil, // nil campaignID means anonymize across all campaigns - recpID, - &anonymizedID, - ) - if err != nil { - r.Logger.Errorw( - "failed to remove recipient - failed to anonymized recipiet campaign data", - "error", err, - ) - return err - } - err = r.CampaignRepository.AnonymizeCampaignEventsByRecipientID( - ctx, - recpID, - &anonymizedID, - ) - if err != nil { - r.Logger.Errorw("failed to anonymize campaign event", "error", err) - return err - } } // remove recipient from group err = r.RecipientGroupRepository.RemoveRecipients( @@ -616,39 +593,15 @@ func (r *RecipientGroup) DeleteByID( } if len(recipients) > 0 { - // anonymize recipients in any recipient-campaign data + // cancel any pending sends for these recipients in active campaigns; the + // recipients are kept and their campaign history is left intact for _, recipient := range recipients { - anonymizedID := uuid.New() recpID := recipient.ID.MustGet() - - // if the recipient is in any active campaign, cancel the recipient sending err = r.CampaignRecipientRepository.CancelInActiveCampaigns(ctx, &recpID) if err != nil { r.Logger.Errorw("failed to cancel campaign recipient", "error", err) return err } - err = r.RecipientService.CampaignRecipientRepository.Anonymize( - ctx, - nil, // nil campaignID means anonymize across all campaigns - &recpID, - &anonymizedID, - ) - if err != nil { - r.Logger.Errorw( - "failed to remove recipient - failed to anonymized recipiet campaign data", - "error", err, - ) - return err - } - err = r.CampaignRepository.AnonymizeCampaignEventsByRecipientID( - ctx, - &recpID, - &anonymizedID, - ) - if err != nil { - r.Logger.Errorw("failed to anonymize campaign event", "error", err) - return err - } } } // remove group from campaign groups diff --git a/frontend/src/routes/recipient/group/+page.svelte b/frontend/src/routes/recipient/group/+page.svelte index fa3be0d..d8715c7 100644 --- a/frontend/src/routes/recipient/group/+page.svelte +++ b/frontend/src/routes/recipient/group/+page.svelte @@ -506,10 +506,10 @@ onClickDelete(deleteValues.id)}