2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
BookingStatus,
|
|
|
|
Credential,
|
|
|
|
WebhookTriggerEvents,
|
|
|
|
Prisma,
|
|
|
|
PrismaPromise,
|
|
|
|
WorkflowMethods,
|
|
|
|
} from "@prisma/client";
|
2021-09-22 19:52:38 +00:00
|
|
|
import async from "async";
|
2021-10-26 16:17:24 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-06-19 15:02:00 +00:00
|
|
|
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
2022-03-23 22:00:30 +00:00
|
|
|
import { FAKE_DAILY_CREDENTIAL } from "@calcom/app-store/dailyvideo/lib/VideoApiAdapter";
|
|
|
|
import { deleteMeeting } from "@calcom/core/videoClient";
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-06-06 17:49:56 +00:00
|
|
|
import { sendCancelledEmails } from "@calcom/emails";
|
2022-06-10 00:32:34 +00:00
|
|
|
import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
|
2022-05-18 21:05:49 +00:00
|
|
|
import prisma, { bookingMinimalSelect } from "@calcom/prisma";
|
2022-03-23 22:00:30 +00:00
|
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
2021-09-22 18:36:13 +00:00
|
|
|
import { refund } from "@ee/lib/stripe/server";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { deleteScheduledEmailReminder } from "@ee/lib/workflows/reminders/emailReminderManager";
|
|
|
|
import { sendCancelledReminders } from "@ee/lib/workflows/reminders/reminderScheduler";
|
|
|
|
import { deleteScheduledSMSReminder } from "@ee/lib/workflows/reminders/smsReminderManager";
|
2021-06-06 01:51:24 +00:00
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import { asStringOrNull } from "@lib/asStringOrNull";
|
2021-09-28 19:12:48 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-10-07 15:14:47 +00:00
|
|
|
import sendPayload from "@lib/webhooks/sendPayload";
|
2022-05-03 23:16:59 +00:00
|
|
|
import getWebhooks from "@lib/webhooks/subscriptions";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
import { getTranslation } from "@server/lib/i18n";
|
2021-10-07 16:12:39 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-09-14 08:45:28 +00:00
|
|
|
// just bail if it not a DELETE
|
2021-10-07 15:14:47 +00:00
|
|
|
if (req.method !== "DELETE" && req.method !== "POST") {
|
2021-09-14 08:45:28 +00:00
|
|
|
return res.status(405).end();
|
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const uid = asStringOrNull(req.body.uid) || "";
|
2022-07-19 19:06:23 +00:00
|
|
|
const allRemainingBookings = asStringOrNull(req.body.allRemainingBookings) || "";
|
2022-01-28 17:40:29 +00:00
|
|
|
const cancellationReason = asStringOrNull(req.body.reason) || "";
|
2021-09-28 17:23:50 +00:00
|
|
|
const session = await getSession({ req: req });
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
const bookingToDelete = await prisma.booking.findUnique({
|
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
select: {
|
2022-05-18 21:05:49 +00:00
|
|
|
...bookingMinimalSelect,
|
2022-06-10 20:38:06 +00:00
|
|
|
recurringEventId: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
userId: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
user: {
|
|
|
|
select: {
|
2021-09-28 17:23:50 +00:00
|
|
|
id: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
credentials: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
email: true,
|
|
|
|
timeZone: true,
|
|
|
|
name: true,
|
2022-01-13 19:47:15 +00:00
|
|
|
destinationCalendar: true,
|
2021-06-09 19:46:41 +00:00
|
|
|
},
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
2021-10-07 16:12:39 +00:00
|
|
|
location: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
references: {
|
|
|
|
select: {
|
|
|
|
uid: true,
|
|
|
|
type: true,
|
2022-05-16 20:20:09 +00:00
|
|
|
externalCalendarId: true,
|
2022-07-19 22:00:01 +00:00
|
|
|
credentialId: true,
|
2021-07-15 21:34:55 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-22 18:36:13 +00:00
|
|
|
payment: true,
|
|
|
|
paid: true,
|
2022-01-21 12:06:45 +00:00
|
|
|
eventType: {
|
|
|
|
select: {
|
2022-06-10 00:32:34 +00:00
|
|
|
recurringEvent: true,
|
2022-01-21 12:06:45 +00:00
|
|
|
title: true,
|
2022-07-14 00:10:45 +00:00
|
|
|
workflows: {
|
|
|
|
include: {
|
|
|
|
workflow: {
|
|
|
|
include: {
|
|
|
|
steps: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-21 12:06:45 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-22 18:36:13 +00:00
|
|
|
uid: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
eventTypeId: true,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: true,
|
2022-07-14 00:10:45 +00:00
|
|
|
smsReminderNumber: true,
|
|
|
|
workflowReminders: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
});
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-09-28 17:23:50 +00:00
|
|
|
if (!bookingToDelete || !bookingToDelete.user) {
|
2021-09-14 08:45:28 +00:00
|
|
|
return res.status(404).end();
|
|
|
|
}
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if ((!session || session.user?.id !== bookingToDelete.user?.id) && bookingToDelete.startTime < new Date()) {
|
2021-09-28 17:23:50 +00:00
|
|
|
return res.status(403).json({ message: "Cannot cancel past events" });
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:17:24 +00:00
|
|
|
if (!bookingToDelete.userId) {
|
|
|
|
return res.status(404).json({ message: "User not found" });
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:14:47 +00:00
|
|
|
const organizer = await prisma.user.findFirst({
|
|
|
|
where: {
|
2021-10-26 16:17:24 +00:00
|
|
|
id: bookingToDelete.userId,
|
2021-10-07 15:14:47 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
timeZone: true,
|
2022-01-27 20:32:53 +00:00
|
|
|
locale: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
},
|
2021-10-26 16:17:24 +00:00
|
|
|
rejectOnNotFound: true,
|
2021-10-07 15:14:47 +00:00
|
|
|
});
|
|
|
|
|
2022-01-27 20:32:53 +00:00
|
|
|
const attendeesListPromises = bookingToDelete.attendees.map(async (attendee) => {
|
|
|
|
return {
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
timeZone: attendee.timeZone,
|
|
|
|
language: {
|
|
|
|
translate: await getTranslation(attendee.locale ?? "en", "common"),
|
|
|
|
locale: attendee.locale ?? "en",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const attendeesList = await Promise.all(attendeesListPromises);
|
|
|
|
const tOrganizer = await getTranslation(organizer.locale ?? "en", "common");
|
2021-10-26 16:17:24 +00:00
|
|
|
|
2021-10-07 15:14:47 +00:00
|
|
|
const evt: CalendarEvent = {
|
|
|
|
title: bookingToDelete?.title,
|
2022-04-08 16:50:10 +00:00
|
|
|
type: (bookingToDelete?.eventType?.title as string) || bookingToDelete?.title,
|
2021-10-07 15:14:47 +00:00
|
|
|
description: bookingToDelete?.description || "",
|
2022-05-18 21:05:49 +00:00
|
|
|
customInputs: isPrismaObjOrUndefined(bookingToDelete.customInputs),
|
2022-01-26 16:41:18 +00:00
|
|
|
startTime: bookingToDelete?.startTime ? dayjs(bookingToDelete.startTime).format() : "",
|
|
|
|
endTime: bookingToDelete?.endTime ? dayjs(bookingToDelete.endTime).format() : "",
|
2021-10-26 16:17:24 +00:00
|
|
|
organizer: {
|
|
|
|
email: organizer.email,
|
|
|
|
name: organizer.name ?? "Nameless",
|
|
|
|
timeZone: organizer.timeZone,
|
2022-01-27 20:32:53 +00:00
|
|
|
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
|
2021-10-26 16:17:24 +00:00
|
|
|
},
|
2022-01-27 20:32:53 +00:00
|
|
|
attendees: attendeesList,
|
2021-10-26 16:17:24 +00:00
|
|
|
uid: bookingToDelete?.uid,
|
2022-07-19 19:06:23 +00:00
|
|
|
/* Include recurringEvent information only when cancelling all bookings */
|
|
|
|
recurringEvent:
|
|
|
|
allRemainingBookings === "true"
|
|
|
|
? parseRecurringEvent(bookingToDelete.eventType?.recurringEvent)
|
|
|
|
: undefined,
|
2021-11-26 11:03:43 +00:00
|
|
|
location: bookingToDelete?.location,
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: bookingToDelete?.destinationCalendar || bookingToDelete?.user.destinationCalendar,
|
2022-01-28 17:40:29 +00:00
|
|
|
cancellationReason: cancellationReason,
|
2021-10-07 15:14:47 +00:00
|
|
|
};
|
|
|
|
// Hook up the webhook logic here
|
2022-03-02 16:24:57 +00:00
|
|
|
const eventTrigger: WebhookTriggerEvents = "BOOKING_CANCELLED";
|
2021-10-07 15:14:47 +00:00
|
|
|
// Send Webhook call if hooked to BOOKING.CANCELLED
|
2022-03-02 16:24:57 +00:00
|
|
|
const subscriberOptions = {
|
|
|
|
userId: bookingToDelete.userId,
|
2022-03-11 09:00:25 +00:00
|
|
|
eventTypeId: (bookingToDelete.eventTypeId as number) || 0,
|
2022-03-02 16:24:57 +00:00
|
|
|
triggerEvent: eventTrigger,
|
|
|
|
};
|
2022-05-03 23:16:59 +00:00
|
|
|
const webhooks = await getWebhooks(subscriberOptions);
|
|
|
|
const promises = webhooks.map((webhook) =>
|
2022-06-16 16:21:48 +00:00
|
|
|
sendPayload(webhook.secret, eventTrigger, new Date().toISOString(), webhook, evt).catch((e) => {
|
2022-05-03 23:16:59 +00:00
|
|
|
console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${webhook.subscriberUrl}`, e);
|
|
|
|
})
|
2021-10-07 15:14:47 +00:00
|
|
|
);
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
// by cancelling first, and blocking whilst doing so; we can ensure a cancel
|
|
|
|
// action always succeeds even if subsequent integrations fail cancellation.
|
2022-07-19 19:06:23 +00:00
|
|
|
if (bookingToDelete.eventType?.recurringEvent && allRemainingBookings === "true") {
|
2022-07-19 17:06:43 +00:00
|
|
|
const recurringEventId = bookingToDelete.recurringEventId;
|
|
|
|
const where = recurringEventId === null ? { uid } : { recurringEventId };
|
2022-07-19 19:06:23 +00:00
|
|
|
// Proceed to mark as cancelled all remaining recurring events instances (greater than or equal to right now)
|
2022-06-10 20:38:06 +00:00
|
|
|
await prisma.booking.updateMany({
|
2022-07-19 19:06:23 +00:00
|
|
|
where: {
|
|
|
|
...where,
|
|
|
|
startTime: {
|
|
|
|
gte: new Date(),
|
|
|
|
},
|
|
|
|
},
|
2022-06-10 20:38:06 +00:00
|
|
|
data: {
|
|
|
|
status: BookingStatus.CANCELLED,
|
|
|
|
cancellationReason: cancellationReason,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
status: BookingStatus.CANCELLED,
|
|
|
|
cancellationReason: cancellationReason,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
/** TODO: Remove this without breaking functionality */
|
2021-10-26 16:17:24 +00:00
|
|
|
if (bookingToDelete.location === "integrations:daily") {
|
|
|
|
bookingToDelete.user.credentials.push(FAKE_DAILY_CREDENTIAL);
|
|
|
|
}
|
|
|
|
|
2022-07-19 22:00:01 +00:00
|
|
|
const apiDeletes = [];
|
|
|
|
|
|
|
|
const bookingCalendarReference = bookingToDelete.references.find((reference) =>
|
|
|
|
reference.type.includes("_calendar")
|
|
|
|
);
|
|
|
|
|
|
|
|
if (bookingCalendarReference) {
|
|
|
|
const { credentialId, uid, externalCalendarId } = bookingCalendarReference;
|
|
|
|
// If the booking calendar reference contains a credentialId
|
|
|
|
if (credentialId) {
|
|
|
|
// Find the correct calendar credential under user credentials
|
|
|
|
const calendarCredential = bookingToDelete.user.credentials.find(
|
|
|
|
(credential) => credential.id === credentialId
|
|
|
|
);
|
|
|
|
if (calendarCredential) {
|
|
|
|
const calendar = getCalendar(calendarCredential);
|
|
|
|
apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId));
|
2021-06-14 17:00:17 +00:00
|
|
|
}
|
2022-07-19 22:00:01 +00:00
|
|
|
// For bookings made before the refactor we go through the old behaviour of running through each calendar credential
|
|
|
|
} else {
|
|
|
|
bookingToDelete.user.credentials
|
|
|
|
.filter((credential) => credential.type.endsWith("_calendar"))
|
|
|
|
.forEach((credential) => {
|
|
|
|
const calendar = getCalendar(credential);
|
|
|
|
apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId));
|
|
|
|
});
|
2021-09-14 08:45:28 +00:00
|
|
|
}
|
2022-07-19 22:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const bookingVideoReference = bookingToDelete.references.find((reference) =>
|
|
|
|
reference.type.includes("_video")
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the video reference has a credentialId find the specific credential
|
|
|
|
if (bookingVideoReference && bookingVideoReference.credentialId) {
|
|
|
|
const { credentialId, uid } = bookingVideoReference;
|
|
|
|
if (credentialId) {
|
|
|
|
const videoCredential = bookingToDelete.user.credentials.find(
|
|
|
|
(credential) => credential.id === credentialId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (videoCredential) {
|
|
|
|
apiDeletes.push(deleteMeeting(videoCredential, uid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For bookings made before this refactor we go through the old behaviour of running through each video credential
|
|
|
|
} else {
|
|
|
|
bookingToDelete.user.credentials
|
|
|
|
.filter((credential) => credential.type.endsWith("_video"))
|
|
|
|
.forEach((credential) => {
|
|
|
|
apiDeletes.push(deleteMeeting(credential, uid));
|
|
|
|
});
|
|
|
|
}
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2022-06-10 00:32:34 +00:00
|
|
|
// Avoiding taking care of recurrence for now as Payments are not supported with Recurring Events at the moment
|
2021-09-22 18:36:13 +00:00
|
|
|
if (bookingToDelete && bookingToDelete.paid) {
|
|
|
|
const evt: CalendarEvent = {
|
2022-01-21 12:06:45 +00:00
|
|
|
type: bookingToDelete?.eventType?.title as string,
|
2021-09-22 18:36:13 +00:00
|
|
|
title: bookingToDelete.title,
|
|
|
|
description: bookingToDelete.description ?? "",
|
2022-05-18 21:05:49 +00:00
|
|
|
customInputs: isPrismaObjOrUndefined(bookingToDelete.customInputs),
|
2021-09-22 18:36:13 +00:00
|
|
|
startTime: bookingToDelete.startTime.toISOString(),
|
|
|
|
endTime: bookingToDelete.endTime.toISOString(),
|
|
|
|
organizer: {
|
|
|
|
email: bookingToDelete.user?.email ?? "dev@calendso.com",
|
|
|
|
name: bookingToDelete.user?.name ?? "no user",
|
|
|
|
timeZone: bookingToDelete.user?.timeZone ?? "",
|
2022-01-27 20:32:53 +00:00
|
|
|
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
2022-01-27 20:32:53 +00:00
|
|
|
attendees: attendeesList,
|
2021-09-22 18:36:13 +00:00
|
|
|
location: bookingToDelete.location ?? "",
|
2021-10-26 16:17:24 +00:00
|
|
|
uid: bookingToDelete.uid ?? "",
|
2022-01-21 21:35:31 +00:00
|
|
|
destinationCalendar: bookingToDelete?.destinationCalendar || bookingToDelete?.user.destinationCalendar,
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
|
|
|
await refund(bookingToDelete, evt);
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: bookingToDelete.id,
|
|
|
|
},
|
|
|
|
data: {
|
2022-06-06 16:54:47 +00:00
|
|
|
status: BookingStatus.REJECTED,
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// We skip the deletion of the event, because that would also delete the payment reference, which we should keep
|
|
|
|
await apiDeletes;
|
|
|
|
return res.status(200).json({ message: "Booking successfully deleted." });
|
|
|
|
}
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const attendeeDeletes = prisma.attendee.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: bookingToDelete.id,
|
|
|
|
},
|
|
|
|
});
|
2021-09-13 08:57:56 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const bookingReferenceDeletes = prisma.bookingReference.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: bookingToDelete.id,
|
|
|
|
},
|
|
|
|
});
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
//Workflows - delete all reminders for that booking
|
|
|
|
const remindersToDelete: PrismaPromise<Prisma.BatchPayload>[] = [];
|
|
|
|
bookingToDelete.workflowReminders.forEach((reminder) => {
|
|
|
|
if (reminder.scheduled && reminder.referenceId) {
|
|
|
|
if (reminder.method === WorkflowMethods.EMAIL) {
|
|
|
|
deleteScheduledEmailReminder(reminder.referenceId);
|
|
|
|
} else if (reminder.method === WorkflowMethods.SMS) {
|
|
|
|
deleteScheduledSMSReminder(reminder.referenceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const reminderToDelete = prisma.workflowReminder.deleteMany({
|
|
|
|
where: {
|
|
|
|
id: reminder.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
remindersToDelete.push(reminderToDelete);
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([apiDeletes, attendeeDeletes, bookingReferenceDeletes].concat(remindersToDelete));
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
await sendCancelledEmails(evt);
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
//Workflows - schedule reminders
|
|
|
|
if (bookingToDelete.eventType?.workflows) {
|
|
|
|
await sendCancelledReminders(
|
|
|
|
bookingToDelete.eventType?.workflows,
|
|
|
|
bookingToDelete.smsReminderNumber,
|
|
|
|
evt
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
res.status(204).end();
|
2021-07-15 21:34:55 +00:00
|
|
|
}
|