b0930b8da2
- ajustements [numEtud]/[idUE]: fix .where() missing and() — PUT/DELETE were applying only numEtud condition, modifying all rows for a student - modules/users/enseignements POST: add try/catch, return 500 on invalid JSON - modules/[idModule] PUT: add try/catch + type check on nom (string required) - modules POST: add .trim() check to reject whitespace-only id/nom - users POST: add .trim() check to reject whitespace-only id/nom/prenom - ues POST: add .trim() check to reject whitespace-only nom - notes POST: add type check (typeof number) and bounds check (0 ≤ note ≤ 20) - ue-modules POST: add coeff >= 0 validation Update robustness tests to reflect fixed behavior (remove [BUG] labels, replace assertRejects with status code assertions).
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { FreshContext, Handlers } from "$fresh/server.ts";
|
|
import { db } from "$root/databases/db.ts";
|
|
import { modules } from "$root/databases/schema.ts";
|
|
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
|
import { eq } from "npm:drizzle-orm@0.45.2";
|
|
|
|
export const handler: Handlers<null, AuthenticatedState> = {
|
|
// #23 GET /modules
|
|
async GET(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
return new Response(JSON.stringify([]), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
const rows = await db.select().from(modules);
|
|
return new Response(JSON.stringify(rows), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #24 POST /modules
|
|
async POST(
|
|
request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
return new Response(null, { status: 403 });
|
|
}
|
|
|
|
let body: { id: string; nom: string };
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return new Response(null, { status: 500 });
|
|
}
|
|
|
|
if (!body.id || !body.id.trim() || !body.nom || !body.nom.trim()) {
|
|
return new Response(null, { status: 400 });
|
|
}
|
|
|
|
const existing = await db
|
|
.select()
|
|
.from(modules)
|
|
.where(eq(modules.id, body.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (existing) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Un module avec cet identifiant existe déjà" }),
|
|
{ status: 409, headers: { "content-type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const [created] = await db
|
|
.insert(modules)
|
|
.values({ id: body.id, nom: body.nom })
|
|
.returning();
|
|
|
|
return new Response(JSON.stringify(created), {
|
|
status: 201,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
};
|