2021-04-11 17:12:18 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
import prisma from '../../../lib/prisma';
|
2021-04-21 22:10:48 +00:00
|
|
|
import { createEvent, CalendarEvent } from '../../../lib/calendarClient';
|
2021-05-27 22:10:20 +00:00
|
|
|
import createConfirmBookedEmail from "../../../lib/emails/confirm-booked";
|
2021-03-22 13:48:48 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-04-11 17:12:18 +00:00
|
|
|
const { user } = req.query;
|
2021-03-22 13:48:48 +00:00
|
|
|
|
|
|
|
const currentUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
username: user,
|
|
|
|
},
|
|
|
|
select: {
|
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-05-27 22:10:20 +00:00
|
|
|
organizer: { email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone },
|
2021-04-21 22:10:48 +00:00
|
|
|
attendees: [
|
2021-05-27 22:10:20 +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-04-21 22:10:48 +00:00
|
|
|
// TODO: for now, first integration created; primary = obvious todo; ability to change primary.
|
|
|
|
const result = await createEvent(currentUser.credentials[0], evt);
|
2021-05-27 22:10:20 +00:00
|
|
|
|
|
|
|
createConfirmBookedEmail(
|
|
|
|
evt
|
|
|
|
);
|
|
|
|
|
2021-04-21 22:10:48 +00:00
|
|
|
res.status(200).json(result);
|
2021-04-16 02:09:22 +00:00
|
|
|
}
|