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';
|
|
|
|
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-16 21:40:13 +00:00
|
|
|
import {createMeeting, updateMeeting} from "../../../lib/videoClient";
|
2021-06-16 23:04:08 +00:00
|
|
|
import EventAttendeeMail from "../../../lib/emails/EventAttendeeMail";
|
2021-06-15 15:26:16 +00:00
|
|
|
import {getEventName} from "../../../lib/event";
|
2021-06-21 23:15:29 +00:00
|
|
|
import { LocationType } from '../../../lib/location';
|
|
|
|
import merge from "lodash.merge"
|
2021-06-09 22:51:09 +00:00
|
|
|
const translator = short();
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2021-06-21 23:15:29 +00:00
|
|
|
interface p {
|
|
|
|
location: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLocationRequestFromIntegration = ({location}: p) => {
|
|
|
|
if (location === LocationType.GoogleMeet.valueOf()) {
|
|
|
|
const requestId = uuidv5(location, uuidv5.URL)
|
|
|
|
|
|
|
|
return {
|
|
|
|
conferenceData: {
|
|
|
|
createRequest: {
|
|
|
|
requestId: requestId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2021-06-15 15:26:16 +00:00
|
|
|
const selectedEventType = await prisma.eventType.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: currentUser.id,
|
|
|
|
id: req.body.eventTypeId
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
eventName: true,
|
|
|
|
title: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-21 23:15:29 +00:00
|
|
|
let rawLocation = req.body.location
|
|
|
|
|
|
|
|
let evt: CalendarEvent = {
|
2021-06-15 15:26:16 +00:00
|
|
|
type: selectedEventType.title,
|
|
|
|
title: getEventName(req.body.name, selectedEventType.title, selectedEventType.eventName),
|
2021-06-09 19:46:41 +00:00
|
|
|
description: req.body.notes,
|
|
|
|
startTime: req.body.start,
|
|
|
|
endTime: req.body.end,
|
|
|
|
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-21 23:15:29 +00:00
|
|
|
// If phone or inPerson use raw location
|
|
|
|
// set evt.location to req.body.location
|
2021-06-24 15:49:15 +00:00
|
|
|
if (!rawLocation?.includes('integration')) {
|
2021-06-21 23:15:29 +00:00
|
|
|
evt.location = rawLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If location is set to an integration location
|
|
|
|
// Build proper transforms for evt object
|
|
|
|
// Extend evt object with those transformations
|
2021-06-24 15:49:15 +00:00
|
|
|
if (rawLocation?.includes('integration')) {
|
2021-06-21 23:15:29 +00:00
|
|
|
let maybeLocationRequestObject = getLocationRequestFromIntegration({
|
|
|
|
location: rawLocation
|
|
|
|
})
|
|
|
|
|
|
|
|
evt = merge(evt, maybeLocationRequestObject)
|
|
|
|
}
|
|
|
|
|
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-14 16:47:05 +00:00
|
|
|
let referencesToCreate = [];
|
2021-06-09 19:46:41 +00:00
|
|
|
|
|
|
|
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-14 16:55:20 +00:00
|
|
|
results = results.concat(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-21 15:56:14 +00:00
|
|
|
return updateEvent(credential, bookingRefUid, evt)
|
2021-06-20 18:32:30 +00:00
|
|
|
.then(response => ({type: credential.type, success: true, response}))
|
|
|
|
.catch(e => {
|
2021-06-21 16:15:05 +00:00
|
|
|
console.error("updateEvent failed", e)
|
2021-06-20 18:32:30 +00:00
|
|
|
return {type: credential.type, success: false}
|
|
|
|
});
|
2021-06-14 16:55:20 +00:00
|
|
|
}));
|
2021-06-05 23:32:24 +00:00
|
|
|
|
2021-06-14 16:55:20 +00:00
|
|
|
results = results.concat(await async.mapLimit(videoCredentials, 5, async (credential) => {
|
|
|
|
const bookingRefUid = booking.references.filter((ref) => ref.type === credential.type)[0].uid;
|
2021-06-21 15:56:14 +00:00
|
|
|
return updateMeeting(credential, bookingRefUid, evt)
|
|
|
|
.then(response => ({type: credential.type, success: true, response}))
|
|
|
|
.catch(e => {
|
2021-06-21 16:15:05 +00:00
|
|
|
console.error("updateMeeting failed", e)
|
2021-06-21 15:56:14 +00:00
|
|
|
return {type: credential.type, success: false}
|
|
|
|
});
|
2021-06-14 16:55:20 +00:00
|
|
|
}));
|
2021-06-05 23:32:24 +00:00
|
|
|
|
2021-06-21 16:15:05 +00:00
|
|
|
if (results.length > 0 && results.every(res => !res.success)) {
|
2021-06-20 18:32:30 +00:00
|
|
|
res.status(500).json({message: "Rescheduling failed"});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-14 16:47:05 +00:00
|
|
|
results = results.concat(await async.mapLimit(calendarCredentials, 5, async (credential) => {
|
2021-06-21 15:56:14 +00:00
|
|
|
return createEvent(credential, evt)
|
2021-06-20 18:32:30 +00:00
|
|
|
.then(response => ({type: credential.type, success: true, response}))
|
|
|
|
.catch(e => {
|
|
|
|
console.error("createEvent failed", e)
|
|
|
|
return {type: credential.type, success: false}
|
|
|
|
});
|
2021-06-13 13:57:01 +00:00
|
|
|
}));
|
|
|
|
|
2021-06-14 16:47:05 +00:00
|
|
|
results = results.concat(await async.mapLimit(videoCredentials, 5, async (credential) => {
|
2021-06-21 15:56:14 +00:00
|
|
|
return createMeeting(credential, evt)
|
|
|
|
.then(response => ({type: credential.type, success: true, response}))
|
|
|
|
.catch(e => {
|
2021-06-21 16:15:05 +00:00
|
|
|
console.error("createMeeting failed", e)
|
2021-06-21 15:56:14 +00:00
|
|
|
return {type: credential.type, success: false}
|
|
|
|
});
|
2021-06-13 13:57:01 +00:00
|
|
|
}));
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-06-21 16:15:05 +00:00
|
|
|
if (results.length > 0 && results.every(res => !res.success)) {
|
2021-06-20 18:32:30 +00:00
|
|
|
res.status(500).json({message: "Booking failed"});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-09 19:46:41 +00:00
|
|
|
referencesToCreate = results.map((result => {
|
|
|
|
return {
|
|
|
|
type: result.type,
|
2021-06-16 21:40:13 +00:00
|
|
|
uid: result.response.createdEvent.id.toString()
|
2021-06-09 19:46:41 +00:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-06-16 22:56:02 +00:00
|
|
|
const hashUID = results.length > 0 ? results[0].response.uid : translator.fromUUID(uuidv5(JSON.stringify(evt), uuidv5.URL));
|
2021-06-21 16:15:05 +00:00
|
|
|
// TODO Should just be set to the true case as soon as we have a "bare email" integration class.
|
|
|
|
// UID generation should happen in the integration itself, not here.
|
|
|
|
if(results.length === 0) {
|
|
|
|
// Legacy as well, as soon as we have a separate email integration class. Just used
|
|
|
|
// to send an email even if there is no integration at all.
|
|
|
|
try {
|
2021-06-21 15:56:14 +00:00
|
|
|
const mail = new EventAttendeeMail(evt, hashUID);
|
|
|
|
await mail.sendEmail();
|
2021-06-21 16:15:05 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error("Sending legacy event mail failed", e)
|
|
|
|
res.status(500).json({message: "Booking failed"});
|
|
|
|
return;
|
2021-06-21 15:56:14 +00:00
|
|
|
}
|
2021-06-16 23:04:08 +00:00
|
|
|
}
|
2021-06-16 22:56:02 +00:00
|
|
|
|
2021-06-20 18:32:30 +00:00
|
|
|
let booking;
|
|
|
|
try {
|
|
|
|
booking = await prisma.booking.create({
|
2021-06-09 19:46:41 +00:00
|
|
|
data: {
|
|
|
|
uid: hashUID,
|
|
|
|
userId: currentUser.id,
|
|
|
|
references: {
|
|
|
|
create: referencesToCreate
|
|
|
|
},
|
|
|
|
eventTypeId: eventType.id,
|
|
|
|
|
2021-06-20 18:32:30 +00:00
|
|
|
title: evt.title,
|
|
|
|
description: evt.description,
|
|
|
|
startTime: evt.startTime,
|
|
|
|
endTime: evt.endTime,
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-06-20 18:32:30 +00:00
|
|
|
attendees: {
|
|
|
|
create: evt.attendees
|
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
}
|
2021-06-20 18:32:30 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error when saving booking to db", e);
|
|
|
|
res.status(500).json({message: "Booking already exists"});
|
|
|
|
return;
|
|
|
|
}
|
2021-06-09 19:46:41 +00:00
|
|
|
|
2021-06-21 15:56:14 +00:00
|
|
|
res.status(204).json({});
|
2021-04-16 02:09:22 +00:00
|
|
|
}
|