* add new trigger with first simple job scheduling * fix DB update * use array to save scheduled jobs in booking * cancel scheduled jobs when zap disabled or zapier disconnected * schedule jobs only for confirmed bookings * schedule jobs for already existing bookings * fix bug to create workflow reminders when confirming recurring events * delete remove all zapier webhooks when api key is deleted * schedule job for all confirmed recurring bookings * fix zapier trigger and workflow reminders when cancelling recurring events * code clean up * code clean up * add migration * add type package for node-schedule * remove nodescheduler * add updated nodescheduler * move code to app-store * add meeting ended to webhook constants * udpate zapier README.md * implement QA suggestions * add default handler and fix imports * Type fix Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Omar López <zomars@me.com>
84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import schedule from "node-schedule";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
export async function scheduleTrigger(
|
|
booking: { id: number; endTime: Date; scheduledJobs: string[] },
|
|
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,
|
|
});
|
|
|
|
//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,
|
|
},
|
|
});
|
|
}
|
|
);
|
|
|
|
//add scheduled job name to booking
|
|
await prisma.booking.update({
|
|
where: {
|
|
id: booking.id,
|
|
},
|
|
data: {
|
|
scheduledJobs: {
|
|
push: job.name,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function cancelScheduledJobs(
|
|
booking: { uid: string; scheduledJobs?: string[] },
|
|
appId?: string | null
|
|
) {
|
|
let scheduledJobs = booking.scheduledJobs || [];
|
|
|
|
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 = [];
|
|
}
|
|
|
|
await prisma.booking.update({
|
|
where: {
|
|
uid: booking.uid,
|
|
},
|
|
data: {
|
|
scheduledJobs: scheduledJobs,
|
|
},
|
|
});
|
|
});
|
|
}
|
|
}
|