From 7a93bec7356f74e1017a0da5093429cd323ff5fe Mon Sep 17 00:00:00 2001 From: alannnc Date: Fri, 28 Oct 2022 03:36:30 -0600 Subject: [PATCH] Add try-catch and validation to run triggers on reschedule (#5252) * Add try-catch and validation to run triggers on reschedule * Update packages/features/bookings/lib/handleNewBooking.ts Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../app-store/zapier/lib/nodeScheduler.ts | 124 ++++++++++-------- 1 file changed, 66 insertions(+), 58 deletions(-) diff --git a/packages/app-store/zapier/lib/nodeScheduler.ts b/packages/app-store/zapier/lib/nodeScheduler.ts index f0096a9f43..2dd4b75681 100644 --- a/packages/app-store/zapier/lib/nodeScheduler.ts +++ b/packages/app-store/zapier/lib/nodeScheduler.ts @@ -7,44 +7,48 @@ export async function scheduleTrigger( subscriberUrl: string, subscriber: { id: string; appId: string | null } ) { - //schedule job to call subscriber url at the end of meeting - const job = schedule.scheduleJob( - `${subscriber.appId}_${subscriber.id}`, - booking.endTime, - async function () { - const body = JSON.stringify(booking); - await fetch(subscriberUrl, { - method: "POST", - body, - }); + try { + //schedule job to call subscriber url at the end of meeting + const job = schedule.scheduleJob( + `${subscriber.appId}_${subscriber.id}`, + booking.endTime, + async function () { + const body = JSON.stringify(booking); + await fetch(subscriberUrl, { + method: "POST", + body, + }); - //remove scheduled job from bookings once triggered - const updatedScheduledJobs = booking.scheduledJobs.filter((scheduledJob) => { - return scheduledJob !== `${subscriber.appId}_${subscriber.id}`; - }); + //remove scheduled job from bookings once triggered + const updatedScheduledJobs = booking.scheduledJobs.filter((scheduledJob) => { + return scheduledJob !== `${subscriber.appId}_${subscriber.id}`; + }); - await prisma.booking.update({ - where: { - id: booking.id, - }, - data: { - scheduledJobs: updatedScheduledJobs, - }, - }); - } - ); + await prisma.booking.update({ + where: { + id: booking.id, + }, + data: { + scheduledJobs: updatedScheduledJobs, + }, + }); + } + ); - //add scheduled job name to booking - await prisma.booking.update({ - where: { - id: booking.id, - }, - data: { - scheduledJobs: { - push: job.name, + //add scheduled job name to booking + await prisma.booking.update({ + where: { + id: booking.id, }, - }, - }); + data: { + scheduledJobs: { + push: job.name, + }, + }, + }); + } catch (error) { + console.error("Error cancelling scheduled jobs", error); + } } export async function cancelScheduledJobs( @@ -52,35 +56,39 @@ export async function cancelScheduledJobs( appId?: string | null, isReschedule?: boolean ) { - let scheduledJobs = booking.scheduledJobs || []; + try { + let scheduledJobs = booking.scheduledJobs || []; - if (booking.scheduledJobs) { - booking.scheduledJobs.forEach(async (scheduledJob) => { - if (appId) { - if (scheduledJob.startsWith(appId)) { + if (booking.scheduledJobs) { + booking.scheduledJobs.forEach(async (scheduledJob) => { + if (appId) { + if (scheduledJob.startsWith(appId)) { + if (schedule.scheduledJobs[scheduledJob]) { + schedule.scheduledJobs[scheduledJob].cancel(); + } + scheduledJobs = scheduledJobs?.filter((job) => scheduledJob !== job) || []; + } + } else { + //if no specific appId given, delete all scheduled jobs of booking if (schedule.scheduledJobs[scheduledJob]) { schedule.scheduledJobs[scheduledJob].cancel(); } - scheduledJobs = scheduledJobs?.filter((job) => scheduledJob !== job) || []; + scheduledJobs = []; } - } else { - //if no specific appId given, delete all scheduled jobs of booking - if (schedule.scheduledJobs[scheduledJob]) { - schedule.scheduledJobs[scheduledJob].cancel(); - } - scheduledJobs = []; - } - if (!isReschedule) { - await prisma.booking.update({ - where: { - uid: booking.uid, - }, - data: { - scheduledJobs: scheduledJobs, - }, - }); - } - }); + if (!isReschedule) { + await prisma.booking.update({ + where: { + uid: booking.uid, + }, + data: { + scheduledJobs: scheduledJobs, + }, + }); + } + }); + } + } catch (error) { + console.error("Error cancelling scheduled jobs", error); } }