Refactors video integrations (#1037)

* Fixes error types

* Type fixes

* Refactors video meeting handling

* More type fixes

* Type fixes

* More fixes

* Makes language non optional

* Adds missing translations

* Apply suggestions from code review

Co-authored-by: Alex Johansson <[email protected]>

* Feedback

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Alex Johansson <[email protected]>
This commit is contained in:
authored and GitHub committed 2021-10-26 10:17:24 -06:00
1 parent eabb096e14
commit e38086b8fe
17 files changed
+618 -612

No files matched your search

+7 -4
View File
@@ -7,6 +7,7 @@ import { CalendarEvent } from "@lib/calendarClient";
import EventRejectionMail from "@lib/emails/EventRejectionMail";
import EventManager from "@lib/events/EventManager";
import prisma from "@lib/prisma";
import { BookingConfirmBody } from "@lib/types/booking";
import { getTranslation } from "@server/lib/i18n";
@@ -18,7 +19,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(401).json({ message: "Not authenticated" });
}
const bookingId = req.body.id;
const reqBody = req.body as BookingConfirmBody;
const bookingId = reqBody.id;
if (!bookingId) {
return res.status(400).json({ message: "bookingId missing" });
}
@@ -60,7 +63,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
});
if (!booking || booking.userId != currentUser.id) {
if (!booking || booking.userId !== currentUser.id) {
return res.status(404).json({ message: "booking not found" });
}
if (booking.confirmed) {
@@ -75,12 +78,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
endTime: booking.endTime.toISOString(),
organizer: { email: currentUser.email, name: currentUser.name!, timeZone: currentUser.timeZone },
attendees: booking.attendees,
location: booking.location,
location: booking.location ?? "",
uid: booking.uid,
language: t,
};
if (req.body.confirmed) {
if (reqBody.confirmed) {
const eventManager = new EventManager(currentUser.credentials);
const scheduleResult = await eventManager.create(evt);
+6 -43
View File
@@ -15,7 +15,7 @@ import { CalendarEvent, getBusyCalendarTimes } from "@lib/calendarClient";
import EventOrganizerRequestMail from "@lib/emails/EventOrganizerRequestMail";
import { getErrorFromUnknown } from "@lib/errors";
import { getEventName } from "@lib/event";
import EventManager, { CreateUpdateResult, EventResult, PartialReference } from "@lib/events/EventManager";
import EventManager, { EventResult, PartialReference } from "@lib/events/EventManager";
import logger from "@lib/logger";
import prisma from "@lib/prisma";
import { BookingCreateBody } from "@lib/types/booking";
@@ -329,6 +329,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
let booking: Booking | null = null;
try {
booking = await createBooking();
evt.uid = booking.uid;
} catch (_err) {
const err = getErrorFromUnknown(_err);
log.error(`Booking ${eventTypeId} failed`, "Error when saving booking to db", err.message);
@@ -431,7 +432,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (rescheduleUid) {
// Use EventManager to conditionally use all needed integrations.
const eventManagerCalendarEvent = { ...evt, uid: rescheduleUid };
const updateResults: CreateUpdateResult = await eventManager.update(eventManagerCalendarEvent);
const updateResults = await eventManager.update(eventManagerCalendarEvent);
results = updateResults.results;
referencesToCreate = updateResults.referencesToCreate;
@@ -444,9 +445,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
log.error(`Booking ${user.name} failed`, error, results);
}
// If it's not a reschedule, doesn't require confirmation and there's no price,
// Create a booking
} else if (!eventType.requiresConfirmation && !eventType.price) {
// Use EventManager to conditionally use all needed integrations.
const createResults: CreateUpdateResult = await eventManager.create(evt);
const createResults = await eventManager.create(evt);
results = createResults.results;
referencesToCreate = createResults.referencesToCreate;
@@ -461,46 +464,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
//for Daily.co video calls will grab the meeting token for the call
const isDaily = evt.location === "integrations:daily";
let dailyEvent: DailyReturnType;
if (!rescheduleUid) {
dailyEvent = results.filter((ref) => ref.type === "daily")[0]?.createdEvent as DailyReturnType;
} else {
dailyEvent = results.filter((ref) => ref.type === "daily_video")[0]?.updatedEvent as DailyReturnType;
}
let meetingToken;
if (isDaily) {
const response = await fetch("https://api.daily.co/v1/meeting-tokens", {
method: "POST",
body: JSON.stringify({ properties: { room_name: dailyEvent.name, is_owner: true } }),
headers: {
Authorization: "Bearer " + process.env.DAILY_API_KEY,
"Content-Type": "application/json",
},
});
meetingToken = await response.json();
}
//for Daily.co video calls will update the dailyEventReference table
if (isDaily) {
await prisma.dailyEventReference.create({
data: {
dailyurl: dailyEvent.url,
dailytoken: meetingToken.token,
booking: {
connect: {
uid: booking.uid,
},
},
},
});
}
if (eventType.requiresConfirmation && !rescheduleUid) {
await new EventOrganizerRequestMail({ ...evt, uid }).sendEmail();
}