From f01498b97d2d4f8381e86544c9d22a57beabbe46 Mon Sep 17 00:00:00 2001 From: rafa1510 <34660086+rafa1510@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:36:32 -0400 Subject: [PATCH] Modify preEventNotficationJobId in db or remove job from queue as needed --- packages/core/src/modules/event/event.core.ts | 14 +++++ .../event/use-cases/pre-event-notification.ts | 60 ++++++++++++++++--- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/packages/core/src/modules/event/event.core.ts b/packages/core/src/modules/event/event.core.ts index 23e2a4d7..f3802d7e 100644 --- a/packages/core/src/modules/event/event.core.ts +++ b/packages/core/src/modules/event/event.core.ts @@ -1,6 +1,7 @@ import { db } from '@oyster/db'; import { job } from '@/infrastructure/bull/use-cases/job'; +import { deletePreEventNotification } from '@/modules/event/use-cases/pre-event-notification'; import { ActivityType } from '@/modules/gamification/gamification.types'; type CheckIntoEventInput = { @@ -48,9 +49,22 @@ export async function checkIntoEvent({ * - `event_registrations` * - `surveys` * + * And any scheduled bull jobs, such as: + * - `pre_event_notification` + * * @param id - ID of the event to delete. */ export async function deleteEvent(id: string) { + const jobID = await db + .selectFrom('events') + .select('preEventNotificationJobId') + .where('id', '=', id) + .executeTakeFirst(); + + if (jobID) { + await deletePreEventNotification(jobID); + } + await db.transaction().execute(async (trx) => { await trx .deleteFrom('completedActivities') diff --git a/packages/core/src/modules/event/use-cases/pre-event-notification.ts b/packages/core/src/modules/event/use-cases/pre-event-notification.ts index cf636789..520b0525 100644 --- a/packages/core/src/modules/event/use-cases/pre-event-notification.ts +++ b/packages/core/src/modules/event/use-cases/pre-event-notification.ts @@ -1,10 +1,19 @@ import dayjs from 'dayjs'; import { match } from 'ts-pattern'; -import { PreEventNotificationBullJob } from '@/infrastructure/bull/bull.types'; +import { db } from '@oyster/db'; +import { id } from '@oyster/utils'; + +import { getQueue } from '@/admin-dashboard.server'; +import { + type GetBullJobData, + PreEventNotificationBullJob, +} from '@/infrastructure/bull/bull.types'; import { job } from '@/infrastructure/bull/use-cases/job'; import { registerWorker } from '@/infrastructure/bull/use-cases/register-worker'; -// import { sendPreEventNotification } from './send-pre-event-notification'; + +// Change the line below with the function from issue #477 +// import { sendPreEventNotificationEmails } from './send-pre-event-notification-emails'; export const preEventNotificationWorker = registerWorker( 'pre_event_notification', @@ -12,8 +21,7 @@ export const preEventNotificationWorker = registerWorker( async (job) => { return match(job) .with({ name: 'pre_event_notification.created' }, ({ data }) => { - return console.log('Pre event notification for event sent'); - // return sendPreEventNotification(data); + return sendPreEventNotification(data); }) .exhaustive(); } @@ -25,6 +33,36 @@ type createPreEventNotificationJobInput = { timezone: string; }; +async function sendPreEventNotification({ + eventID, +}: GetBullJobData<'pre_event_notification.created'>) { + // sendPreEventNotificationEmails(eventID); + console.log('Pre event notifications for event with ID' + eventID + 'sent!'); + + await db + .updateTable('events') + .set({ preEventNotificationJobId: null }) + .where('id', '=', eventID) + .execute(); +} + +export async function deletePreEventNotification({ + preEventNotificationJobId, +}: { + preEventNotificationJobId: string | null; +}) { + if (preEventNotificationJobId) { + const queue = getQueue('pre_event_notification'); + const job = await queue.getJob(preEventNotificationJobId); + + if (!job) { + throw new Response(null, { status: 404 }); + } + + return job.remove(); + } +} + export async function createPreEventNotificationJob({ eventID, startDate, @@ -37,12 +75,18 @@ export async function createPreEventNotificationJob({ if (today.isBefore(twoDaysBeforeEvent)) { const delay = twoDaysBeforeEvent.diff(today); - const scheduledJob = job( + const customJobID = id(); + + job( 'pre_event_notification.created', { eventID }, - { delay } + { delay, jobId: customJobID } ); - } - // TODO: Add job ID to event in database + await db + .updateTable('events') + .set({ preEventNotificationJobId: customJobID }) + .where('id', '=', eventID) + .execute(); + } }