Merge branch 'main' into feat/admin-events
This commit is contained in:
commit
996ec31b26
8 files changed
+288
-5
No files matched your search
@@ -1,10 +1,15 @@
|
||||
import { WebhookTriggerEvents } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { BookingResponse, BookingsResponse } from "@lib/types";
|
||||
import sendPayload from "@lib/utils/sendPayload";
|
||||
import getWebhooks from "@lib/utils/webhookSubscriptions";
|
||||
import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
|
||||
import { schemaEventTypeReadPublic } from "@lib/validations/event-type";
|
||||
|
||||
async function createOrlistAllBookings(
|
||||
{ method, body, userId }: NextApiRequest,
|
||||
@@ -30,6 +35,7 @@ async function createOrlistAllBookings(
|
||||
*/
|
||||
const data = await prisma.booking.findMany({ where: { userId } });
|
||||
const bookings = data.map((booking) => schemaBookingReadPublic.parse(booking));
|
||||
console.log(`Bookings requested by ${userId}`);
|
||||
if (bookings) res.status(200).json({ bookings });
|
||||
else
|
||||
(error: Error) =>
|
||||
@@ -78,11 +84,67 @@ async function createOrlistAllBookings(
|
||||
return;
|
||||
}
|
||||
safe.data.userId = userId;
|
||||
const data = await prisma.booking.create({ data: { ...safe.data } });
|
||||
const data = await prisma.booking.create({ data: { uid: uuidv4(), ...safe.data } });
|
||||
const booking = schemaBookingReadPublic.parse(data);
|
||||
|
||||
if (booking) res.status(201).json({ booking, message: "Booking created successfully" });
|
||||
else
|
||||
if (booking) {
|
||||
const eventType = await prisma.eventType
|
||||
.findUnique({ where: { id: booking.eventTypeId as number } })
|
||||
.then((data) => schemaEventTypeReadPublic.parse(data))
|
||||
.catch((e: Error) => {
|
||||
console.error(`Event type with ID: ${booking.eventTypeId} not found`, e);
|
||||
});
|
||||
console.log(`eventType: ${eventType}`);
|
||||
const evt = {
|
||||
type: eventType?.title || booking.title,
|
||||
title: booking.title,
|
||||
description: "",
|
||||
additionalNotes: "",
|
||||
customInputs: {},
|
||||
startTime: booking.startTime.toISOString(),
|
||||
endTime: booking.endTime.toISOString(),
|
||||
organizer: {
|
||||
name: "",
|
||||
email: "",
|
||||
timeZone: "",
|
||||
language: {
|
||||
locale: "en",
|
||||
},
|
||||
},
|
||||
attendees: [],
|
||||
location: "",
|
||||
destinationCalendar: null,
|
||||
hideCalendar: false,
|
||||
uid: booking.uid,
|
||||
metadata: {},
|
||||
};
|
||||
console.log(`evt: ${evt}`);
|
||||
|
||||
// Send Webhook call if hooked to BOOKING_CREATED
|
||||
const triggerEvent = WebhookTriggerEvents.BOOKING_CREATED;
|
||||
console.log(`Trigger Event: ${triggerEvent}`);
|
||||
const subscriberOptions = {
|
||||
userId,
|
||||
eventTypeId: booking.eventTypeId as number,
|
||||
triggerEvent,
|
||||
};
|
||||
console.log(`subscriberOptions: ${subscriberOptions}`);
|
||||
|
||||
const subscribers = await getWebhooks(subscriberOptions);
|
||||
console.log(`subscribers: ${subscribers}`);
|
||||
const bookingId = booking?.id;
|
||||
const promises = subscribers.map((sub) =>
|
||||
sendPayload(triggerEvent, new Date().toISOString(), sub, {
|
||||
...evt,
|
||||
bookingId,
|
||||
}).catch((e) => {
|
||||
console.error(`Error executing webhook for event: ${triggerEvent}, URL: ${sub.subscriberUrl}`, e);
|
||||
})
|
||||
);
|
||||
await Promise.all(promises);
|
||||
console.log("All promises resolved! About to send the response");
|
||||
res.status(201).json({ booking, message: "Booking created successfully" });
|
||||
} else
|
||||
(error: Error) => {
|
||||
console.log(error);
|
||||
res.status(400).json({
|
||||
|
||||
@@ -95,6 +95,26 @@ export async function WebhookById(
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (safeBody.data.eventTypeId) {
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
eventTypes: {
|
||||
some: {
|
||||
id: safeBody.data.eventTypeId,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
members: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Team should be available and the user should be a member of the team
|
||||
if (!team?.members.some((membership) => membership.userId === userId)) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
await prisma.webhook
|
||||
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
||||
.then((data) => schemaWebhookReadPublic.parse(data))
|
||||
|
||||
@@ -61,6 +61,26 @@ async function createOrlistAllWebhooks(
|
||||
res.status(400).json({ message: "Invalid request body" });
|
||||
return;
|
||||
}
|
||||
if (safe.data.eventTypeId) {
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
eventTypes: {
|
||||
some: {
|
||||
id: safe.data.eventTypeId,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
members: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Team should be available and the user should be a member of the team
|
||||
if (!team?.members.some((membership) => membership.userId === userId)) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
const data = await prisma.webhook.create({ data: { id: uuidv4(), ...safe.data, userId } });
|
||||
if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" });
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user