- getPlanningForDate(houseId, date: DateTime) — paramétré au lieu de toujours "aujourd'hui", même logique de recherche sinon - GET /planning?date=YYYY-MM-DD, validation de forme (zod) puis de validité calendaire (parseDateOnly, 400 VALIDATION_ERROR sinon) — un seul endpoint générique au lieu de deux qui se recouvrent - Tests Mocha + Cucumber adaptés, + cas date manquante/malformée/ impossible et "semaine différente d'aujourd'hui"
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
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<unknown, AuthLocals>(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);
|
|
}),
|
|
);
|