subscribe unsubscribe in zapier using common nodeScheduler
This commit is contained in:
parent
486a24ca62
commit
d7067a1d49
|
@ -1,11 +1,8 @@
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
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 findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
|
||||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
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) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const apiKey = req.query.apiKey as string;
|
const apiKey = req.query.apiKey as string;
|
||||||
|
@ -22,41 +19,18 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
const { subscriberUrl, triggerEvent } = req.body;
|
const { subscriberUrl, triggerEvent } = req.body;
|
||||||
|
|
||||||
try {
|
const createAppSubscription = await addSubscription({
|
||||||
const createSubscription = await prisma.webhook.create({
|
appApiKey: validKey,
|
||||||
data: {
|
triggerEvent: triggerEvent,
|
||||||
id: v4(),
|
subscriberUrl: subscriberUrl,
|
||||||
userId: validKey.userId,
|
appId: "zapier",
|
||||||
eventTriggers: [triggerEvent],
|
});
|
||||||
subscriberUrl,
|
|
||||||
active: true,
|
|
||||||
appId: "zapier",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (triggerEvent === WebhookTriggerEvents.MEETING_ENDED) {
|
if (!createAppSubscription) {
|
||||||
//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." });
|
return res.status(500).json({ message: "Could not create subscription." });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res.status(200).json(createAppSubscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defaultHandler({
|
export default defaultHandler({
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
|
|
||||||
|
import { deleteSubscription } from "@calcom/app-store/_utils/nodeScheduler";
|
||||||
import findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
|
import findValidApiKey from "@calcom/features/ee/api-keys/lib/findValidApiKey";
|
||||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||||
import prisma from "@calcom/prisma";
|
|
||||||
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
|
|
||||||
|
|
||||||
const querySchema = z.object({
|
const querySchema = z.object({
|
||||||
apiKey: z.string(),
|
apiKey: z.string(),
|
||||||
|
@ -24,41 +23,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
return res.status(401).json({ message: "API key not valid" });
|
return res.status(401).json({ message: "API key not valid" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const webhook = await prisma.webhook.findFirst({
|
const deleteEventSubscription = await deleteSubscription({
|
||||||
where: {
|
appApiKey: validKey,
|
||||||
id,
|
webhookId: id,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (webhook?.eventTriggers.includes(WebhookTriggerEvents.MEETING_ENDED)) {
|
if (!deleteEventSubscription) {
|
||||||
const bookingsWithScheduledJobs = await prisma.booking.findMany({
|
return res.status(500).json({ message: "Could not delete subscription." });
|
||||||
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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.webhook.delete({
|
|
||||||
where: {
|
|
||||||
id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
res.status(204).json({ message: "Subscription is deleted." });
|
res.status(204).json({ message: "Subscription is deleted." });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user