import { parseDateOnly } from "@batch-cooking/date-tools"; import { HttpError } from "@batch-cooking/error-tools"; import { wrapAsyncHandler } from "@batch-cooking/express-tools"; import { ErrorCode, getPlanningByDateSchema } from "@batch-cooking/shared"; import { Router } from "express"; import { type AuthLocals, requireAuth } from "../../middlewares/require-auth.js"; import { getPlanningForDate } from "./planning.service.js"; /** Router mounted at `/planning` in app.ts. */ export const planningRouter = Router(); /** * Returns the authenticated user's household's planning covering `?date=` * (`YYYY-MM-DD`), or `null` if none exists yet — a valid, common response, * not an error (see {@link getPlanningForDate}). Used both for "today" * (the planning page's initial load) and for any other week the planning * page's week navigator/calendar picks. */ planningRouter.get( "/", requireAuth, wrapAsyncHandler(async (req, res) => { const input = getPlanningByDateSchema.parse(req.query); const date = parseDateOnly(input.date); if (date === null) { throw new HttpError( 400, ErrorCode.VALIDATION_ERROR, `Not a real calendar date: ${input.date}`, ); } const planning = await getPlanningForDate(res.locals.userProfile.houseId, date); res.status(200).json(planning); }), );