From f6868a4a9920dc4a6fac078305a3548d754334ab Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Sat, 9 Nov 2024 09:40:08 -0600 Subject: [PATCH] Check for unsubscribe state right before any send (#537) --- apps/platform/src/campaigns/CampaignService.ts | 2 -- .../src/providers/MessageTriggerService.ts | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/platform/src/campaigns/CampaignService.ts b/apps/platform/src/campaigns/CampaignService.ts index 7c231493..6237d44d 100644 --- a/apps/platform/src/campaigns/CampaignService.ts +++ b/apps/platform/src/campaigns/CampaignService.ts @@ -219,8 +219,6 @@ export const triggerCampaignSend = async ({ campaign, user, event, send_id, refe export const sendCampaignJob = ({ campaign, user, event, send_id, reference_type, reference_id }: SendCampaign): EmailJob | TextJob | PushJob | WebhookJob => { - // TODO: Might also need to check for unsubscribe in here since we can - // do individual sends const body = { campaign_id: campaign.id, user_id: user instanceof User ? user.id : user, diff --git a/apps/platform/src/providers/MessageTriggerService.ts b/apps/platform/src/providers/MessageTriggerService.ts index 269e383d..397584d2 100644 --- a/apps/platform/src/providers/MessageTriggerService.ts +++ b/apps/platform/src/providers/MessageTriggerService.ts @@ -17,6 +17,8 @@ import { MessageTrigger } from './MessageTrigger' import JourneyProcessJob from '../journey/JourneyProcessJob' import { createEvent } from '../users/UserEventRepository' import { loadUserStepDataMap } from '../journey/JourneyService' +import { getUserSubscriptionState } from '../subscriptions/SubscriptionService' +import { SubscriptionState } from '../subscriptions/Subscription' interface MessageTriggerHydrated { user: User @@ -46,6 +48,18 @@ export async function loadSendJob({ campaign_id, user_id const campaign = await Campaign.find(campaign_id) if (!campaign) return + // Check to see if user has already unsubscribed or not + const subscriptionState = await getUserSubscriptionState(user.id, campaign.subscription_id) + if (subscriptionState === SubscriptionState.unsubscribed) { + await updateSendState({ + campaign, + user, + reference_id, + state: 'aborted', + }) + return + } + // Get all templates const templates = await Template.all( qb => qb.where('campaign_id', campaign_id),