2021-07-17 12:30:29 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-03 20:51:21 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-07-17 12:30:29 +00:00
|
|
|
import prisma from "../../../lib/prisma";
|
|
|
|
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-09-22 18:36:13 +00:00
|
|
|
import { refund } from "@ee/lib/stripe/server";
|
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-07-17 12:30:29 +00:00
|
|
|
const session = await getSession({ req: req });
|
|
|
|
if (!session) {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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(),
|
|
|
|
organizer: { email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone },
|
|
|
|
attendees: booking.attendees,
|
2021-07-25 14:29:06 +00:00
|
|
|
location: booking.location,
|
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-07-25 15:08:11 +00:00
|
|
|
const scheduleResult = await eventManager.create(evt, booking.uid);
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const attendeeMail = new EventRejectionMail(evt, booking.uid);
|
|
|
|
await attendeeMail.sendEmail();
|
|
|
|
res.status(204).json({ message: "ok" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|