import { FreshContext, Handlers } from "$fresh/server.ts"; import { db } from "$root/databases/db.ts"; import { ajustements } from "$root/databases/schema.ts"; import { AuthenticatedState } from "$root/defaults/interfaces.ts"; import { eq } from "npm:drizzle-orm@0.45.2"; const NOT_FOUND = new Response( JSON.stringify({ error: "Ajustement introuvable" }), { status: 404, headers: { "content-type": "application/json" } }, ); const FORBIDDEN = new Response(null, { status: 403 }); export const handler: Handlers = { // #50 GET /ajustements/{numEtud}/{idUE} async GET( _request: Request, context: FreshContext, ): Promise { if (context.state.session.eduPersonPrimaryAffiliation !== "employee") { return FORBIDDEN; } const numEtud = Number(context.params.numEtud); const idUE = Number(context.params.idUE); if (isNaN(numEtud) || isNaN(idUE)) { return new Response("Paramètres invalides", { status: 400 }); } const ajustement = await db .select() .from(ajustements) .where(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)) .then((rows) => rows[0] ?? null); if (!ajustement) return NOT_FOUND; return new Response(JSON.stringify(ajustement), { headers: { "content-type": "application/json" }, }); }, };