adds get schedule by event-type-slug

feat/troubleshooter-v2
Sean Brydon 2023-10-25 10:52:25 +01:00
parent 8e97169de4
commit daab6a17d0
4 changed files with 77 additions and 2 deletions

View File

@ -27,8 +27,6 @@ export function EventTypeSelect() {
}
}, [selectedEventQueryParam, selectedEventType, setSelectedEventType]);
console.log({ options, eventTypes });
return (
<SelectField
label="Event Type"

View File

@ -4,6 +4,7 @@ import { ZCreateInputSchema } from "./create.schema";
import { ZDeleteInputSchema } from "./delete.schema";
import { ZScheduleDuplicateSchema } from "./duplicate.schema";
import { ZGetInputSchema } from "./get.schema";
import { ZGetByEventSlugInputSchema } from "./getScheduleByEventTypeSlug.schema";
import { ZGetByUserIdInputSchema } from "./getScheduleByUserId.schema";
import { ZUpdateInputSchema } from "./update.schema";
@ -14,6 +15,7 @@ type ScheduleRouterHandlerCache = {
update?: typeof import("./update.handler").updateHandler;
duplicate?: typeof import("./duplicate.handler").duplicateHandler;
getScheduleByUserId?: typeof import("./getScheduleByUserId.handler").getScheduleByUserIdHandler;
getScheduleByEventSlug?: typeof import("./getScheduleByEventTypeSlug.handler").getScheduleByEventSlugHandler;
};
const UNSTABLE_HANDLER_CACHE: ScheduleRouterHandlerCache = {};
@ -118,4 +120,21 @@ export const scheduleRouter = router({
input,
});
}),
getScheduleByEventSlug: authedProcedure.input(ZGetByEventSlugInputSchema).query(async ({ input, ctx }) => {
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,
});
}),
});

View File

@ -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<TrpcSessionUser>;
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
};
}
};

View File

@ -0,0 +1,7 @@
import { z } from "zod";
export const ZGetByEventSlugInputSchema = z.object({
eventSlug: z.string(),
});
export type TGetByEventSlugInputSchema = z.infer<typeof ZGetByEventSlugInputSchema>;