cal.pub0.org/pages/api/book/confirm.ts

109 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-07-17 12:30:29 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "next-auth/client";
import prisma from "../../../lib/prisma";
import { handleLegacyConfirmationMail } from "./[user]";
2021-07-17 12:30:29 +00:00
import { CalendarEvent } from "@lib/calendarClient";
import EventRejectionMail from "@lib/emails/EventRejectionMail";
import EventManager from "@lib/events/EventManager";
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,
},
});
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) {
const eventManager = new EventManager(currentUser.credentials);
const scheduleResult = await eventManager.create(evt);
2021-07-17 12:30:29 +00:00
await handleLegacyConfirmationMail(
scheduleResult.results,
{ requiresConfirmation: false },
evt,
booking.uid
);
await prisma.booking.update({
where: {
id: bookingId,
},
data: {
confirmed: true,
references: {
create: scheduleResult.referencesToCreate,
},
},
});
res.status(204).json({ message: "ok" });
} else {
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" });
}
}
}