Files
PolyMPR/routes/(apps)/mobility/api/mobilites/[idMob].ts
T
djalim 49bcc3083a
Check Deno code / Check Deno code (push) Has been cancelled
Tests / Unit tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
fix: faculty users are now recognized as employees
2026-05-05 15:29:02 +02:00

150 lines
4.1 KiB
TypeScript

import { FreshContext, Handlers } from "$fresh/server.ts";
import { db } from "$root/databases/db.ts";
import { mobilites } from "$root/databases/schema.ts";
import { AuthenticatedState, isEmployee } from "$root/defaults/interfaces.ts";
import { eq } from "npm:drizzle-orm@0.45.2";
const VALID_STATUSES = [
"contracts_received",
"under_revision",
"done",
"validated",
"canceled",
] as const;
const NOT_FOUND = () =>
new Response(
JSON.stringify({ error: "Mobilité introuvable" }),
{ status: 404, headers: { "content-type": "application/json" } },
);
const FORBIDDEN = () => new Response(null, { status: 403 });
export const handler: Handlers<null, AuthenticatedState> = {
// GET /mobilites/:idMob
async GET(
_request: Request,
context: FreshContext<AuthenticatedState>,
): Promise<Response> {
const idMob = Number(context.params.idMob);
if (isNaN(idMob)) {
return new Response("Paramètre idMob invalide", { status: 400 });
}
const row = await db
.select()
.from(mobilites)
.where(eq(mobilites.id, idMob))
.then((rows) => rows[0] ?? null);
if (!row) return NOT_FOUND();
return new Response(JSON.stringify(row), {
headers: { "content-type": "application/json" },
});
},
// PUT /mobilites/:idMob (employee only)
async PUT(
request: Request,
context: FreshContext<AuthenticatedState>,
): Promise<Response> {
if (!isEmployee(context.state.session)) {
return FORBIDDEN();
}
const idMob = Number(context.params.idMob);
if (isNaN(idMob)) {
return new Response("Paramètre idMob invalide", { status: 400 });
}
const body = await request.json();
const { duree, ecole, pays, status, idStage } = body;
if (
status !== undefined &&
!VALID_STATUSES.includes(status)
) {
return new Response(
JSON.stringify({
error: `status invalide, valeurs: ${VALID_STATUSES.join(", ")}`,
}),
{ status: 400, headers: { "content-type": "application/json" } },
);
}
if (duree !== undefined && (!Number.isInteger(duree) || duree < 1)) {
return new Response(
JSON.stringify({ error: "duree doit être un entier >= 1" }),
{ status: 400, headers: { "content-type": "application/json" } },
);
}
const set: Record<string, unknown> = {};
if (duree !== undefined) set.duree = duree;
if (ecole !== undefined) set.ecole = ecole;
if (pays !== undefined) set.pays = pays;
if (status !== undefined) set.status = status;
if (idStage !== undefined) {
set.idStage = idStage;
if (idStage) set.status = "validated";
}
if (Object.keys(set).length === 0) {
return new Response(
JSON.stringify({ error: "Au moins un champ à modifier requis" }),
{ status: 400, headers: { "content-type": "application/json" } },
);
}
const [updated] = await db
.update(mobilites)
.set(set)
.where(eq(mobilites.id, idMob))
.returning();
if (!updated) return NOT_FOUND();
return new Response(JSON.stringify(updated), {
headers: { "content-type": "application/json" },
});
},
// DELETE /mobilites/:idMob (employee only)
async DELETE(
_request: Request,
context: FreshContext<AuthenticatedState>,
): Promise<Response> {
if (!isEmployee(context.state.session)) {
return FORBIDDEN();
}
const idMob = Number(context.params.idMob);
if (isNaN(idMob)) {
return new Response("Paramètre idMob invalide", { status: 400 });
}
// Delete contract file if exists
const row = await db
.select({ contratMob: mobilites.contratMob })
.from(mobilites)
.where(eq(mobilites.id, idMob))
.then((rows) => rows[0] ?? null);
if (row?.contratMob) {
try {
await Deno.remove(`uploads/contracts/${row.contratMob}`);
} catch { /* file may not exist */ }
}
const [deleted] = await db
.delete(mobilites)
.where(eq(mobilites.id, idMob))
.returning();
if (!deleted) return NOT_FOUND();
return new Response(null, { status: 204 });
},
};