|
| 1 | +import { z } from "zod"; |
| 2 | +import { eq, and, isNull, asc } from "drizzle-orm"; |
| 3 | +import { TRPCError } from "@trpc/server"; |
| 4 | +import { createRouter, protectedProcedure, requireRole } from "../trpc"; |
| 5 | +import { appointmentWaitlist, clients, patients, appointmentTypes } from "@openpims/db"; |
| 6 | +import { matchWaitlist, type WaitlistEntry } from "@/lib/scheduling/waitlist"; |
| 7 | + |
| 8 | +const manageRole = requireRole("admin", "veterinarian", "front_desk"); |
| 9 | + |
| 10 | +/** Join the display fields the UI needs alongside the matcher fields. */ |
| 11 | +function selectShape() { |
| 12 | + return { |
| 13 | + id: appointmentWaitlist.id, |
| 14 | + status: appointmentWaitlist.status, |
| 15 | + typeId: appointmentWaitlist.typeId, |
| 16 | + preferredFrom: appointmentWaitlist.preferredFrom, |
| 17 | + preferredTo: appointmentWaitlist.preferredTo, |
| 18 | + notes: appointmentWaitlist.notes, |
| 19 | + createdAt: appointmentWaitlist.createdAt, |
| 20 | + clientFirstName: clients.firstName, |
| 21 | + clientLastName: clients.lastName, |
| 22 | + clientPhone: clients.phone, |
| 23 | + patientName: patients.name, |
| 24 | + typeName: appointmentTypes.name, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +export const waitlistRouter = createRouter({ |
| 29 | + list: protectedProcedure |
| 30 | + .input( |
| 31 | + z |
| 32 | + .object({ status: z.enum(["waiting", "scheduled", "cancelled"]).default("waiting") }) |
| 33 | + .optional() |
| 34 | + ) |
| 35 | + .query(async ({ ctx, input }) => { |
| 36 | + const status = input?.status ?? "waiting"; |
| 37 | + return ctx.db |
| 38 | + .select(selectShape()) |
| 39 | + .from(appointmentWaitlist) |
| 40 | + .leftJoin(clients, eq(appointmentWaitlist.clientId, clients.id)) |
| 41 | + .leftJoin(patients, eq(appointmentWaitlist.patientId, patients.id)) |
| 42 | + .leftJoin(appointmentTypes, eq(appointmentWaitlist.typeId, appointmentTypes.id)) |
| 43 | + .where( |
| 44 | + and( |
| 45 | + eq(appointmentWaitlist.practiceId, ctx.practiceId), |
| 46 | + eq(appointmentWaitlist.status, status), |
| 47 | + isNull(appointmentWaitlist.deletedAt) |
| 48 | + ) |
| 49 | + ) |
| 50 | + .orderBy(asc(appointmentWaitlist.createdAt)); |
| 51 | + }), |
| 52 | + |
| 53 | + add: protectedProcedure |
| 54 | + .use(manageRole) |
| 55 | + .input( |
| 56 | + z.object({ |
| 57 | + clientId: z.string().uuid(), |
| 58 | + patientId: z.string().uuid().optional(), |
| 59 | + typeId: z.string().uuid().optional(), |
| 60 | + preferredFrom: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(), |
| 61 | + preferredTo: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(), |
| 62 | + notes: z.string().max(1000).optional(), |
| 63 | + }) |
| 64 | + ) |
| 65 | + .mutation(async ({ ctx, input }) => { |
| 66 | + const [row] = await ctx.db |
| 67 | + .insert(appointmentWaitlist) |
| 68 | + .values({ |
| 69 | + practiceId: ctx.practiceId, |
| 70 | + clientId: input.clientId, |
| 71 | + patientId: input.patientId ?? null, |
| 72 | + typeId: input.typeId ?? null, |
| 73 | + preferredFrom: input.preferredFrom ?? null, |
| 74 | + preferredTo: input.preferredTo ?? null, |
| 75 | + notes: input.notes ?? null, |
| 76 | + createdBy: ctx.user.id, |
| 77 | + }) |
| 78 | + .returning(); |
| 79 | + return row!; |
| 80 | + }), |
| 81 | + |
| 82 | + setStatus: protectedProcedure |
| 83 | + .use(manageRole) |
| 84 | + .input( |
| 85 | + z.object({ |
| 86 | + id: z.string().uuid(), |
| 87 | + status: z.enum(["waiting", "scheduled", "cancelled"]), |
| 88 | + }) |
| 89 | + ) |
| 90 | + .mutation(async ({ ctx, input }) => { |
| 91 | + const [updated] = await ctx.db |
| 92 | + .update(appointmentWaitlist) |
| 93 | + .set({ status: input.status }) |
| 94 | + .where( |
| 95 | + and( |
| 96 | + eq(appointmentWaitlist.id, input.id), |
| 97 | + eq(appointmentWaitlist.practiceId, ctx.practiceId), |
| 98 | + isNull(appointmentWaitlist.deletedAt) |
| 99 | + ) |
| 100 | + ) |
| 101 | + .returning(); |
| 102 | + if (!updated) throw new TRPCError({ code: "NOT_FOUND", message: "Waitlist entry not found" }); |
| 103 | + return updated; |
| 104 | + }), |
| 105 | + |
| 106 | + /** |
| 107 | + * When a slot opens up (e.g. a cancellation), find waiting clients who fit |
| 108 | + * the freed date + appointment type, oldest request first. |
| 109 | + */ |
| 110 | + matchesForSlot: protectedProcedure |
| 111 | + .input( |
| 112 | + z.object({ |
| 113 | + date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), |
| 114 | + typeId: z.string().uuid().optional(), |
| 115 | + }) |
| 116 | + ) |
| 117 | + .query(async ({ ctx, input }) => { |
| 118 | + const rows = await ctx.db |
| 119 | + .select(selectShape()) |
| 120 | + .from(appointmentWaitlist) |
| 121 | + .leftJoin(clients, eq(appointmentWaitlist.clientId, clients.id)) |
| 122 | + .leftJoin(patients, eq(appointmentWaitlist.patientId, patients.id)) |
| 123 | + .leftJoin(appointmentTypes, eq(appointmentWaitlist.typeId, appointmentTypes.id)) |
| 124 | + .where( |
| 125 | + and( |
| 126 | + eq(appointmentWaitlist.practiceId, ctx.practiceId), |
| 127 | + eq(appointmentWaitlist.status, "waiting"), |
| 128 | + isNull(appointmentWaitlist.deletedAt) |
| 129 | + ) |
| 130 | + ); |
| 131 | + |
| 132 | + const entries: WaitlistEntry[] = rows.map((r) => ({ |
| 133 | + id: r.id, |
| 134 | + status: r.status, |
| 135 | + typeId: r.typeId, |
| 136 | + preferredFrom: r.preferredFrom, |
| 137 | + preferredTo: r.preferredTo, |
| 138 | + createdAt: r.createdAt, |
| 139 | + })); |
| 140 | + // Run the matcher once, then map back to the joined display rows in the |
| 141 | + // matcher's FIFO order. |
| 142 | + const byId = new Map(rows.map((r) => [r.id, r])); |
| 143 | + return matchWaitlist(entries, { date: input.date, typeId: input.typeId }) |
| 144 | + .map((e) => byId.get(e.id)) |
| 145 | + .filter((r): r is (typeof rows)[number] => Boolean(r)); |
| 146 | + }), |
| 147 | +}); |
0 commit comments