2022-08-15 20:18:41 +00:00
|
|
|
import schedule from "node-schedule";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
2023-06-26 18:35:32 +00:00
|
|
|
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
|
2022-08-15 20:18:41 +00:00
|
|
|
|
|
|
|
export async function scheduleTrigger(
|
|
|
|
booking: { id: number; endTime: Date; scheduledJobs: string[] },
|
|
|
|
subscriberUrl: string,
|
|
|
|
subscriber: { id: string; appId: string | null }
|
|
|
|
) {
|
2022-10-28 09:36:30 +00:00
|
|
|
try {
|
|
|
|
//schedule job to call subscriber url at the end of meeting
|
2023-08-26 00:43:10 +00:00
|
|
|
// FIXME: in-process scheduling - job will vanish on server crash / restart
|
2022-10-28 09:36:30 +00:00
|
|
|
const job = schedule.scheduleJob(
|
|
|
|
`${subscriber.appId}_${subscriber.id}`,
|
|
|
|
booking.endTime,
|
|
|
|
async function () {
|
2023-06-26 18:35:32 +00:00
|
|
|
const body = JSON.stringify({ triggerEvent: WebhookTriggerEvents.MEETING_ENDED, ...booking });
|
2022-10-28 09:36:30 +00:00
|
|
|
await fetch(subscriberUrl, {
|
|
|
|
method: "POST",
|
|
|
|
body,
|
|
|
|
});
|
2022-08-15 20:18:41 +00:00
|
|
|
|
2022-10-28 09:36:30 +00:00
|
|
|
//remove scheduled job from bookings once triggered
|
|
|
|
const updatedScheduledJobs = booking.scheduledJobs.filter((scheduledJob) => {
|
|
|
|
return scheduledJob !== `${subscriber.appId}_${subscriber.id}`;
|
|
|
|
});
|
2022-08-15 20:18:41 +00:00
|
|
|
|
2022-10-28 09:36:30 +00:00
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: booking.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduledJobs: updatedScheduledJobs,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2022-08-15 20:18:41 +00:00
|
|
|
|
2022-10-28 09:36:30 +00:00
|
|
|
//add scheduled job name to booking
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: booking.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduledJobs: {
|
|
|
|
push: job.name,
|
|
|
|
},
|
2022-08-15 20:18:41 +00:00
|
|
|
},
|
2022-10-28 09:36:30 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error cancelling scheduled jobs", error);
|
|
|
|
}
|
2022-08-15 20:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function cancelScheduledJobs(
|
|
|
|
booking: { uid: string; scheduledJobs?: string[] },
|
2022-10-25 19:16:16 +00:00
|
|
|
appId?: string | null,
|
|
|
|
isReschedule?: boolean
|
2022-08-15 20:18:41 +00:00
|
|
|
) {
|
2023-08-26 00:43:10 +00:00
|
|
|
if (!booking.scheduledJobs) return;
|
2022-08-15 20:18:41 +00:00
|
|
|
|
2023-08-26 00:43:10 +00:00
|
|
|
let scheduledJobs = booking.scheduledJobs || [];
|
|
|
|
const promises = booking.scheduledJobs.map(async (scheduledJob) => {
|
|
|
|
if (appId) {
|
|
|
|
if (scheduledJob.startsWith(appId)) {
|
|
|
|
if (schedule.scheduledJobs[scheduledJob]) {
|
|
|
|
schedule.scheduledJobs[scheduledJob].cancel();
|
2022-08-15 20:18:41 +00:00
|
|
|
}
|
2023-08-26 00:43:10 +00:00
|
|
|
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 = [];
|
|
|
|
}
|
2022-08-15 20:18:41 +00:00
|
|
|
|
2023-08-26 00:43:10 +00:00
|
|
|
if (!isReschedule) {
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
uid: booking.uid,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
scheduledJobs: scheduledJobs,
|
|
|
|
},
|
2022-10-28 09:36:30 +00:00
|
|
|
});
|
|
|
|
}
|
2023-08-26 00:43:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(promises);
|
2022-10-28 09:36:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error cancelling scheduled jobs", error);
|
2022-08-15 20:18:41 +00:00
|
|
|
}
|
|
|
|
}
|