2023-04-25 22:39:47 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
import publicProcedure from "../../../procedures/publicProcedure";
|
|
|
|
import { router } from "../../../trpc";
|
2023-04-25 22:39:47 +00:00
|
|
|
import { ZGetScheduleInputSchema } from "./getSchedule.schema";
|
|
|
|
import { ZReserveSlotInputSchema } from "./reserveSlot.schema";
|
|
|
|
|
|
|
|
type SlotsRouterHandlerCache = {
|
|
|
|
getSchedule?: typeof import("./getSchedule.handler").getScheduleHandler;
|
|
|
|
reserveSlot?: typeof import("./reserveSlot.handler").reserveSlotHandler;
|
|
|
|
};
|
|
|
|
|
|
|
|
const UNSTABLE_HANDLER_CACHE: SlotsRouterHandlerCache = {};
|
|
|
|
|
|
|
|
/** This should be called getAvailableSlots */
|
|
|
|
export const slotsRouter = router({
|
|
|
|
getSchedule: publicProcedure.input(ZGetScheduleInputSchema).query(async ({ input, ctx }) => {
|
|
|
|
if (!UNSTABLE_HANDLER_CACHE.getSchedule) {
|
|
|
|
UNSTABLE_HANDLER_CACHE.getSchedule = await import("./getSchedule.handler").then(
|
|
|
|
(mod) => mod.getScheduleHandler
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unreachable code but required for type safety
|
|
|
|
if (!UNSTABLE_HANDLER_CACHE.getSchedule) {
|
|
|
|
throw new Error("Failed to load handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
return UNSTABLE_HANDLER_CACHE.getSchedule({
|
|
|
|
ctx,
|
|
|
|
input,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
reserveSlot: publicProcedure.input(ZReserveSlotInputSchema).mutation(async ({ input, ctx }) => {
|
|
|
|
if (!UNSTABLE_HANDLER_CACHE.reserveSlot) {
|
|
|
|
UNSTABLE_HANDLER_CACHE.reserveSlot = await import("./reserveSlot.handler").then(
|
|
|
|
(mod) => mod.reserveSlotHandler
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unreachable code but required for type safety
|
|
|
|
if (!UNSTABLE_HANDLER_CACHE.reserveSlot) {
|
|
|
|
throw new Error("Failed to load handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
return UNSTABLE_HANDLER_CACHE.reserveSlot({
|
|
|
|
ctx: { ...ctx, req: ctx.req as NextApiRequest, res: ctx.res as NextApiResponse },
|
|
|
|
input,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
// This endpoint has no dependencies, it doesn't need its own file
|
|
|
|
removeSelectedSlotMark: publicProcedure.mutation(async ({ ctx }) => {
|
|
|
|
const { req, prisma } = ctx;
|
|
|
|
const uid = req?.cookies?.uid;
|
|
|
|
if (uid) {
|
|
|
|
await prisma.selectedSlots.deleteMany({ where: { uid: { equals: uid } } });
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}),
|
|
|
|
});
|