subscribe unsubscribe in zapier using common nodeScheduler

This commit is contained in:
aar2dee2 2023-07-01 22:27:49 +05:30
parent 486a24ca62
commit d7067a1d49
2 changed files with 16 additions and 70 deletions

View File

@ -1,11 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { v4 } from "uuid";
import { scheduleTrigger } from "@calcom/app-store/_utils/nodeScheduler";
import { addSubscription } from "@calcom/app-store/_utils/nodeScheduler";
import findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import prisma from "@calcom/prisma";
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
async function handler(req: NextApiRequest, res: NextApiResponse) {
const apiKey = req.query.apiKey as string;
@ -22,41 +19,18 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
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",
},
});
const createAppSubscription = await addSubscription({
appApiKey: validKey,
triggerEvent: triggerEvent,
subscriberUrl: subscriberUrl,
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) {
if (!createAppSubscription) {
return res.status(500).json({ message: "Could not create subscription." });
}
res.status(200).json(createAppSubscription);
}
export default defaultHandler({

View File

@ -1,10 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";
import z from "zod";
import { deleteSubscription } from "@calcom/app-store/_utils/nodeScheduler";
import findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import prisma from "@calcom/prisma";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
const querySchema = z.object({
apiKey: z.string(),
@ -24,41 +23,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
return res.status(401).json({ message: "API key not valid" });
}
const webhook = await prisma.webhook.findFirst({
where: {
id,
},
const deleteEventSubscription = await deleteSubscription({
appApiKey: validKey,
webhookId: id,
});
if (webhook?.eventTriggers.includes(WebhookTriggerEvents.MEETING_ENDED)) {
const bookingsWithScheduledJobs = await prisma.booking.findMany({
where: {
userId: validKey.userId,
scheduledJobs: {
isEmpty: false,
},
},
});
for (const booking of bookingsWithScheduledJobs) {
const updatedScheduledJobs = booking.scheduledJobs.filter(
(scheduledJob) => scheduledJob !== `zapier_${webhook.id}`
);
await prisma.booking.update({
where: {
id: booking.id,
},
data: {
scheduledJobs: updatedScheduledJobs,
},
});
}
if (!deleteEventSubscription) {
return res.status(500).json({ message: "Could not delete subscription." });
}
await prisma.webhook.delete({
where: {
id,
},
});
res.status(204).json({ message: "Subscription is deleted." });
}