Skip to content

Commit

Permalink
fix: compute requiredSifa from js and not mongo (#3958)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomarom authored Dec 27, 2024
1 parent f8c3eb6 commit 2f6a6f9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 66 deletions.
122 changes: 60 additions & 62 deletions server/src/http/routes/specific.routes/organisme.routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ObjectId } from "bson";
import { compact, get } from "lodash-es";
import {
STATUT_APPRENANT,
getAnneesScolaireListFromDate,
Expand Down Expand Up @@ -52,6 +53,36 @@ export async function getOrganismeEffectifs(
}),
});

const addSifaFilter = (sifa: boolean) => {
return sifa
? [
{
$addFields: {
dernierStatut: {
$arrayElemAt: [
{
$filter: {
input: "$_computed.statut.parcours",
as: "statut",
cond: {
$lte: ["$$statut.date", currentDate],
},
},
},
-1,
],
},
},
},
{
$match: {
"dernierStatut.valeur": STATUT_APPRENANT.APPRENTI,
},
},
]
: [];
};

const matchConditions = {
...Object.keys(parsedFilters).reduce((acc, key) => {
if (parsedFilters[key]?.length > 0) {
Expand Down Expand Up @@ -87,42 +118,17 @@ export async function getOrganismeEffectifs(
? { [fieldMap[sortField] || sortField]: sortOrder === "desc" ? -1 : 1 }
: { "formation.annee_scolaire": -1 };

const currentDate = sifa ? getSIFADate(new Date()) : new Date();
const pipeline = [
{
$match: {
...matchOrgaAndAnneScolaire(sifa),
},
},
...addSifaFilter(sifa),
{
$facet: {
allFilters: [
...(sifa
? [
{
$match: {
"_computed.statut.parcours": {
$elemMatch: {
valeur: STATUT_APPRENANT.APPRENTI,
date: {
$eq: {
$arrayElemAt: [
{
$filter: {
input: "$_computed.statut.parcours",
as: "parcours",
cond: { $eq: ["$$parcours.valeur", STATUT_APPRENANT.APPRENTI] },
},
},
-1,
],
},
},
},
},
},
},
]
: []),
{
$group: {
_id: null,
Expand All @@ -147,40 +153,6 @@ export async function getOrganismeEffectifs(
{ $sort: sortConditions },
{ $skip: pageIndex * pageSize },
{ $limit: pageSize },
{
$project: {
id: { $toString: "$_id" },
id_erp_apprenant: 1,
organisme_id: 1,
annee_scolaire: 1,
source: 1,
validation_errors: 1,
formation: 1,
nom: "$apprenant.nom",
prenom: "$apprenant.prenom",
date_de_naissance: "$apprenant.date_de_naissance",
historique_statut: "$apprenant.historique_statut",
statut: "$_computed.statut",
...(sifa
? {
requiredSifa: {
$filter: {
input: {
$setUnion: [
requiredFieldsSifa,
{
$cond: [{ $not: "$apprenant.adresse.complete" }, requiredApprenantAdresseFieldsSifa, []],
},
],
},
as: "fieldName",
cond: { $or: [{ $not: { $ifNull: ["$$fieldName", false] } }] },
},
},
}
: {}),
},
},
],
totalCount: [
{ $match: matchConditions },
Expand All @@ -206,11 +178,37 @@ export async function getOrganismeEffectifs(

const [data] = await db.aggregate(pipeline).toArray();

const effectifs = data?.results.map((effectif) => ({
id: effectif._id.toString(),
id_erp_apprenant: effectif.id_erp_apprenant,
organisme_id: organismeId,
annee_scolaire: effectif.annee_scolaire,
source: effectif.source,
validation_errors: effectif.validation_errors,
formation: effectif.formation,
nom: effectif.apprenant.nom,
prenom: effectif.apprenant.prenom,
date_de_naissance: effectif.apprenant.date_de_naissance,
historique_statut: effectif.apprenant.historique_statut,
statut: effectif._computed?.statut,
...(sifa
? {
requiredSifa: compact(
[
...(!effectif.apprenant.adresse?.complete
? [...requiredFieldsSifa, ...requiredApprenantAdresseFieldsSifa]
: requiredFieldsSifa),
].map((fieldName) => (!get(effectif, fieldName) || get(effectif, fieldName) === "" ? fieldName : undefined))
),
}
: {}),
}));

return {
fromDECA: isDeca,
total: data?.total || 0,
filters: data?.filters || {},
organismesEffectifs: data?.results || [],
organismesEffectifs: effectifs || [],
};
}

Expand Down
4 changes: 2 additions & 2 deletions ui/modules/mon-espace/SIFA/SIFAPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ function SIFAPage(props: SIFAPageProps) {
const { fromDECA, total, filters: returnedFilters, organismesEffectifs } = response;

setCurrentEffectifsState(
organismesEffectifs.reduce((acc, { id, validation_errors }) => {
acc.set(id, { validation_errors, requiredSifa: [] });
organismesEffectifs.reduce((acc, { id, validation_errors, requiredSifa }) => {
acc.set(id, { validation_errors, requiredSifa });
return acc;
}, new Map())
);
Expand Down
4 changes: 2 additions & 2 deletions ui/modules/mon-espace/SIFA/SIFATable/SIFAEffectifsColumns.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Alert, Box, HStack, ListItem, Text, UnorderedList, VStack } from "@chakra-ui/react";
import { Box, HStack, ListItem, Text, UnorderedList, VStack } from "@chakra-ui/react";
import { DateTime } from "luxon";
import { useRecoilValue } from "recoil";
import { getStatut } from "shared";

import { capitalizeWords } from "@/common/utils/stringUtils";
import { InfoTooltip } from "@/components/Tooltip/InfoTooltip";
import { ValidateIcon } from "@/theme/components/icons";
import { ValidateIcon, Alert } from "@/theme/components/icons";

import { effectifStateSelector } from "../../effectifs/engine/formEngine/atoms";

Expand Down

0 comments on commit 2f6a6f9

Please sign in to comment.