feat: améliorer inscription et export apprenants
Some checks failed
Validation applicative / TypeScript, tests et build (push) Failing after 2m21s
Some checks failed
Validation applicative / TypeScript, tests et build (push) Failing after 2m21s
This commit is contained in:
51
server/db.ts
51
server/db.ts
@@ -381,6 +381,57 @@ export async function getApprenants() {
|
||||
return apprenantsWithInscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne une ligne par apprenant avec les formations non annulées auxquelles il est inscrit.
|
||||
* La requête joint les tables en une fois afin d'éviter une requête par apprenant à l'export.
|
||||
*/
|
||||
export async function getApprenantsWithFormationNames() {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: apprenants.id,
|
||||
nom: apprenants.nom,
|
||||
prenom: apprenants.prenom,
|
||||
formationNom: formations.nom,
|
||||
inscriptionStatut: inscriptions.statut,
|
||||
})
|
||||
.from(apprenants)
|
||||
.leftJoin(inscriptions, eq(inscriptions.apprenantId, apprenants.id))
|
||||
.leftJoin(sequences, eq(inscriptions.sequenceId, sequences.id))
|
||||
.leftJoin(formations, eq(sequences.formationId, formations.id));
|
||||
|
||||
const byApprenant = new Map<number, { nom: string; prenom: string; formations: Set<string> }>();
|
||||
for (const row of rows) {
|
||||
const entry = byApprenant.get(row.id) ?? {
|
||||
nom: row.nom,
|
||||
prenom: row.prenom,
|
||||
formations: new Set<string>(),
|
||||
};
|
||||
|
||||
if (row.inscriptionStatut !== "annulee" && row.formationNom) {
|
||||
entry.formations.add(row.formationNom);
|
||||
}
|
||||
byApprenant.set(row.id, entry);
|
||||
}
|
||||
|
||||
return [...byApprenant.values()]
|
||||
.map((apprenant) => ({
|
||||
nom: apprenant.nom,
|
||||
prenom: apprenant.prenom,
|
||||
formationSuivie: [...apprenant.formations]
|
||||
.sort((left, right) => left.localeCompare(right, "fr", { sensitivity: "base" }))
|
||||
.join(" | "),
|
||||
}))
|
||||
.sort((left, right) => {
|
||||
const nomOrder = left.nom.localeCompare(right.nom, "fr", { sensitivity: "base" });
|
||||
return nomOrder !== 0
|
||||
? nomOrder
|
||||
: left.prenom.localeCompare(right.prenom, "fr", { sensitivity: "base" });
|
||||
});
|
||||
}
|
||||
|
||||
export async function getApprenantsByFormateur(formateurId: number) {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
|
||||
Reference in New Issue
Block a user