From 4389288e699796c67e51dd2706ea003ff6161bed Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Thu, 16 Jun 2022 00:04:04 +0200 Subject: [PATCH] feat: adds users/id/availability endpoint --- pages/api/users/[id]/availability/_get.ts | 29 ++++++++++++++++++++++ pages/api/users/[id]/availability/index.ts | 9 +++++++ 2 files changed, 38 insertions(+) create mode 100644 pages/api/users/[id]/availability/_get.ts create mode 100644 pages/api/users/[id]/availability/index.ts diff --git a/pages/api/users/[id]/availability/_get.ts b/pages/api/users/[id]/availability/_get.ts new file mode 100644 index 0000000000..11c9b3d586 --- /dev/null +++ b/pages/api/users/[id]/availability/_get.ts @@ -0,0 +1,29 @@ +import type { NextApiRequest } from "next"; +import { z } from "zod"; + +import { getUserAvailability } from "@calcom/core/getUserAvailability"; +import { defaultResponder } from "@calcom/lib/server"; +import { stringOrNumber } from "@calcom/prisma/zod-utils"; + +const availabilitySchema = z + .object({ + id: stringOrNumber, + username: z.string().optional(), + dateFrom: z.string(), + dateTo: z.string(), + eventTypeId: stringOrNumber.optional(), + }) + .refine((data) => !!data.username || !!data.id, "Either username or userId should be filled in."); + +async function handler(req: NextApiRequest) { + const { username, id, eventTypeId, dateTo, dateFrom } = availabilitySchema.parse(req.query); + return getUserAvailability({ + username, + dateFrom, + dateTo, + eventTypeId, + userId: id, + }); +} + +export default defaultResponder(handler); diff --git a/pages/api/users/[id]/availability/index.ts b/pages/api/users/[id]/availability/index.ts new file mode 100644 index 0000000000..fe6357f6bc --- /dev/null +++ b/pages/api/users/[id]/availability/index.ts @@ -0,0 +1,9 @@ +import { defaultHandler } from "@calcom/lib/server"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; + +export default withMiddleware("HTTP_GET")( + defaultHandler({ + GET: import("./_get"), + }) +);