From 677a964d4fb948bb676e0388e35960787406aa0d Mon Sep 17 00:00:00 2001 From: Abhijeet Singh Date: Fri, 2 Feb 2024 20:40:43 +0530 Subject: [PATCH] feat: Added new API route to get event-types using slug (#13374) * added new api route /event-types/bySlug/{slug} * refactor: schema query slug * refactor: event-types/bySlug to event-types/by-slug route * feat: allow admins to fetch event types on behalf of others * fix: auth middleware * added slug query param to main event-type api route * removed the /by-slug route * refactor logic * remove unwanted changes * updated swagger api doc --------- Co-authored-by: supalarry --- apps/api/lib/validations/shared/querySlug.ts | 7 +++++++ apps/api/pages/api/event-types/_get.ts | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 apps/api/lib/validations/shared/querySlug.ts diff --git a/apps/api/lib/validations/shared/querySlug.ts b/apps/api/lib/validations/shared/querySlug.ts new file mode 100644 index 0000000000..e8fefc3fd0 --- /dev/null +++ b/apps/api/lib/validations/shared/querySlug.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +import { baseApiParams } from "./baseApiParams"; + +export const schemaQuerySlug = baseApiParams.extend({ + slug: z.string().optional(), +}); diff --git a/apps/api/pages/api/event-types/_get.ts b/apps/api/pages/api/event-types/_get.ts index b2094ab13e..5b014f603d 100644 --- a/apps/api/pages/api/event-types/_get.ts +++ b/apps/api/pages/api/event-types/_get.ts @@ -5,6 +5,7 @@ import { defaultResponder } from "@calcom/lib/server"; import type { PrismaClient } from "@calcom/prisma"; import { schemaEventTypeReadPublic } from "~/lib/validations/event-type"; +import { schemaQuerySlug } from "~/lib/validations/shared/querySlug"; import { schemaQuerySingleOrMultipleUserIds } from "~/lib/validations/shared/queryUserId"; import getCalLink from "./_utils/getCalLink"; @@ -22,6 +23,12 @@ import getCalLink from "./_utils/getCalLink"; * type: string * required: true * description: Your API Key + * - in: query + * name: slug + * schema: + * type: string + * required: false + * description: Slug to filter event types by * tags: * - event-types * externalDocs: @@ -35,11 +42,16 @@ import getCalLink from "./_utils/getCalLink"; * description: No event types were found */ async function getHandler(req: NextApiRequest) { - const { userId, prisma } = req; + const { userId, prisma, isAdmin } = req; const userIds = req.query.userId ? extractUserIdsFromQuery(req) : [userId]; + const { slug } = schemaQuerySlug.parse(req.query); + const shouldUseUserId = !isAdmin || !slug || !!req.query.userId; + // When user is admin and no query params are provided we should return all event types. + // But currently we return only the event types of the user. Not changing this for backwards compatibility. const data = await prisma.eventType.findMany({ where: { - userId: { in: userIds }, + userId: shouldUseUserId ? { in: userIds } : undefined, + slug: slug, // slug will be undefined if not provided in query }, include: { customInputs: true,