2021-06-05 23:32:24 +00:00
|
|
|
import type {NextApiRequest, NextApiResponse} from 'next';
|
2021-04-11 17:12:18 +00:00
|
|
|
import prisma from '../../../lib/prisma';
|
2021-06-05 23:32:24 +00:00
|
|
|
import {createEvent, CalendarEvent} from '../../../lib/calendarClient';
|
2021-05-27 22:10:20 +00:00
|
|
|
import createConfirmBookedEmail from "../../../lib/emails/confirm-booked";
|
2021-06-05 23:32:24 +00:00
|
|
|
import sha256 from "../../../lib/sha256";
|
2021-03-22 13:48:48 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-06-05 23:32:24 +00:00
|
|
|
const {user} = req.query;
|
2021-03-22 13:48:48 +00:00
|
|
|
|
|
|
|
const currentUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
2021-06-05 23:32:24 +00:00
|
|
|
username: user,
|
2021-03-22 13:48:48 +00:00
|
|
|
},
|
|
|
|
select: {
|
2021-06-05 23:32:24 +00:00
|
|
|
id: true,
|
2021-04-16 02:09:22 +00:00
|
|
|
credentials: true,
|
|
|
|
timeZone: true,
|
2021-05-27 22:10:20 +00:00
|
|
|
email: true,
|
|
|
|
name: true,
|
2021-03-22 13:48:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-21 22:10:48 +00:00
|
|
|
const evt: CalendarEvent = {
|
2021-05-27 22:10:20 +00:00
|
|
|
type: req.body.eventName,
|
|
|
|
title: req.body.eventName + ' with ' + req.body.name,
|
2021-04-21 22:10:48 +00:00
|
|
|
description: req.body.notes,
|
|
|
|
startTime: req.body.start,
|
|
|
|
endTime: req.body.end,
|
2021-05-08 19:03:47 +00:00
|
|
|
location: req.body.location,
|
2021-06-05 23:32:24 +00:00
|
|
|
organizer: {email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone},
|
2021-04-21 22:10:48 +00:00
|
|
|
attendees: [
|
2021-06-05 23:32:24 +00:00
|
|
|
{email: req.body.email, name: req.body.name, timeZone: req.body.timeZone}
|
2021-04-21 22:10:48 +00:00
|
|
|
]
|
|
|
|
};
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2021-06-05 23:32:24 +00:00
|
|
|
const eventType = await prisma.eventType.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: currentUser.id,
|
|
|
|
title: evt.type
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-21 22:10:48 +00:00
|
|
|
const result = await createEvent(currentUser.credentials[0], evt);
|
2021-05-27 22:10:20 +00:00
|
|
|
|
2021-06-05 23:32:24 +00:00
|
|
|
const hashUID = sha256(JSON.stringify(evt));
|
2021-06-07 07:18:41 +00:00
|
|
|
const referencesToCreate = currentUser.credentials.length == 0 ? [] : [
|
|
|
|
{
|
|
|
|
type: currentUser.credentials[0].type,
|
2021-06-07 07:37:28 +00:00
|
|
|
// Get the eventId. The Google API does not return it somewhere elso, so we need do it like this:
|
|
|
|
uid: result.htmlLink.replace("https://www.google.com/calendar/event?eid=", "")
|
2021-06-07 07:18:41 +00:00
|
|
|
}
|
|
|
|
];
|
2021-06-05 23:32:24 +00:00
|
|
|
|
|
|
|
await prisma.booking.create({
|
|
|
|
data: {
|
|
|
|
uid: hashUID,
|
|
|
|
userId: currentUser.id,
|
|
|
|
references: {
|
2021-06-07 07:18:41 +00:00
|
|
|
create: referencesToCreate
|
2021-06-05 23:32:24 +00:00
|
|
|
},
|
|
|
|
eventTypeId: eventType.id,
|
|
|
|
|
|
|
|
title: evt.title,
|
|
|
|
description: evt.description,
|
|
|
|
startTime: evt.startTime,
|
|
|
|
endTime: evt.endTime,
|
|
|
|
|
|
|
|
attendees: {
|
|
|
|
create: evt.attendees
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-01 19:16:06 +00:00
|
|
|
if (!result.disableConfirmationEmail) {
|
|
|
|
createConfirmBookedEmail(
|
2021-06-05 23:32:24 +00:00
|
|
|
evt
|
2021-06-01 19:16:06 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-27 22:10:20 +00:00
|
|
|
|
2021-04-21 22:10:48 +00:00
|
|
|
res.status(200).json(result);
|
2021-04-16 02:09:22 +00:00
|
|
|
}
|