* 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>
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { BookingStatus, WebhookTriggerEvents } from "@prisma/client";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { v4 } from "uuid";
|
|
|
|
import { scheduleTrigger } from "@calcom/app-store/zapier/lib/nodeScheduler";
|
|
import findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
|
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const apiKey = req.query.apiKey as string;
|
|
|
|
if (!apiKey) {
|
|
return res.status(401).json({ message: "No API key provided" });
|
|
}
|
|
|
|
const validKey = await findValidApiKey(apiKey, "zapier");
|
|
|
|
if (!validKey) {
|
|
return res.status(401).json({ message: "API key not valid" });
|
|
}
|
|
|
|
const { subscriberUrl, triggerEvent } = req.body;
|
|
|
|
try {
|
|
const createSubscription = await prisma.webhook.create({
|
|
data: {
|
|
id: v4(),
|
|
userId: validKey.userId,
|
|
eventTriggers: [triggerEvent],
|
|
subscriberUrl,
|
|
active: true,
|
|
appId: "zapier",
|
|
},
|
|
});
|
|
|
|
if (triggerEvent === WebhookTriggerEvents.MEETING_ENDED) {
|
|
//schedule job for already existing bookings
|
|
const bookings = await prisma.booking.findMany({
|
|
where: {
|
|
userId: validKey.userId,
|
|
startTime: {
|
|
gte: new Date(),
|
|
},
|
|
status: BookingStatus.ACCEPTED,
|
|
},
|
|
});
|
|
|
|
for (const booking of bookings) {
|
|
scheduleTrigger(booking, createSubscription.subscriberUrl, {
|
|
id: createSubscription.id,
|
|
appId: createSubscription.appId,
|
|
});
|
|
}
|
|
}
|
|
res.status(200).json(createSubscription);
|
|
} catch (error) {
|
|
return res.status(500).json({ message: "Could not create subscription." });
|
|
}
|
|
}
|
|
|
|
export default defaultHandler({
|
|
POST: Promise.resolve({ default: defaultResponder(handler) }),
|
|
});
|