Skip to content

Commit

Permalink
Check for unsubscribe state right before any send (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris authored Nov 9, 2024
1 parent 9a4f6da commit f6868a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 0 additions & 2 deletions apps/platform/src/campaigns/CampaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions apps/platform/src/providers/MessageTriggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
user: User
Expand Down Expand Up @@ -46,6 +48,18 @@ export async function loadSendJob<T extends TemplateType>({ 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),
Expand Down

0 comments on commit f6868a4

Please sign in to comment.