Skip to content

Commit

Permalink
Modify preEventNotficationJobId in db or remove job from queue as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa1510 committed Sep 10, 2024
1 parent af9ec6e commit f01498b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
14 changes: 14 additions & 0 deletions packages/core/src/modules/event/event.core.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
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',
PreEventNotificationBullJob,
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();
}
Expand All @@ -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,
Expand All @@ -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();
}
}

0 comments on commit f01498b

Please sign in to comment.