22750ba07e
- DELETE /ajustements/{numEtud}/{idUE}: remove ajustement from DB
- Requires employee role
- Returns 204 on success
108 lines
3.1 KiB
TypeScript
108 lines
3.1 KiB
TypeScript
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<null, AuthenticatedState> = {
|
|
// #50 GET /ajustements/{numEtud}/{idUE}
|
|
async GET(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
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" },
|
|
});
|
|
},
|
|
|
|
// #51 PUT /ajustements/{numEtud}/{idUE}
|
|
async PUT(
|
|
request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
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 body: { valeur: number } = await request.json();
|
|
|
|
if (body.valeur === undefined) {
|
|
return new Response(JSON.stringify({ error: "Champ requis: valeur" }), {
|
|
status: 400,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
const [updated] = await db
|
|
.update(ajustements)
|
|
.set({ valeur: body.valeur })
|
|
.where(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE))
|
|
.returning();
|
|
|
|
if (!updated) return NOT_FOUND;
|
|
|
|
return new Response(JSON.stringify(updated), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #52 DELETE /ajustements/{numEtud}/{idUE}
|
|
async DELETE(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
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 [deleted] = await db
|
|
.delete(ajustements)
|
|
.where(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE))
|
|
.returning();
|
|
|
|
if (!deleted) return NOT_FOUND;
|
|
|
|
return new Response(null, { status: 204 });
|
|
},
|
|
};
|