Refactored buildSlots (#6389)

* Refactored buildSlots

* Added typing for computedAvailableSlots

* Adds very simple boundary logic to make sure slots start at the right times

* Logic was cutting off too early

* Woops, should be eventLength, not frequency

* Expect 12:15 slots as availability is till 12:30 for booking

* Deliberately skip test for testing purposes

* slotInterval overrides intervals, but not duration

* Fix calendar mocking

* Another 45m length

* Unskip :(

* Fix test

Co-authored-by: Hariom Balhara <[email protected]>
Co-authored-by: Omar López <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-01-11 17:33:34 +00:00
1 parent b34a568a93
commit c8744768c4
7 files changed
+179 -209

No files matched your search

-1
View File
@@ -89,7 +89,6 @@
"next-seo": "^4.26.0",
"next-themes": "^0.2.0",
"next-transpile-modules": "^10.0.0",
"nock": "^13.2.8",
"nodemailer": "^6.7.8",
"otplib": "^12.0.1",
"qrcode": "^1.5.1",
+29 -60
View File
@@ -6,7 +6,6 @@ import {
SchedulingType,
} from "@prisma/client";
import { diff } from "jest-diff";
import nock from "nock";
import { v4 as uuidv4 } from "uuid";
import logger from "@calcom/lib/logger";
@@ -14,7 +13,7 @@ import prisma from "@calcom/prisma";
import { BookingStatus } from "@calcom/prisma/client";
import { getSchedule, Slot } from "@calcom/trpc/server/routers/viewer/slots";
import { prismaMock } from "../../../../tests/config/singleton";
import { prismaMock, CalendarManagerMock } from "../../../../tests/config/singleton";
// TODO: Mock properly
prismaMock.eventType.findUnique.mockResolvedValue(null);
@@ -228,6 +227,7 @@ describe("getSchedule", () => {
{
id: 1,
slotInterval: 45,
length: 45,
users: [
{
id: 101,
@@ -249,15 +249,12 @@ describe("getSchedule", () => {
// An event with one accepted booking
createBookingScenario(scenarioData);
addBusyTimesInGoogleCalendar(
[
{
start: `${plus2DateString}T04:30:00.000Z`,
end: `${plus2DateString}T23:00:00.000Z`,
},
],
scenarioData
);
addBusyTimesInGoogleCalendar([
{
start: `${plus2DateString}T04:45:00.000Z`,
end: `${plus2DateString}T23:00:00.000Z`,
},
]);
const scheduleForDayWithAGoogleCalendarBooking = await getSchedule(
{
eventTypeId: 1,
@@ -269,7 +266,7 @@ describe("getSchedule", () => {
ctx
);
// As per Google Calendar Availability, only 4PM GMT slot would be available
// As per Google Calendar Availability, only 4PM(4-4:45PM) GMT slot would be available
expect(scheduleForDayWithAGoogleCalendarBooking).toHaveTimeSlots([`04:00:00.000Z`], {
dateString: plus2DateString,
});
@@ -290,6 +287,7 @@ describe("getSchedule", () => {
id: 1,
// If `slotInterval` is set, it supersedes `length`
slotInterval: 45,
length: 45,
users: [
{
id: 101,
@@ -613,15 +611,12 @@ describe("getSchedule", () => {
createBookingScenario(scenarioData);
addBusyTimesInGoogleCalendar(
[
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
],
scenarioData
);
addBusyTimesInGoogleCalendar([
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
]);
const scheduleForEventOnADayWithNonCalBooking = await getSchedule(
{
@@ -690,15 +685,12 @@ describe("getSchedule", () => {
createBookingScenario(scenarioData);
addBusyTimesInGoogleCalendar(
[
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
],
scenarioData
);
addBusyTimesInGoogleCalendar([
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
]);
const scheduleForEventOnADayWithCalBooking = await getSchedule(
{
@@ -785,6 +777,7 @@ describe("getSchedule", () => {
{
id: 1,
slotInterval: 45,
length: 45,
users: [
{
id: 101,
@@ -797,6 +790,7 @@ describe("getSchedule", () => {
{
id: 2,
slotInterval: 45,
length: 45,
users: [
{
id: 102,
@@ -905,6 +899,7 @@ describe("getSchedule", () => {
{
id: 1,
slotInterval: 45,
length: 45,
users: [
{
id: 101,
@@ -918,6 +913,7 @@ describe("getSchedule", () => {
{
id: 2,
slotInterval: 45,
length: 45,
users: [
{
id: 102,
@@ -1082,7 +1078,6 @@ function addEventTypes(eventTypes: InputEventType[], usersStore: InputUser[]) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
prismaMock.eventType.findUnique.mockImplementation(({ where }) => {
console.log("eventTypesWithUsers", eventTypesWithUsers);
return new Promise((resolve) => {
const eventType = eventTypesWithUsers.find((e) => e.id === where.id) as unknown as PrismaEventType & {
users: PrismaUser[];
@@ -1210,7 +1205,6 @@ const getDate = (param: { dateIncrement?: number; monthIncrement?: number; yearI
const date = _date < 10 ? "0" + _date : _date;
const month = _month < 10 ? "0" + _month : _month;
console.log(`Date, month, year for ${JSON.stringify(param)}`, date, month, year);
return {
date,
month,
@@ -1220,38 +1214,13 @@ const getDate = (param: { dateIncrement?: number; monthIncrement?: number; yearI
};
/**
* Remember that this fn must be called only if you expect your test to lookup for busy times in Google Calendar.
* Calling it unnecessarily will result in a test failure. This is how nock works because it would expect a call to the requests and that too only once.
* TODO: Improve this to validate the arguments passed to getBusyCalendarTimes if they are valid or not.
*/
function addBusyTimesInGoogleCalendar(
busy: {
start: string;
end: string;
}[],
data: ScenarioData
}[]
) {
if (!data.users.find((u) => u.credentials && u.selectedCalendars)) {
throw new Error(
"Google Calendar mocking requires atleast one user with both `credentials` and `selectedCalendars`"
);
}
if (!data.apps?.find((app) => app.slug === "google-calendar")) {
throw new Error('Google Calendar mocking requires an app with slug "google-calendar"');
}
logger.silly("Adding busy times in Google Calendar", busy);
nock("https://oauth2.googleapis.com").post("/token").reply(200, {
access_token: "access_token",
expiry_date: Infinity,
});
// Google Calendar with 11th July having many events
nock("https://www.googleapis.com")
.post("/calendar/v3/freeBusy")
.reply(200, {
calendars: [
{
busy,
},
],
});
CalendarManagerMock.getBusyCalendarTimes.mockResolvedValue(busy);
}