import type { FastifyInstance, FastifyReply } from "fastify"; import type { Tender } from "@parking/shared"; import { requireAnyPermission, requirePermission } from "../../auth.js"; import { requireModule } from "../../modules.js"; import { NoShiftOpenError } from "../../shift-service.js"; import type { ServerModuleDeps } from "../index.js"; import type { ReviewOutbox } from "./review-outbox.js"; import { CarwashError, CarwashService, isPayAt, type SettingsBody } from "./service.js"; // HTTP surface of the Car Wash module. Every route is behind the venue-module gate // FIRST (403 module_disabled), then a permission: // settings (master data) site:read / site:update — the site admin's job // queue / ticket lookup carwash:read — the wash desk // intake carwash:create // done / bay payment / void carwash:update // The sponsorship PROGRAM itself is a validation program row (id "carwash") and is // composed through the existing /api/validation/programs/:id route (site:update). function sendError(reply: FastifyReply, err: unknown): FastifyReply { if (err instanceof CarwashError) { return reply.code(err.status).send({ error: err.message, ...(err.code ? { code: err.code } : {}) }); } if (err instanceof NoShiftOpenError) { // The bay takes money on the CARWASH till: the wash operator's own shift must be // open (the booth's does not count). The desk shows its shift control on this code. return reply.code(409).send({ error: err.message, code: "no_shift", till: err.till }); } throw err; } export async function carwashRoutes(app: FastifyInstance, deps: ServerModuleDeps, service: CarwashService, outbox?: ReviewOutbox): Promise { const moduleOn = requireModule(deps.db, "carwash"); // The price list is the desk's working data as much as Setup's: the wash operator // reads it under the module's own permission (the Wash operator job holds no site:*). const settingsRead = [moduleOn, requireAnyPermission("carwash:read", "site:read")]; const settingsWrite = [moduleOn, requirePermission("site:update")]; const read = [moduleOn, requirePermission("carwash:read")]; const create = [moduleOn, requirePermission("carwash:create")]; const update = [moduleOn, requirePermission("carwash:update")]; app.get("/api/carwash/settings", { preHandler: settingsRead }, async () => service.settings()); // The review outbox's health (Setup → Car wash): how many decisions wait for the // reviewer, how many went, the last error. Site admin's read. app.get("/api/carwash/review/status", { preHandler: settingsRead }, async () => outbox?.status() ?? { enabled: false, boothId: null, queued: 0, sent: 0, failed: 0, lastSentAt: null, lastError: null, entrySample: 0 }, ); app.put<{ Body: SettingsBody }>("/api/carwash/settings", { preHandler: settingsWrite }, async (req, reply) => { try { return await service.saveSettings(req.body ?? {}, req.user.username); } catch (err) { return sendError(reply, err); } }); app.get<{ Params: { identity: string } }>("/api/carwash/session/:identity", { preHandler: read }, async (req) => service.lookup(req.params.identity), ); app.get<{ Querystring: { scope?: string; limit?: string } }>("/api/carwash/orders", { preHandler: read }, async (req) => { if (req.query.scope === "recent") return { orders: service.recentOrders(Number(req.query.limit) || 100) }; return { orders: service.openOrders() }; }); app.post<{ Body: { identity?: string; categoryId?: string; serviceId?: string; payAt?: string } }>( "/api/carwash/orders", { preHandler: create }, async (req, reply) => { const b = req.body ?? {}; // payAt is a SITE setting now; the desk no longer sends it. Accept it only when it // matches (the service refuses a mismatch) so a stale client cannot pick the till. if (b.payAt !== undefined && !isPayAt(b.payAt)) return reply.code(400).send({ error: "payAt must be booth|bay" }); try { const order = await service.createOrder({ identity: String(b.identity ?? ""), categoryId: String(b.categoryId ?? ""), serviceId: String(b.serviceId ?? ""), ...(b.payAt !== undefined ? { payAt: b.payAt } : {}), actor: req.user.username, }); return reply.code(201).send(order); } catch (err) { return sendError(reply, err); } }, ); app.post<{ Params: { id: string } }>("/api/carwash/orders/:id/done", { preHandler: update }, async (req, reply) => { try { return await service.markDone(req.params.id, req.user.username); } catch (err) { return sendError(reply, err); } }); app.post<{ Params: { id: string }; Body: { tender?: Tender } }>( "/api/carwash/orders/:id/pay", { preHandler: update }, async (req, reply) => { try { return await service.payAtBay(req.params.id, (req.body?.tender ?? "cash") as Tender, req.user.username); } catch (err) { return sendError(reply, err); } }, ); app.post<{ Params: { id: string }; Body: { reason?: string } }>( "/api/carwash/orders/:id/void", { preHandler: update }, async (req, reply) => { try { return await service.voidOrder(req.params.id, String(req.body?.reason ?? "").trim(), req.user.username); } catch (err) { return sendError(reply, err); } }, ); }