From 43f899582f8d81bd0ee2f64dd71bb0334a6d39fa Mon Sep 17 00:00:00 2001 From: Bailey Pumfleet Date: Tue, 6 Jul 2021 22:09:53 +0100 Subject: [PATCH] Fix issue where user ID is not fetched --- pages/api/availability/[user].ts | 88 ++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/pages/api/availability/[user].ts b/pages/api/availability/[user].ts index 3b07c2e49f..f0c6406850 100644 --- a/pages/api/availability/[user].ts +++ b/pages/api/availability/[user].ts @@ -1,49 +1,63 @@ -import type {NextApiRequest, NextApiResponse} from 'next'; -import prisma from '../../../lib/prisma'; -import {getBusyCalendarTimes} from '../../../lib/calendarClient'; -import {getBusyVideoTimes} from '../../../lib/videoClient'; +import type { NextApiRequest, NextApiResponse } from "next"; +import prisma from "../../../lib/prisma"; +import { getBusyCalendarTimes } from "../../../lib/calendarClient"; +import { getBusyVideoTimes } from "../../../lib/videoClient"; import dayjs from "dayjs"; export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const { user } = req.query + const { user } = req.query; - const currentUser = await prisma.user.findFirst({ - where: { - username: user, - }, - select: { - credentials: true, - timeZone: true, - bufferTime: true - } - }); + const currentUser = await prisma.user.findFirst({ + where: { + username: user, + }, + select: { + credentials: true, + timeZone: true, + bufferTime: true, + id: true, + }, + }); - const selectedCalendars = (await prisma.selectedCalendar.findMany({ - where: { - userId: currentUser.id - } - })); + const selectedCalendars = await prisma.selectedCalendar.findMany({ + where: { + userId: currentUser.id, + }, + }); - const hasCalendarIntegrations = currentUser.credentials.filter((cred) => cred.type.endsWith('_calendar')).length > 0; - const hasVideoIntegrations = currentUser.credentials.filter((cred) => cred.type.endsWith('_video')).length > 0; + const hasCalendarIntegrations = + currentUser.credentials.filter((cred) => cred.type.endsWith("_calendar")).length > 0; + const hasVideoIntegrations = + currentUser.credentials.filter((cred) => cred.type.endsWith("_video")).length > 0; - const calendarAvailability = await getBusyCalendarTimes(currentUser.credentials, req.query.dateFrom, req.query.dateTo, selectedCalendars); - const videoAvailability = await getBusyVideoTimes(currentUser.credentials, req.query.dateFrom, req.query.dateTo); + const calendarAvailability = await getBusyCalendarTimes( + currentUser.credentials, + req.query.dateFrom, + req.query.dateTo, + selectedCalendars + ); + const videoAvailability = await getBusyVideoTimes( + currentUser.credentials, + req.query.dateFrom, + req.query.dateTo + ); - let commonAvailability = []; + let commonAvailability = []; - if(hasCalendarIntegrations && hasVideoIntegrations) { - commonAvailability = calendarAvailability.filter(availability => videoAvailability.includes(availability)); - } else if(hasVideoIntegrations) { - commonAvailability = videoAvailability; - } else if(hasCalendarIntegrations) { - commonAvailability = calendarAvailability; - } + if (hasCalendarIntegrations && hasVideoIntegrations) { + commonAvailability = calendarAvailability.filter((availability) => + videoAvailability.includes(availability) + ); + } else if (hasVideoIntegrations) { + commonAvailability = videoAvailability; + } else if (hasCalendarIntegrations) { + commonAvailability = calendarAvailability; + } - commonAvailability = commonAvailability.map(a => ({ - start: dayjs(a.start).subtract(currentUser.bufferTime, 'minute').toString(), - end: dayjs(a.end).add(currentUser.bufferTime, 'minute').toString() - })); + commonAvailability = commonAvailability.map((a) => ({ + start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toString(), + end: dayjs(a.end).add(currentUser.bufferTime, "minute").toString(), + })); - res.status(200).json(commonAvailability); + res.status(200).json(commonAvailability); }