-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Delete/Create WebhookScheduledTriggers for existing bookings #14121
Changes from 3 commits
9f50f12
ad08aac
0d0fa2c
13ea956
bd8d500
ea019ab
b98f4d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import type { Prisma } from "@prisma/client"; | ||
import type { Prisma, Webhook } from "@prisma/client"; | ||
import { v4 } from "uuid"; | ||
|
||
import { getHumanReadableLocationValue } from "@calcom/core/location"; | ||
|
@@ -10,6 +10,11 @@ import prisma from "@calcom/prisma"; | |
import type { ApiKey } from "@calcom/prisma/client"; | ||
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums"; | ||
|
||
const SCHEDULING_TRIGGER: WebhookTriggerEvents[] = [ | ||
WebhookTriggerEvents.MEETING_ENDED, | ||
WebhookTriggerEvents.MEETING_STARTED, | ||
]; | ||
|
||
const log = logger.getSubLogger({ prefix: ["[node-scheduler]"] }); | ||
|
||
export async function addSubscription({ | ||
|
@@ -321,7 +326,8 @@ export async function scheduleTrigger( | |
export async function cancelScheduledJobs( | ||
booking: { uid: string; scheduledJobs?: string[] }, | ||
appId?: string | null, | ||
isReschedule?: boolean | ||
isReschedule?: boolean, | ||
triggerEvent?: WebhookTriggerEvents | ||
) { | ||
if (!booking.scheduledJobs) return; | ||
|
||
|
@@ -338,12 +344,24 @@ export async function cancelScheduledJobs( | |
} | ||
} else { | ||
//if no specific appId given, delete all scheduled jobs of booking | ||
await prisma.webhookScheduledTriggers.deleteMany({ | ||
where: { | ||
jobName: scheduledJob, | ||
}, | ||
}); | ||
scheduledJobs = []; | ||
if (triggerEvent) { | ||
const shouldContain = `"triggerEvent":"${triggerEvent}"`; | ||
await prisma.webhookScheduledTriggers.deleteMany({ | ||
where: { | ||
jobName: scheduledJob, | ||
payload: { | ||
contains: shouldContain, | ||
}, | ||
}, | ||
}); | ||
} else { | ||
await prisma.webhookScheduledTriggers.deleteMany({ | ||
where: { | ||
jobName: scheduledJob, | ||
}, | ||
}); | ||
scheduledJobs = []; | ||
} | ||
} | ||
|
||
if (!isReschedule) { | ||
|
@@ -364,3 +382,57 @@ export async function cancelScheduledJobs( | |
console.error("Error cancelling scheduled jobs", error); | ||
} | ||
} | ||
|
||
export async function updateTriggerForExistingBookings( | ||
webhook: Webhook, | ||
existingEventTriggers: WebhookTriggerEvents[], | ||
updatedEventTriggers: WebhookTriggerEvents[] | ||
) { | ||
const addedEventTriggers = updatedEventTriggers.filter( | ||
(trigger) => !existingEventTriggers.includes(trigger) && SCHEDULING_TRIGGER.includes(trigger) | ||
); | ||
const removedEventTriggers = existingEventTriggers.filter( | ||
(trigger) => !updatedEventTriggers.includes(trigger) && SCHEDULING_TRIGGER.includes(trigger) | ||
); | ||
const currentTime = new Date(); | ||
const bookings = await prisma.booking.findMany({ | ||
where: { | ||
eventTypeId: webhook.eventTypeId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can already query for only ACCEPTED bookings here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah right, slipped my mind 😅 |
||
OR: [{ startTime: { gt: currentTime } }, { endTime: { gt: currentTime } }], | ||
}, | ||
}); | ||
|
||
if (bookings.length === 0) return; | ||
|
||
if (addedEventTriggers.length > 0) { | ||
const promise = bookings.flatMap((booking) => { | ||
if (booking.status === BookingStatus.ACCEPTED) { | ||
return addedEventTriggers.map((trigger) => { | ||
scheduleTrigger(booking, webhook.subscriberUrl, webhook, trigger); | ||
}); | ||
} else { | ||
return []; | ||
} | ||
}); | ||
|
||
await Promise.all(promise); | ||
} | ||
|
||
if ( | ||
removedEventTriggers.length > 0 && | ||
removedEventTriggers.some((trigger) => SCHEDULING_TRIGGER.includes(trigger)) | ||
) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why do we need that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot to remove it from here. |
||
const promise = bookings.map((booking) => { | ||
removedEventTriggers.map((trigger) => | ||
cancelScheduledJobs( | ||
{ uid: booking.uid, scheduledJobs: booking.scheduledJobs }, | ||
undefined, | ||
false, | ||
trigger | ||
) | ||
); | ||
}); | ||
|
||
await Promise.all(promise); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's early return before that if there are no added or removed triggers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.