safely check isAvailable (#331)
fix issue where checking availability throws an error
This commit is contained in:
parent
e1720e0161
commit
2f73997e72
1 changed files with 75 additions and 69 deletions
|
@ -1,10 +1,10 @@
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import prisma from "../../../lib/prisma";
|
import prisma from "../../../lib/prisma";
|
||||||
import { CalendarEvent, createEvent, updateEvent } from "../../../lib/calendarClient";
|
import { CalendarEvent, createEvent, getBusyCalendarTimes, updateEvent } from "../../../lib/calendarClient";
|
||||||
import async from "async";
|
import async from "async";
|
||||||
import { v5 as uuidv5 } from "uuid";
|
import { v5 as uuidv5 } from "uuid";
|
||||||
import short from "short-uuid";
|
import short from "short-uuid";
|
||||||
import { createMeeting, updateMeeting } from "../../../lib/videoClient";
|
import { createMeeting, getBusyVideoTimes, updateMeeting } from "../../../lib/videoClient";
|
||||||
import EventAttendeeMail from "../../../lib/emails/EventAttendeeMail";
|
import EventAttendeeMail from "../../../lib/emails/EventAttendeeMail";
|
||||||
import { getEventName } from "../../../lib/event";
|
import { getEventName } from "../../../lib/event";
|
||||||
import { LocationType } from "../../../lib/location";
|
import { LocationType } from "../../../lib/location";
|
||||||
|
@ -13,37 +13,39 @@ import dayjs from "dayjs";
|
||||||
|
|
||||||
const translator = short();
|
const translator = short();
|
||||||
|
|
||||||
// Commented out because unused and thus throwing an error in linter.
|
function isAvailable(busyTimes, time, length) {
|
||||||
// const isAvailable = (busyTimes, time, length) => {
|
// Check for conflicts
|
||||||
// // Check for conflicts
|
let t = true;
|
||||||
// let t = true;
|
|
||||||
// busyTimes.forEach((busyTime) => {
|
if (Array.isArray(busyTimes) && busyTimes.length > 0) {
|
||||||
// const startTime = dayjs(busyTime.start);
|
busyTimes.forEach((busyTime) => {
|
||||||
// const endTime = dayjs(busyTime.end);
|
const startTime = dayjs(busyTime.start);
|
||||||
//
|
const endTime = dayjs(busyTime.end);
|
||||||
// // Check if start times are the same
|
|
||||||
// if (dayjs(time).format("HH:mm") == startTime.format("HH:mm")) {
|
// Check if start times are the same
|
||||||
// t = false;
|
if (dayjs(time).format("HH:mm") == startTime.format("HH:mm")) {
|
||||||
// }
|
t = false;
|
||||||
//
|
}
|
||||||
// // Check if time is between start and end times
|
|
||||||
// if (dayjs(time).isBetween(startTime, endTime)) {
|
// Check if time is between start and end times
|
||||||
// t = false;
|
if (dayjs(time).isBetween(startTime, endTime)) {
|
||||||
// }
|
t = false;
|
||||||
//
|
}
|
||||||
// // Check if slot end time is between start and end time
|
|
||||||
// if (dayjs(time).add(length, "minutes").isBetween(startTime, endTime)) {
|
// Check if slot end time is between start and end time
|
||||||
// t = false;
|
if (dayjs(time).add(length, "minutes").isBetween(startTime, endTime)) {
|
||||||
// }
|
t = false;
|
||||||
//
|
}
|
||||||
// // Check if startTime is between slot
|
|
||||||
// if (startTime.isBetween(dayjs(time), dayjs(time).add(length, "minutes"))) {
|
// Check if startTime is between slot
|
||||||
// t = false;
|
if (startTime.isBetween(dayjs(time), dayjs(time).add(length, "minutes"))) {
|
||||||
// }
|
t = false;
|
||||||
// });
|
}
|
||||||
//
|
});
|
||||||
// return t;
|
}
|
||||||
// };
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
interface GetLocationRequestFromIntegrationRequest {
|
interface GetLocationRequestFromIntegrationRequest {
|
||||||
location: string;
|
location: string;
|
||||||
|
@ -91,46 +93,43 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Commented out because unused and thus throwing an error in linter.
|
const selectedCalendars = await prisma.selectedCalendar.findMany({
|
||||||
// const selectedCalendars = await prisma.selectedCalendar.findMany({
|
where: {
|
||||||
// where: {
|
userId: currentUser.id,
|
||||||
// userId: currentUser.id,
|
},
|
||||||
// },
|
});
|
||||||
// });
|
|
||||||
// Split credentials up into calendar credentials and video credentials
|
// Split credentials up into calendar credentials and video credentials
|
||||||
let calendarCredentials = currentUser.credentials.filter((cred) => cred.type.endsWith("_calendar"));
|
let calendarCredentials = currentUser.credentials.filter((cred) => cred.type.endsWith("_calendar"));
|
||||||
let videoCredentials = currentUser.credentials.filter((cred) => cred.type.endsWith("_video"));
|
let videoCredentials = currentUser.credentials.filter((cred) => cred.type.endsWith("_video"));
|
||||||
|
|
||||||
// Commented out because unused and thus throwing an error in linter.
|
const hasCalendarIntegrations =
|
||||||
// const hasCalendarIntegrations =
|
currentUser.credentials.filter((cred) => cred.type.endsWith("_calendar")).length > 0;
|
||||||
// currentUser.credentials.filter((cred) => cred.type.endsWith("_calendar")).length > 0;
|
const hasVideoIntegrations =
|
||||||
// const hasVideoIntegrations =
|
currentUser.credentials.filter((cred) => cred.type.endsWith("_video")).length > 0;
|
||||||
// currentUser.credentials.filter((cred) => cred.type.endsWith("_video")).length > 0;
|
|
||||||
|
|
||||||
// Commented out because unused and thus throwing an error in linter.
|
const calendarAvailability = await getBusyCalendarTimes(
|
||||||
// const calendarAvailability = await getBusyCalendarTimes(
|
currentUser.credentials,
|
||||||
// currentUser.credentials,
|
dayjs(req.body.start).startOf("day").utc().format(),
|
||||||
// dayjs(req.body.start).startOf("day").utc().format(),
|
dayjs(req.body.end).endOf("day").utc().format(),
|
||||||
// dayjs(req.body.end).endOf("day").utc().format(),
|
selectedCalendars
|
||||||
// selectedCalendars
|
);
|
||||||
// );
|
const videoAvailability = await getBusyVideoTimes(
|
||||||
// const videoAvailability = await getBusyVideoTimes(
|
currentUser.credentials,
|
||||||
// currentUser.credentials,
|
dayjs(req.body.start).startOf("day").utc().format(),
|
||||||
// dayjs(req.body.start).startOf("day").utc().format(),
|
dayjs(req.body.end).endOf("day").utc().format()
|
||||||
// dayjs(req.body.end).endOf("day").utc().format()
|
);
|
||||||
// );
|
let commonAvailability = [];
|
||||||
// let commonAvailability = [];
|
|
||||||
|
|
||||||
// Commented out because unused and thus throwing an error in linter.
|
if (hasCalendarIntegrations && hasVideoIntegrations) {
|
||||||
// if (hasCalendarIntegrations && hasVideoIntegrations) {
|
commonAvailability = calendarAvailability.filter((availability) =>
|
||||||
// commonAvailability = calendarAvailability.filter((availability) =>
|
videoAvailability.includes(availability)
|
||||||
// videoAvailability.includes(availability)
|
);
|
||||||
// );
|
} else if (hasVideoIntegrations) {
|
||||||
// } else if (hasVideoIntegrations) {
|
commonAvailability = videoAvailability;
|
||||||
// commonAvailability = videoAvailability;
|
} else if (hasCalendarIntegrations) {
|
||||||
// } else if (hasCalendarIntegrations) {
|
commonAvailability = calendarAvailability;
|
||||||
// commonAvailability = calendarAvailability;
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// Now, get the newly stored credentials (new refresh token for example).
|
// Now, get the newly stored credentials (new refresh token for example).
|
||||||
currentUser = await prisma.user.findFirst({
|
currentUser = await prisma.user.findFirst({
|
||||||
|
@ -201,8 +200,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO isAvailable was throwing an error
|
let isAvailableToBeBooked = true;
|
||||||
const isAvailableToBeBooked = true; //isAvailable(commonAvailability, req.body.start, selectedEventType.length);
|
|
||||||
|
try {
|
||||||
|
isAvailableToBeBooked = isAvailable(commonAvailability, req.body.start, selectedEventType.length);
|
||||||
|
} catch {
|
||||||
|
console.debug({
|
||||||
|
message: "Unable set isAvailableToBeBooked. Using true. ",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!isAvailableToBeBooked) {
|
if (!isAvailableToBeBooked) {
|
||||||
return res.status(400).json({ message: `${currentUser.name} is unavailable at this time.` });
|
return res.status(400).json({ message: `${currentUser.name} is unavailable at this time.` });
|
||||||
|
|
Loading…
Reference in a new issue