Fixes campaign sending

This commit is contained in:
Chris Anderson 2023-02-01 08:31:32 -06:00
parent a1d34e9a7a
commit 979a47260c
4 changed files with 12 additions and 3 deletions

View file

@ -146,7 +146,7 @@ export const updateSendState = async (campaign: Campaign | number, user: User |
state,
})
.onConflict(['user_id', 'list_id'])
.ignore()
.merge(['state'])
}
export const sendList = async (campaign: SentCampaign) => {
@ -205,8 +205,8 @@ export const sendList = async (campaign: SentCampaign) => {
export const campaignSendReadyQuery = () => {
return CampaignSend.query()
.where('send_at', '<', Date.now())
.where('state', 'pending')
.where('campaign_sends.send_at', '<', CampaignSend.raw('NOW()'))
.where('campaign_sends.state', 'pending')
.leftJoin('campaigns', 'campaigns.id', '=', 'campaign_sends.campaign_id')
.select('user_id', 'campaigns.*')
}

View file

@ -31,6 +31,7 @@ export default class EmailJob extends Job {
qb => qb.where('campaign_id', campaign.id).where('locale', user.locale),
)
// TODO: This is bad, need a fallback template for sending
// If not available template, abort
if (!template) return
@ -39,6 +40,8 @@ export default class EmailJob extends Job {
template_id: template?.id,
}
// TODO: Use the providers attached to the campaign
// Send and render email
const channel = await loadChannel(user.project_id, 'email')
await channel.send(template, { user, event, context })

View file

@ -13,11 +13,13 @@ import CampaignTriggerJob from '../campaigns/CampaignTriggerJob'
import ListPopulateJob from '../lists/ListPopulateJob'
import ListStatsJob from '../lists/ListStatsJob'
import ProcessListsJob from '../lists/ProcessListsJob'
import CampaignSendJob from '../campaigns/CampaignSendJob'
export type Queues = Record<number, Queue>
export const loadJobs = (queue: Queue) => {
queue.register(CampaignTriggerJob)
queue.register(CampaignSendJob)
queue.register(EmailJob)
queue.register(EventPostJob)
queue.register(JourneyProcessJob)

View file

@ -5,6 +5,7 @@ import App from '../app'
import CampaignTriggerJob from '../campaigns/CampaignTriggerJob'
import JourneyDelayJob from '../journey/JourneyDelayJob'
import ProcessListsJob from '../lists/ProcessListsJob'
import CampaignSendJob from '../campaigns/CampaignSendJob'
export default async (app: App) => {
schedule.scheduleJob('* * * * *', function() {
@ -17,5 +18,8 @@ export default async (app: App) => {
schedule.scheduleJob('0 * * * *', function() {
cleanupExpiredRevokedTokens(subDays(new Date(), 1))
})
schedule.scheduleJob('* * * * *', function() {
app.queue.enqueue(CampaignSendJob.from())
})
return schedule
}