66183c2ad8
- PUT /ue-modules/{idModule}/{idUE}/{idPromo}: update coeff for
UE-Module-Promo association
- DELETE /ue-modules/{idModule}/{idUE}/{idPromo}: remove UE-Module-Promo
association
- requires employee role
137 lines
3.7 KiB
TypeScript
137 lines
3.7 KiB
TypeScript
import { FreshContext, Handlers } from "$fresh/server.ts";
|
|
import { db } from "$root/databases/db.ts";
|
|
import { ueModules } from "$root/databases/schema.ts";
|
|
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
|
import { and, eq } from "npm:drizzle-orm@0.45.2";
|
|
|
|
const NOT_FOUND = new Response(
|
|
JSON.stringify({ error: "Association UE-Module introuvable" }),
|
|
{ status: 404, headers: { "content-type": "application/json" } },
|
|
);
|
|
|
|
const FORBIDDEN = new Response(null, { status: 403 });
|
|
|
|
const BAD_REQUEST = new Response(
|
|
JSON.stringify({ error: "Paramètres invalides" }),
|
|
{ status: 400, headers: { "content-type": "application/json" } },
|
|
);
|
|
|
|
export const handler: Handlers<null, AuthenticatedState> = {
|
|
// #39 GET /ue-modules/{idModule}/{idUE}/{idPromo}
|
|
async GET(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
return FORBIDDEN;
|
|
}
|
|
|
|
const idModule = context.params.idModule;
|
|
const idUE = Number(context.params.idUE);
|
|
const idPromo = context.params.idPromo;
|
|
|
|
if (isNaN(idUE)) {
|
|
return BAD_REQUEST;
|
|
}
|
|
|
|
const ueModuleAssociation = await db
|
|
.select()
|
|
.from(ueModules)
|
|
.where(
|
|
eq(ueModules.idModule, idModule),
|
|
eq(ueModules.idUE, idUE),
|
|
eq(ueModules.idPromo, idPromo),
|
|
)
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!ueModuleAssociation) return NOT_FOUND;
|
|
|
|
return new Response(JSON.stringify(ueModuleAssociation), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #40 PUT /ue-modules/{idModule}/{idUE}/{idPromo}
|
|
async PUT(
|
|
request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
return FORBIDDEN;
|
|
}
|
|
|
|
const idModule = context.params.idModule;
|
|
const idUE = Number(context.params.idUE);
|
|
const idPromo = context.params.idPromo;
|
|
|
|
if (isNaN(idUE)) {
|
|
return BAD_REQUEST;
|
|
}
|
|
|
|
const body: { coeff: number } = await request.json();
|
|
|
|
if (typeof body.coeff !== "number") {
|
|
return new Response(
|
|
JSON.stringify({ error: "Le champ 'coeff' doit être un nombre" }),
|
|
{ status: 400, headers: { "content-type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const [updated] = await db
|
|
.update(ueModules)
|
|
.set({ coeff: body.coeff })
|
|
.where(
|
|
and(
|
|
eq(ueModules.idModule, idModule),
|
|
eq(ueModules.idUE, idUE),
|
|
eq(ueModules.idPromo, idPromo),
|
|
),
|
|
)
|
|
.returning();
|
|
|
|
if (!updated) return NOT_FOUND;
|
|
|
|
return new Response(JSON.stringify({
|
|
idModule: updated.idModule,
|
|
idUE: updated.idUE,
|
|
idPromo: updated.idPromo,
|
|
coeff: updated.coeff,
|
|
}), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #41 DELETE /ue-modules/{idModule}/{idUE}/{idPromo}
|
|
async DELETE(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
return FORBIDDEN;
|
|
}
|
|
|
|
const idModule = context.params.idModule;
|
|
const idUE = Number(context.params.idUE);
|
|
const idPromo = context.params.idPromo;
|
|
|
|
if (isNaN(idUE)) {
|
|
return BAD_REQUEST;
|
|
}
|
|
|
|
const [deleted] = await db
|
|
.delete(ueModules)
|
|
.where(
|
|
and(
|
|
eq(ueModules.idModule, idModule),
|
|
eq(ueModules.idUE, idUE),
|
|
eq(ueModules.idPromo, idPromo),
|
|
),
|
|
)
|
|
.returning();
|
|
|
|
if (!deleted) return NOT_FOUND;
|
|
|
|
return new Response(null, { status: 204 });
|
|
},
|
|
};
|