Skip to content

Scheduling infrastructure for absolutely everyone.

fix: API slots endpoint return format (#13377)

Omar Lópeze5b4d2fb80d6
diff --git a/apps/api/pages/api/slots/_get.test.ts b/apps/api/pages/api/slots/_get.test.ts
new file mode 100644
index 00000000000..58e77fe290f
--- /dev/null
+++ b/apps/api/pages/api/slots/_get.test.ts
@@ -0,0 +1,98 @@
+import prismock from "../../../../../tests/libs/__mocks__/prisma";
+
+import type { Request, Response } from "express";
+import type { NextApiRequest, NextApiResponse } from "next";
+import { createMocks } from "node-mocks-http";
+import { describe, expect, test } from "vitest";
+
+import dayjs from "@calcom/dayjs";
+
+import handler from "./_get";
+
+type CustomNextApiRequest = NextApiRequest & Request;
+type CustomNextApiResponse = NextApiResponse & Response;
+
+function buildMockData() {
+ prismock.user.create({
+ data: {
+ id: 1,
+ username: "test",
+ name: "Test User",
+ email: "[email protected]",
+ },
+ });
+ prismock.eventType.create({
+ data: {
+ id: 1,
+ slug: "test",
+ length: 30,
+ title: "Test Event Type",
+ userId: 1,
+ },
+ });
+}
+
+describe("GET /api/slots", () => {
+ describe("Errors", () => {
+ test("Missing required data", async () => {
+ const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
+ method: "GET",
+ });
+
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(400);
+ expect(JSON.parse(res._getData())).toMatchInlineSnapshot(`
+ {
+ "message": "invalid_type in 'startTime': Required; invalid_type in 'endTime': Required",
+ }
+ `);
+ });
+ });
+
+ describe("Success", () => {
+ describe("Regular event-type", () => {
+ test("Returns and event type available slots", async () => {
+ const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
+ method: "GET",
+ query: {
+ eventTypeId: 1,
+ startTime: dayjs().format(),
+ endTime: dayjs().add(1, "day").format(),
+ usernameList: "test",
+ },
+ prisma: prismock,
+ });
+ buildMockData();
+ await handler(req, res);
+ console.log({ statusCode: res._getStatusCode(), data: JSON.parse(res._getData()) });
+ expect(JSON.parse(res._getData())).toMatchInlineSnapshot(`
+ {
+ "slots": {},
+ }
+ `);
+ });
+ test("Returns and event type available slots with passed timeZone", async () => {
+ const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
+ method: "GET",
+ query: {
+ eventTypeId: 1,
+ startTime: dayjs().format(),
+ endTime: dayjs().add(1, "day").format(),
+ usernameList: "test",
+ timeZone: "UTC",
+ },
+ prisma: prismock,
+ });
+ buildMockData();
+ await handler(req, res);
+ console.log({ statusCode: res._getStatusCode(), data: JSON.parse(res._getData()) });
+ expect(JSON.parse(res._getData())).toMatchInlineSnapshot(`
+ {
+ "slots": {},
+ }
+ `);
+ });
+ });
+ });
+});
diff --git a/tests/libs/__mocks__/prisma.ts b/tests/libs/__mocks__/prisma.ts
index 1f508e1ef43..7d2b9b34c5f 100644
--- a/tests/libs/__mocks__/prisma.ts
+++ b/tests/libs/__mocks__/prisma.ts
@@ -7,6 +7,7 @@ import * as selects from "@calcom/prisma/selects";
vi.mock("@calcom/prisma", () => ({
default: prisma,
prisma,
+ readonlyPrisma: prisma,
...selects,
}));