From daab6a17d0ea6edb5f2e3f499c3e4ba097f2f711 Mon Sep 17 00:00:00 2001 From: Sean Brydon Date: Wed, 25 Oct 2023 10:52:25 +0100 Subject: [PATCH] adds get schedule by event-type-slug --- .../components/EventTypeSelect.tsx | 2 - .../viewer/availability/schedule/_router.tsx | 19 +++++++ .../getScheduleByEventTypeSlug.handler.ts | 51 +++++++++++++++++++ .../getScheduleByEventTypeSlug.schema.ts | 7 +++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.handler.ts create mode 100644 packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.schema.ts diff --git a/packages/features/troubleshooter/components/EventTypeSelect.tsx b/packages/features/troubleshooter/components/EventTypeSelect.tsx index 4628fa7d0b..acaa61cad5 100644 --- a/packages/features/troubleshooter/components/EventTypeSelect.tsx +++ b/packages/features/troubleshooter/components/EventTypeSelect.tsx @@ -27,8 +27,6 @@ export function EventTypeSelect() { } }, [selectedEventQueryParam, selectedEventType, setSelectedEventType]); - console.log({ options, eventTypes }); - return ( { + if (!UNSTABLE_HANDLER_CACHE.getScheduleByEventSlug) { + UNSTABLE_HANDLER_CACHE.getScheduleByEventSlug = await import( + "./getScheduleByEventTypeSlug.handler" + ).then((mod) => mod.getScheduleByEventSlugHandler); + } + + // Unreachable code but required for type safety + if (!UNSTABLE_HANDLER_CACHE.getScheduleByEventSlug) { + throw new Error("Failed to load handler"); + } + + return UNSTABLE_HANDLER_CACHE.getScheduleByEventSlug({ + ctx, + input, + }); + }), }); diff --git a/packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.handler.ts b/packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.handler.ts new file mode 100644 index 0000000000..fcf69731c5 --- /dev/null +++ b/packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.handler.ts @@ -0,0 +1,51 @@ +import type { PrismaClient } from "@calcom/prisma"; + +import type { TrpcSessionUser } from "../../../../trpc"; +import { getHandler } from "./get.handler"; +import type { TGetByEventSlugInputSchema } from "./getScheduleByEventTypeSlug.schema"; + +type GetOptions = { + ctx: { + user: NonNullable; + prisma: PrismaClient; + }; + input: TGetByEventSlugInputSchema; +}; + +const EMPTY_SCHEDULE = [[], [], [], [], [], [], []]; + +export const getScheduleByEventSlugHandler = async ({ ctx, input }: GetOptions) => { + const foundScheduleForSlug = await ctx.prisma.eventType.findFirst({ + where: { + slug: input.eventSlug, + userId: ctx.user.id, + }, + select: { + scheduleId: true, + }, + }); + + try { + // This looks kinda weird that we throw straight in the catch - its so that we can return a default schedule if the user has not completed onboarding @shiraz will loveme for this + if (!foundScheduleForSlug?.scheduleId) { + throw new Error("NOT_FOUND"); + } + return await getHandler({ + ctx, + input: { + scheduleId: foundScheduleForSlug?.scheduleId, + }, + }); + } catch (e) { + return { + id: -1, + name: "No schedules found", + availability: EMPTY_SCHEDULE, + dateOverrides: [], + timeZone: ctx.user.timeZone || "Europe/London", + workingHours: [], + isDefault: true, + hasDefaultSchedule: false, // This is the path that we take if the user has not completed onboarding + }; + } +}; diff --git a/packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.schema.ts b/packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.schema.ts new file mode 100644 index 0000000000..b65f1b5e37 --- /dev/null +++ b/packages/trpc/server/routers/viewer/availability/schedule/getScheduleByEventTypeSlug.schema.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +export const ZGetByEventSlugInputSchema = z.object({ + eventSlug: z.string(), +}); + +export type TGetByEventSlugInputSchema = z.infer;