Skip to content

Commit

Permalink
fix: optimisation de la requete des effectifs (#3957)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomarom authored Dec 27, 2024
1 parent 1071ea4 commit f8c3eb6
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions server/src/http/routes/specific.routes/organisme.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export async function getOrganismeEffectifs(
{} as Record<string, string[]>
);

const matchConditions = {
const matchOrgaAndAnneScolaire = (sifa: boolean) => ({
organisme_id: organismeId,
...(sifa && {
annee_scolaire: {
$in: getAnneesScolaireListFromDate(sifa ? getSIFADate(new Date()) : new Date()),
},
}),
});

const matchConditions = {
...Object.keys(parsedFilters).reduce((acc, key) => {
if (parsedFilters[key]?.length > 0) {
const fieldKey =
Expand Down Expand Up @@ -85,38 +88,41 @@ export async function getOrganismeEffectifs(
: { "formation.annee_scolaire": -1 };

const pipeline = [
{
$match: {
...matchOrgaAndAnneScolaire(sifa),
},
},
{
$facet: {
allFilters: [
{
$match: {
organisme_id: organismeId,
...(sifa && {
annee_scolaire: {
$in: getAnneesScolaireListFromDate(sifa ? getSIFADate(new Date()) : new Date()),
},
"_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] },
},
...(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,
],
},
-1,
],
},
},
},
},
},
}),
},
},
]
: []),
{
$group: {
_id: null,
Expand Down

0 comments on commit f8c3eb6

Please sign in to comment.