Skip to content

Commit

Permalink
fix: correction des listes nominatives (#3666)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Gaucher <[email protected]>
  • Loading branch information
Pomarom and Pomarom authored May 13, 2024
1 parent 549391c commit 312afcd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 62 deletions.
85 changes: 24 additions & 61 deletions server/src/common/actions/indicateurs/indicateurs.actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Collection, ObjectId } from "mongodb";
import {
Acl,
CODES_STATUT_APPRENANT,
IndicateursEffectifs,
IndicateursEffectifsAvecDepartement,
IndicateursEffectifsAvecFormation,
Expand Down Expand Up @@ -558,6 +557,22 @@ export async function getEffectifsNominatifsGenerique(
decaMode: boolean = false,
organismeId?: ObjectId
): Promise<IndicateursEffectifsAvecOrganisme[]> {
const computedType = (t: TypeEffectifNominatif) => {
switch (t) {
case "apprenant":
return ["APPRENTI", "INSCRIT", "RUPTURANT", "FIN_DE_FORMATION"];
case "apprenti":
return ["APPRENTI", "FIN_DE_FORMATION"];
case "inscritSansContrat":
return ["INSCRIT"];
case "rupturant":
return ["RUPTURANT"];
case "abandon":
return ["ABANDON"];
default:
return [t];
}
};
const indicateurs = (await db
.aggregate([
{
Expand All @@ -572,92 +587,40 @@ export async function getEffectifsNominatifsGenerique(
},
{
$addFields: {
"apprenant.historique_statut": {
"_computed.statut.parcours": {
$sortArray: {
input: {
$filter: {
input: "$apprenant.historique_statut",
input: "$_computed.statut.parcours",
as: "statut",
cond: {
$lte: ["$$statut.date_statut", filters.date],
$lte: ["$$statut.date", filters.date],
},
},
},
sortBy: { date_statut: 1 },
sortBy: { date: 1 },
},
},
},
},
{
$match: { "apprenant.historique_statut": { $not: { $size: 0 } } },
$match: { "_computed.statut.parcours": { $not: { $size: 0 } } },
},
{
$addFields: {
statut_apprenant_at_date: {
$last: "$apprenant.historique_statut",
$last: "$_computed.statut.parcours",
},
},
},
{
$addFields: {
statut: {
// pipeline commun entre statuts plutôt que $facet limité à 16Mo
$switch: {
branches: [
{
case: { $eq: ["$statut_apprenant_at_date.valeur_statut", CODES_STATUT_APPRENANT.apprenti] },
then: "apprenti" satisfies TypeEffectifNominatif,
},
{
case: { $eq: ["$statut_apprenant_at_date.valeur_statut", CODES_STATUT_APPRENANT.abandon] },
then: "abandon" satisfies TypeEffectifNominatif,
},
// l'ordre important ici, du statut le plus spécifique au plus générique
{
case: {
$and: [
{ $eq: ["$statut_apprenant_at_date.valeur_statut", CODES_STATUT_APPRENANT.inscrit] },
{
$eq: [
0,
{
$size: {
$filter: {
input: "$apprenant.historique_statut",
cond: {
$and: [
{ $eq: ["$$this.valeur_statut", CODES_STATUT_APPRENANT.apprenti] },
{ $lte: ["$$this.date_statut", filters.date] },
],
},
limit: 1,
},
},
},
],
},
],
},
then: "inscritSansContrat" satisfies TypeEffectifNominatif,
},
{
case: { $eq: ["$statut_apprenant_at_date.valeur_statut", CODES_STATUT_APPRENANT.inscrit] },
then: "rupturant" satisfies TypeEffectifNominatif,
},
],
default: "inconnu" satisfies TypeEffectifNominatif,
},
},
statut: "$statut_apprenant_at_date.valeur",
},
},
{
$match: {
statut:
type === "apprenant"
? {
$in: ["apprenti", "inscritSansContrat", "rupturant"] satisfies TypeEffectifNominatif[],
}
: { $eq: type },
statut: { $in: computedType(type) },
},
},
{
Expand Down
6 changes: 5 additions & 1 deletion server/tests/integration/http/indicateurs.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ describe("Route indicateurs", () => {
...createSampleEffectif({
...commonEffectifsAttributes,
annee_scolaire: anneeScolaire,
formation: {
date_entree: new Date("2020-11-15T00:00:00.00"),
date_fin: new Date(),
},
apprenant: {
historique_statut: historySequenceApprentiToAbandon,
},
Expand All @@ -344,7 +348,7 @@ describe("Route indicateurs", () => {
apprenant_date_de_naissance: effectif.apprenant.date_de_naissance?.toISOString().substring(0, 10),
apprenant_nom: effectif.apprenant.nom,
apprenant_prenom: effectif.apprenant.prenom,
apprenant_statut: "abandon",
apprenant_statut: "ABANDON",
formation_annee: effectif.formation?.annee,
formation_cfd: effectif.formation?.cfd,
formation_date_debut_formation: effectif.formation?.periode?.[0],
Expand Down

0 comments on commit 312afcd

Please sign in to comment.