2022-05-18 21:05:49 +00:00
|
|
|
|
import { Booking, BookingStatus, Prisma, SchedulingType, User } from "@prisma/client";
|
2022-06-06 16:54:47 +00:00
|
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
|
import { z } from "zod";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
|
import EventManager from "@calcom/core/EventManager";
|
2022-06-06 17:49:56 +00:00
|
|
|
|
import { sendDeclinedEmails, sendScheduledEmails } from "@calcom/emails";
|
2022-06-10 00:32:34 +00:00
|
|
|
|
import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
|
2022-03-23 22:00:30 +00:00
|
|
|
|
import logger from "@calcom/lib/logger";
|
2022-06-10 18:38:46 +00:00
|
|
|
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
2022-06-06 16:54:47 +00:00
|
|
|
|
import prisma from "@calcom/prisma";
|
2022-06-10 00:32:34 +00:00
|
|
|
|
import type { AdditionalInformation, CalendarEvent } from "@calcom/types/Calendar";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
import { refund } from "@ee/lib/stripe/server";
|
|
|
|
|
|
2021-09-03 20:51:21 +00:00
|
|
|
|
import { getSession } from "@lib/auth";
|
2022-06-06 16:54:47 +00:00
|
|
|
|
import { HttpError } from "@lib/core/http/error";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
|
import { getTranslation } from "@server/lib/i18n";
|
2021-07-17 12:30:29 +00:00
|
|
|
|
|
2021-10-29 00:50:52 +00:00
|
|
|
|
const authorized = async (
|
|
|
|
|
currentUser: Pick<User, "id">,
|
|
|
|
|
booking: Pick<Booking, "eventTypeId" | "userId">
|
|
|
|
|
) => {
|
|
|
|
|
// if the organizer
|
|
|
|
|
if (booking.userId === currentUser.id) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const eventType = await prisma.eventType.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: booking.eventTypeId || undefined,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
schedulingType: true,
|
|
|
|
|
users: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (
|
|
|
|
|
eventType?.schedulingType === SchedulingType.COLLECTIVE &&
|
|
|
|
|
eventType.users.find((user) => user.id === currentUser.id)
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-26 11:03:43 +00:00
|
|
|
|
const log = logger.getChildLogger({ prefix: ["[api] book:user"] });
|
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const bookingConfirmPatchBodySchema = z.object({
|
|
|
|
|
confirmed: z.boolean(),
|
|
|
|
|
id: z.number(),
|
|
|
|
|
recurringEventId: z.string().optional(),
|
|
|
|
|
reason: z.string().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function patchHandler(req: NextApiRequest) {
|
|
|
|
|
const session = await getSession({ req });
|
2021-10-25 13:05:21 +00:00
|
|
|
|
if (!session?.user?.id) {
|
2022-06-06 16:54:47 +00:00
|
|
|
|
throw new HttpError({ statusCode: 401, message: "Not authenticated" });
|
2021-07-17 12:30:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const {
|
|
|
|
|
id: bookingId,
|
|
|
|
|
recurringEventId,
|
|
|
|
|
reason: rejectionReason,
|
|
|
|
|
confirmed,
|
|
|
|
|
} = bookingConfirmPatchBodySchema.parse(req.body);
|
2021-07-17 12:30:29 +00:00
|
|
|
|
|
|
|
|
|
const currentUser = await prisma.user.findFirst({
|
2022-06-06 16:54:47 +00:00
|
|
|
|
rejectOnNotFound() {
|
|
|
|
|
throw new HttpError({ statusCode: 404, message: "User not found" });
|
|
|
|
|
},
|
2021-07-17 12:30:29 +00:00
|
|
|
|
where: {
|
|
|
|
|
id: session.user.id,
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
2021-12-06 13:25:22 +00:00
|
|
|
|
credentials: {
|
|
|
|
|
orderBy: { id: "desc" as Prisma.SortOrder },
|
|
|
|
|
},
|
2021-07-17 12:30:29 +00:00
|
|
|
|
timeZone: true,
|
|
|
|
|
email: true,
|
|
|
|
|
name: true,
|
2021-11-26 11:03:43 +00:00
|
|
|
|
username: true,
|
2021-12-09 15:51:37 +00:00
|
|
|
|
destinationCalendar: true,
|
2022-01-27 20:32:53 +00:00
|
|
|
|
locale: true,
|
2021-07-17 12:30:29 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const tOrganizer = await getTranslation(currentUser.locale ?? "en", "common");
|
|
|
|
|
|
|
|
|
|
const booking = await prisma.booking.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: bookingId,
|
|
|
|
|
},
|
|
|
|
|
rejectOnNotFound() {
|
|
|
|
|
throw new HttpError({ statusCode: 404, message: "Booking not found" });
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
title: true,
|
|
|
|
|
description: true,
|
|
|
|
|
customInputs: true,
|
|
|
|
|
startTime: true,
|
|
|
|
|
endTime: true,
|
|
|
|
|
attendees: true,
|
|
|
|
|
eventTypeId: true,
|
|
|
|
|
eventType: {
|
|
|
|
|
select: {
|
|
|
|
|
recurringEvent: true,
|
2022-06-17 09:02:29 +00:00
|
|
|
|
requiresConfirmation: true,
|
2022-06-06 16:54:47 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
location: true,
|
|
|
|
|
userId: true,
|
|
|
|
|
id: true,
|
|
|
|
|
uid: true,
|
|
|
|
|
payment: true,
|
|
|
|
|
destinationCalendar: true,
|
|
|
|
|
paid: true,
|
|
|
|
|
recurringEventId: true,
|
|
|
|
|
status: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!(await authorized(currentUser, booking))) {
|
|
|
|
|
throw new HttpError({ statusCode: 401, message: "UNAUTHORIZED" });
|
2021-10-25 13:05:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const isConfirmed = booking.status === BookingStatus.ACCEPTED;
|
|
|
|
|
if (isConfirmed) {
|
|
|
|
|
throw new HttpError({ statusCode: 400, message: "booking already confirmed" });
|
|
|
|
|
}
|
2022-01-27 20:32:53 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
/** When a booking that requires payment its being confirmed but doesn't have any payment,
|
|
|
|
|
* we shouldn’t save it on DestinationCalendars
|
|
|
|
|
*/
|
|
|
|
|
if (booking.payment.length > 0 && !booking.paid) {
|
|
|
|
|
await prisma.booking.update({
|
2021-07-17 12:30:29 +00:00
|
|
|
|
where: {
|
|
|
|
|
id: bookingId,
|
|
|
|
|
},
|
2022-06-06 16:54:47 +00:00
|
|
|
|
data: {
|
|
|
|
|
status: BookingStatus.ACCEPTED,
|
2021-07-17 12:30:29 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
req.statusCode = 204;
|
|
|
|
|
return { message: "Booking confirmed" };
|
|
|
|
|
}
|
2021-07-17 12:30:29 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const attendeesListPromises = booking.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",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-05-16 16:27:36 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const attendeesList = await Promise.all(attendeesListPromises);
|
2022-05-16 16:27:36 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const evt: CalendarEvent = {
|
|
|
|
|
type: booking.title,
|
|
|
|
|
title: booking.title,
|
|
|
|
|
description: booking.description,
|
|
|
|
|
customInputs: isPrismaObjOrUndefined(booking.customInputs),
|
|
|
|
|
startTime: booking.startTime.toISOString(),
|
|
|
|
|
endTime: booking.endTime.toISOString(),
|
|
|
|
|
organizer: {
|
|
|
|
|
email: currentUser.email,
|
|
|
|
|
name: currentUser.name || "Unnamed",
|
|
|
|
|
timeZone: currentUser.timeZone,
|
|
|
|
|
language: { translate: tOrganizer, locale: currentUser.locale ?? "en" },
|
|
|
|
|
},
|
|
|
|
|
attendees: attendeesList,
|
|
|
|
|
location: booking.location ?? "",
|
|
|
|
|
uid: booking.uid,
|
|
|
|
|
destinationCalendar: booking?.destinationCalendar || currentUser.destinationCalendar,
|
2022-06-17 09:02:29 +00:00
|
|
|
|
requiresConfirmation: booking?.eventType?.requiresConfirmation ?? false,
|
2022-06-06 16:54:47 +00:00
|
|
|
|
};
|
2022-01-27 20:32:53 +00:00
|
|
|
|
|
2022-06-10 00:32:34 +00:00
|
|
|
|
const recurringEvent = parseRecurringEvent(booking.eventType?.recurringEvent);
|
2022-06-06 16:54:47 +00:00
|
|
|
|
if (recurringEventId && recurringEvent) {
|
|
|
|
|
const groupedRecurringBookings = await prisma.booking.groupBy({
|
|
|
|
|
where: {
|
|
|
|
|
recurringEventId: booking.recurringEventId,
|
2021-10-29 00:50:52 +00:00
|
|
|
|
},
|
2022-06-06 16:54:47 +00:00
|
|
|
|
by: [Prisma.BookingScalarFieldEnum.recurringEventId],
|
|
|
|
|
_count: true,
|
|
|
|
|
});
|
|
|
|
|
// Overriding the recurring event configuration count to be the actual number of events booked for
|
|
|
|
|
// the recurring event (equal or less than recurring event configuration count)
|
|
|
|
|
recurringEvent.count = groupedRecurringBookings[0]._count;
|
2022-06-10 00:32:34 +00:00
|
|
|
|
// count changed, parsing again to get the new value in
|
|
|
|
|
evt.recurringEvent = parseRecurringEvent(recurringEvent);
|
2022-06-06 16:54:47 +00:00
|
|
|
|
}
|
2022-05-05 21:16:25 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
if (confirmed) {
|
|
|
|
|
const eventManager = new EventManager(currentUser);
|
|
|
|
|
const scheduleResult = await eventManager.create(evt);
|
2021-07-17 12:30:29 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
const results = scheduleResult.results;
|
2021-11-26 11:03:43 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
if (results.length > 0 && results.every((res) => !res.success)) {
|
|
|
|
|
const error = {
|
|
|
|
|
errorCode: "BookingCreatingMeetingFailed",
|
|
|
|
|
message: "Booking failed",
|
|
|
|
|
};
|
2021-11-26 11:03:43 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
log.error(`Booking ${currentUser.username} failed`, error, results);
|
|
|
|
|
} else {
|
2022-06-06 19:49:00 +00:00
|
|
|
|
const metadata: AdditionalInformation = {};
|
2021-11-26 11:03:43 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
if (results.length) {
|
|
|
|
|
// TODO: Handle created event metadata more elegantly
|
|
|
|
|
metadata.hangoutLink = results[0].createdEvent?.hangoutLink;
|
|
|
|
|
metadata.conferenceData = results[0].createdEvent?.conferenceData;
|
|
|
|
|
metadata.entryPoints = results[0].createdEvent?.entryPoints;
|
2021-11-26 11:03:43 +00:00
|
|
|
|
}
|
2022-06-06 16:54:47 +00:00
|
|
|
|
try {
|
2022-06-10 00:32:34 +00:00
|
|
|
|
await sendScheduledEmails({ ...evt, additionalInformation: metadata });
|
2022-06-06 16:54:47 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
log.error(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-26 11:03:43 +00:00
|
|
|
|
|
2022-06-06 16:54:47 +00:00
|
|
|
|
if (recurringEventId) {
|
2022-06-10 20:38:06 +00:00
|
|
|
|
// The booking to confirm is a recurring event and comes from /booking/recurring, proceeding to mark all related
|
2022-06-06 16:54:47 +00:00
|
|
|
|
// bookings as confirmed. Prisma updateMany does not support relations, so doing this in two steps for now.
|
|
|
|
|
const unconfirmedRecurringBookings = await prisma.booking.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
recurringEventId,
|
|
|
|
|
status: BookingStatus.PENDING,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
unconfirmedRecurringBookings.map(async (recurringBooking) => {
|
2022-05-05 21:16:25 +00:00
|
|
|
|
await prisma.booking.update({
|
|
|
|
|
where: {
|
2022-06-06 16:54:47 +00:00
|
|
|
|
id: recurringBooking.id,
|
2022-05-05 21:16:25 +00:00
|
|
|
|
},
|
|
|
|
|
data: {
|
2022-06-06 16:54:47 +00:00
|
|
|
|
status: BookingStatus.ACCEPTED,
|
2022-05-05 21:16:25 +00:00
|
|
|
|
references: {
|
|
|
|
|
create: scheduleResult.referencesToCreate,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-06-06 16:54:47 +00:00
|
|
|
|
});
|
2021-07-17 12:30:29 +00:00
|
|
|
|
} else {
|
2022-06-06 16:54:47 +00:00
|
|
|
|
// @NOTE: be careful with this as if any error occurs before this booking doesn't get confirmed
|
|
|
|
|
// Should perform update on booking (confirm) -> then trigger the rest handlers
|
|
|
|
|
await prisma.booking.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: bookingId,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
status: BookingStatus.ACCEPTED,
|
|
|
|
|
references: {
|
|
|
|
|
create: scheduleResult.referencesToCreate,
|
2022-05-05 21:16:25 +00:00
|
|
|
|
},
|
2022-06-06 16:54:47 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
evt.rejectionReason = rejectionReason;
|
|
|
|
|
if (recurringEventId) {
|
|
|
|
|
// The booking to reject is a recurring event and comes from /booking/upcoming, proceeding to mark all related
|
2022-06-10 00:32:34 +00:00
|
|
|
|
// bookings as rejected.
|
|
|
|
|
await prisma.booking.updateMany({
|
2022-06-06 16:54:47 +00:00
|
|
|
|
where: {
|
|
|
|
|
recurringEventId,
|
|
|
|
|
status: BookingStatus.PENDING,
|
|
|
|
|
},
|
2022-06-10 00:32:34 +00:00
|
|
|
|
data: {
|
|
|
|
|
status: BookingStatus.REJECTED,
|
|
|
|
|
rejectionReason,
|
|
|
|
|
},
|
2022-06-06 16:54:47 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
await refund(booking, evt); // No payment integration for recurring events for v1
|
|
|
|
|
await prisma.booking.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: bookingId,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
status: BookingStatus.REJECTED,
|
|
|
|
|
rejectionReason,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-07-17 12:30:29 +00:00
|
|
|
|
}
|
2022-06-06 16:54:47 +00:00
|
|
|
|
|
2022-06-10 00:32:34 +00:00
|
|
|
|
await sendDeclinedEmails(evt);
|
2021-07-17 12:30:29 +00:00
|
|
|
|
}
|
2022-06-06 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
req.statusCode = 204;
|
|
|
|
|
return { message: "Booking " + confirmed ? "confirmed" : "rejected" };
|
2021-07-17 12:30:29 +00:00
|
|
|
|
}
|
2022-06-06 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
export type BookConfirmPatchResponse = Awaited<ReturnType<typeof patchHandler>>;
|
|
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
|
// To prevent too much git diff until moved to another file
|
|
|
|
|
PATCH: Promise.resolve({ default: defaultResponder(patchHandler) }),
|
|
|
|
|
});
|