2021-07-17 12:30:29 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
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";
|
2021-07-17 12:30:29 +00:00
|
|
|
import { CalendarEvent } from "@lib/calendarClient";
|
|
|
|
import EventRejectionMail from "@lib/emails/EventRejectionMail";
|
2021-07-24 20:24:00 +00:00
|
|
|
import EventManager from "@lib/events/EventManager";
|
2021-10-25 13:05:21 +00:00
|
|
|
import prisma from "@lib/prisma";
|
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-07-18 19:22:39 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
|
2021-10-25 13:05:21 +00:00
|
|
|
const t = await getTranslation(req.body.language ?? "en", "common");
|
|
|
|
|
2021-07-17 12:30:29 +00:00
|
|
|
const session = await getSession({ req: req });
|
2021-10-25 13:05:21 +00:00
|
|
|
if (!session?.user?.id) {
|
2021-07-17 12:30:29 +00:00
|
|
|
return res.status(401).json({ message: "Not authenticated" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const bookingId = req.body.id;
|
|
|
|
if (!bookingId) {
|
|
|
|
return res.status(400).json({ message: "bookingId missing" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
credentials: true,
|
|
|
|
timeZone: true,
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-25 13:05:21 +00:00
|
|
|
if (!currentUser) {
|
|
|
|
return res.status(404).json({ message: "User not found" });
|
|
|
|
}
|
|
|
|
|
2021-07-17 12:30:29 +00:00
|
|
|
if (req.method == "PATCH") {
|
|
|
|
const booking = await prisma.booking.findFirst({
|
|
|
|
where: {
|
|
|
|
id: bookingId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
title: true,
|
|
|
|
description: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
confirmed: true,
|
|
|
|
attendees: true,
|
2021-07-25 12:37:22 +00:00
|
|
|
location: true,
|
2021-07-17 12:30:29 +00:00
|
|
|
userId: true,
|
|
|
|
id: true,
|
|
|
|
uid: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
payment: true,
|
2021-07-17 12:30:29 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!booking || booking.userId != currentUser.id) {
|
|
|
|
return res.status(404).json({ message: "booking not found" });
|
|
|
|
}
|
|
|
|
if (booking.confirmed) {
|
|
|
|
return res.status(400).json({ message: "booking already confirmed" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const evt: CalendarEvent = {
|
|
|
|
type: booking.title,
|
|
|
|
title: booking.title,
|
|
|
|
description: booking.description,
|
|
|
|
startTime: booking.startTime.toISOString(),
|
|
|
|
endTime: booking.endTime.toISOString(),
|
2021-10-25 13:05:21 +00:00
|
|
|
organizer: { email: currentUser.email, name: currentUser.name!, timeZone: currentUser.timeZone },
|
2021-07-17 12:30:29 +00:00
|
|
|
attendees: booking.attendees,
|
2021-07-25 14:29:06 +00:00
|
|
|
location: booking.location,
|
2021-10-25 13:05:21 +00:00
|
|
|
uid: booking.uid,
|
|
|
|
language: t,
|
2021-07-17 12:30:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (req.body.confirmed) {
|
2021-07-24 20:24:00 +00:00
|
|
|
const eventManager = new EventManager(currentUser.credentials);
|
2021-10-25 13:05:21 +00:00
|
|
|
const scheduleResult = await eventManager.create(evt);
|
2021-07-17 12:30:29 +00:00
|
|
|
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: bookingId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
confirmed: true,
|
|
|
|
references: {
|
|
|
|
create: scheduleResult.referencesToCreate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
res.status(204).json({ message: "ok" });
|
|
|
|
} else {
|
2021-09-22 18:36:13 +00:00
|
|
|
await refund(booking, evt);
|
|
|
|
|
2021-07-17 12:30:29 +00:00
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: bookingId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
rejected: true,
|
|
|
|
},
|
|
|
|
});
|
2021-10-25 13:05:21 +00:00
|
|
|
const attendeeMail = new EventRejectionMail(evt);
|
2021-07-17 12:30:29 +00:00
|
|
|
await attendeeMail.sendEmail();
|
2021-10-25 13:05:21 +00:00
|
|
|
|
2021-07-17 12:30:29 +00:00
|
|
|
res.status(204).json({ message: "ok" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|