Fix issue where user ID is not fetched

pull/348/head
Bailey Pumfleet 2021-07-06 22:09:53 +01:00
parent 90d6f8faee
commit 43f899582f
1 changed files with 51 additions and 37 deletions

View File

@ -1,49 +1,63 @@
import type {NextApiRequest, NextApiResponse} from 'next'; import type { NextApiRequest, NextApiResponse } from "next";
import prisma from '../../../lib/prisma'; import prisma from "../../../lib/prisma";
import {getBusyCalendarTimes} from '../../../lib/calendarClient'; import { getBusyCalendarTimes } from "../../../lib/calendarClient";
import {getBusyVideoTimes} from '../../../lib/videoClient'; import { getBusyVideoTimes } from "../../../lib/videoClient";
import dayjs from "dayjs"; import dayjs from "dayjs";
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { user } = req.query const { user } = req.query;
const currentUser = await prisma.user.findFirst({ const currentUser = await prisma.user.findFirst({
where: { where: {
username: user, username: user,
}, },
select: { select: {
credentials: true, credentials: true,
timeZone: true, timeZone: true,
bufferTime: true bufferTime: true,
} id: true,
}); },
});
const selectedCalendars = (await prisma.selectedCalendar.findMany({ const selectedCalendars = await prisma.selectedCalendar.findMany({
where: { where: {
userId: currentUser.id userId: currentUser.id,
} },
})); });
const hasCalendarIntegrations = currentUser.credentials.filter((cred) => cred.type.endsWith('_calendar')).length > 0; const hasCalendarIntegrations =
const hasVideoIntegrations = currentUser.credentials.filter((cred) => cred.type.endsWith('_video')).length > 0; 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 calendarAvailability = await getBusyCalendarTimes(
const videoAvailability = await getBusyVideoTimes(currentUser.credentials, req.query.dateFrom, req.query.dateTo); 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) { if (hasCalendarIntegrations && hasVideoIntegrations) {
commonAvailability = calendarAvailability.filter(availability => videoAvailability.includes(availability)); commonAvailability = calendarAvailability.filter((availability) =>
} else if(hasVideoIntegrations) { videoAvailability.includes(availability)
commonAvailability = videoAvailability; );
} else if(hasCalendarIntegrations) { } else if (hasVideoIntegrations) {
commonAvailability = calendarAvailability; commonAvailability = videoAvailability;
} } else if (hasCalendarIntegrations) {
commonAvailability = calendarAvailability;
}
commonAvailability = commonAvailability.map(a => ({ commonAvailability = commonAvailability.map((a) => ({
start: dayjs(a.start).subtract(currentUser.bufferTime, 'minute').toString(), start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toString(),
end: dayjs(a.end).add(currentUser.bufferTime, 'minute').toString() end: dayjs(a.end).add(currentUser.bufferTime, "minute").toString(),
})); }));
res.status(200).json(commonAvailability); res.status(200).json(commonAvailability);
} }