Skip to content

Commit

Permalink
Functionality fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris committed Oct 18, 2024
1 parent 6fb9bbd commit 766929e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions apps/platform/src/campaigns/CampaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import CampaignGenerateListJob from './CampaignGenerateListJob'
import Project from '../projects/Project'
import Template from '../render/Template'
import { subDays } from 'date-fns'
import { raw } from '../core/Model'

export const pagedCampaigns = async (params: PageParams, projectId: number) => {
const result = await Campaign.search(
Expand Down Expand Up @@ -459,15 +460,21 @@ export const canSendCampaignToUser = async (campaign: Campaign, user: Pick<User,

export const updateCampaignSendEnrollment = async (user: User) => {
const campaigns = await Campaign.query()
.leftJoin('campaign_sends', 'campaign_sends.campaign_id', 'campaigns.id')
.leftJoin('campaign_sends', (qb) =>
qb.on('campaign_sends.campaign_id', 'campaigns.id')
.andOn('campaign_sends.user_id', raw(user.id)),
)
.leftJoin('projects', 'projects.id', 'campaigns.project_id')
.where('campaigns.project_id', user.project_id)
.where('campaigns.state', 'scheduled')
.select('campaigns.*', 'campaign_sends.id AS send_id', 'campaign_sends.state AS send_state', 'campaign_sends.send_at', 'projects.timezone') as Array<SentCampaign & { send_id: number, send_state: CampaignSendState, timezone: string }>
.select('campaigns.*', 'campaign_sends.id AS send_id', 'campaign_sends.state AS send_state', 'projects.timezone') as Array<SentCampaign & { send_id: number, send_state: CampaignSendState, timezone: string }>

const join = []
const leave = []
for (const campaign of campaigns) {
const match = await recipientQuery(campaign).where('users.id', user.id)
const match = await recipientQuery(campaign)
.where('users.id', user.id)
.first()

// If user matches recipient query and they aren't already in the
// list, add to send list
Expand All @@ -482,9 +489,13 @@ export const updateCampaignSendEnrollment = async (user: User) => {
}
}

await CampaignSend.query().whereIn('id', leave).delete()
await CampaignSend.query()
.insert(join)
.onConflict(['campaign_id', 'user_id', 'reference_id'])
.merge(['state', 'send_at'])
if (leave.length) {
await CampaignSend.query().whereIn('id', leave).delete()
}
if (join.length) {
await CampaignSend.query()
.insert(join)
.onConflict(['campaign_id', 'user_id', 'reference_id'])
.merge(['state', 'send_at'])
}
}

0 comments on commit 766929e

Please sign in to comment.