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-09 19:46:41 +00:00
|
|
|
import {CalendarEvent, createEvent, updateEvent} from '../../../lib/calendarClient';
|
2021-05-27 22:10:20 +00:00
|
|
|
import createConfirmBookedEmail from "../../../lib/emails/confirm-booked";
|
2021-06-09 19:46:41 +00:00
|
|
|
import async from 'async';
|
2021-06-09 22:51:09 +00:00
|
|
|
import {v5 as uuidv5} from 'uuid';
|
|
|
|
import short from 'short-uuid';
|
2021-06-13 13:57:01 +00:00
|
|
|
import {createMeeting, VideoMeeting} from "../../../lib/videoClient";
|
2021-06-09 22:51:09 +00:00
|
|
|
|
|
|
|
const translator = short();
|
2021-03-22 13:48:48 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-06-09 19:46:41 +00:00
|
|
|
const {user} = req.query;
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
const currentUser = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
username: user,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
credentials: true,
|
|
|
|
timeZone: true,
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-13 13:57:01 +00:00
|
|
|
// Split credentials up into calendar credentials and video credentials
|
|
|
|
const calendarCredentials = currentUser.credentials.filter(cred => cred.type.endsWith('_calendar'));
|
|
|
|
const videoCredentials = currentUser.credentials.filter(cred => cred.type.endsWith('_video'));
|
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
const rescheduleUid = req.body.rescheduleUid;
|
|
|
|
|
|
|
|
const evt: CalendarEvent = {
|
|
|
|
type: req.body.eventName,
|
|
|
|
title: req.body.eventName + ' with ' + req.body.name,
|
|
|
|
description: req.body.notes,
|
|
|
|
startTime: req.body.start,
|
|
|
|
endTime: req.body.end,
|
|
|
|
location: req.body.location,
|
|
|
|
organizer: {email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone},
|
|
|
|
attendees: [
|
|
|
|
{email: req.body.email, name: req.body.name, timeZone: req.body.timeZone}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2021-06-13 13:57:01 +00:00
|
|
|
//TODO Only create meeting if integration exists.
|
|
|
|
const meeting: VideoMeeting = {
|
|
|
|
attendees: [
|
|
|
|
{email: req.body.email, name: req.body.name, timeZone: req.body.timeZone}
|
|
|
|
],
|
|
|
|
endTime: req.body.end,
|
|
|
|
organizer: {email: currentUser.email, name: currentUser.name, timeZone: currentUser.timeZone},
|
|
|
|
startTime: req.body.start,
|
|
|
|
timezone: currentUser.timeZone,
|
|
|
|
title: req.body.eventName + ' with ' + req.body.name,
|
|
|
|
};
|
|
|
|
|
2021-06-12 22:37:35 +00:00
|
|
|
const hashUID: string = translator.fromUUID(uuidv5(JSON.stringify(evt), uuidv5.URL));
|
|
|
|
const cancelLink: string = process.env.BASE_URL + '/cancel/' + hashUID;
|
|
|
|
const rescheduleLink:string = process.env.BASE_URL + '/reschedule/' + hashUID;
|
|
|
|
const appendLinksToEvents = (event: CalendarEvent) => {
|
|
|
|
const eventCopy = {...event};
|
|
|
|
eventCopy.description += "\n\n"
|
|
|
|
+ "Need to change this event?\n"
|
|
|
|
+ "Cancel: " + cancelLink + "\n"
|
|
|
|
+ "Reschedule:" + rescheduleLink;
|
|
|
|
|
|
|
|
return eventCopy;
|
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
|
|
|
|
const eventType = await prisma.eventType.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: currentUser.id,
|
|
|
|
title: evt.type
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-13 13:57:01 +00:00
|
|
|
let results = [];
|
2021-06-09 19:46:41 +00:00
|
|
|
let referencesToCreate = undefined;
|
|
|
|
|
|
|
|
if (rescheduleUid) {
|
|
|
|
// Reschedule event
|
|
|
|
const booking = await prisma.booking.findFirst({
|
|
|
|
where: {
|
|
|
|
uid: rescheduleUid
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
references: {
|
|
|
|
select: {
|
2021-06-05 23:32:24 +00:00
|
|
|
id: true,
|
2021-06-09 19:46:41 +00:00
|
|
|
type: true,
|
|
|
|
uid: true
|
|
|
|
}
|
2021-03-22 13:48:48 +00:00
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
}
|
2021-03-22 13:48:48 +00:00
|
|
|
});
|
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
// Use all integrations
|
2021-06-13 13:57:01 +00:00
|
|
|
results = await async.mapLimit(calendarCredentials, 5, async (credential) => {
|
2021-06-09 19:46:41 +00:00
|
|
|
const bookingRefUid = booking.references.filter((ref) => ref.type === credential.type)[0].uid;
|
2021-06-12 22:37:35 +00:00
|
|
|
return await updateEvent(credential, bookingRefUid, appendLinksToEvents(evt))
|
2021-06-05 23:32:24 +00:00
|
|
|
});
|
|
|
|
|
2021-06-13 13:57:01 +00:00
|
|
|
//TODO: Reschedule with videoCredentials as well
|
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
// Clone elements
|
|
|
|
referencesToCreate = [...booking.references];
|
2021-05-27 22:10:20 +00:00
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
// Now we can delete the old booking and its references.
|
|
|
|
let bookingReferenceDeletes = prisma.bookingReference.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: booking.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let attendeeDeletes = prisma.attendee.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: booking.id
|
|
|
|
}
|
2021-06-05 23:32:24 +00:00
|
|
|
});
|
2021-06-09 19:46:41 +00:00
|
|
|
let bookingDeletes = prisma.booking.delete({
|
|
|
|
where: {
|
|
|
|
uid: rescheduleUid
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
bookingReferenceDeletes,
|
|
|
|
attendeeDeletes,
|
|
|
|
bookingDeletes
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// Schedule event
|
2021-06-13 13:57:01 +00:00
|
|
|
results.concat(await async.mapLimit(calendarCredentials, 5, async (credential) => {
|
2021-06-12 22:37:35 +00:00
|
|
|
const response = await createEvent(credential, appendLinksToEvents(evt));
|
2021-06-09 19:46:41 +00:00
|
|
|
return {
|
|
|
|
type: credential.type,
|
|
|
|
response
|
|
|
|
};
|
2021-06-13 13:57:01 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
results.concat(await async.mapLimit(videoCredentials, 5, async (credential) => {
|
|
|
|
const response = await createMeeting(credential, meeting);
|
|
|
|
return {
|
|
|
|
type: credential.type,
|
|
|
|
response
|
|
|
|
};
|
|
|
|
}));
|
2021-06-09 19:46:41 +00:00
|
|
|
|
|
|
|
referencesToCreate = results.map((result => {
|
|
|
|
return {
|
|
|
|
type: result.type,
|
|
|
|
uid: result.response.id
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
await prisma.booking.create({
|
|
|
|
data: {
|
|
|
|
uid: hashUID,
|
|
|
|
userId: currentUser.id,
|
|
|
|
references: {
|
|
|
|
create: referencesToCreate
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
});
|
|
|
|
|
2021-06-10 21:18:57 +00:00
|
|
|
// If one of the integrations allows email confirmations or no integrations are added, send it.
|
|
|
|
if (currentUser.credentials.length === 0 || !results.every((result) => result.disableConfirmationEmail)) {
|
2021-06-09 19:46:41 +00:00
|
|
|
await createConfirmBookedEmail(
|
2021-06-12 22:37:35 +00:00
|
|
|
evt, cancelLink, rescheduleLink
|
2021-06-09 19:46:41 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-27 22:10:20 +00:00
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
res.status(200).json(results);
|
2021-04-16 02:09:22 +00:00
|
|
|
}
|