-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: suppression de buildMongoFilters (#3463)
- Loading branch information
Showing
9 changed files
with
288 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
server/src/common/actions/indicateurs/effectifs/effectifs-filters.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { strict as assert } from "assert"; | ||
|
||
import { buildEffectifMongoFilters } from "./effectifs-filters"; | ||
|
||
const currentDate = new Date("2023-02-14T10:00:00Z"); | ||
|
||
describe("Filtres Indicateurs", () => { | ||
describe("buildEffectifMongoFilters()", () => { | ||
it("Gère le filtre date uniquement", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
}); | ||
}); | ||
|
||
it("Gère le filtre organisme_departements", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
organisme_departements: ["56"], | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
"_computed.organisme.departement": { | ||
$in: ["56"], | ||
}, | ||
}); | ||
}); | ||
|
||
it("Gère le filtre organisme_regions", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
organisme_regions: ["25"], | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
"_computed.organisme.region": { | ||
$in: ["25"], | ||
}, | ||
}); | ||
}); | ||
|
||
it("Gère le filtre organisme_reseaux", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
organisme_reseaux: ["AGRI"], | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
"_computed.organisme.reseaux": { | ||
$in: ["AGRI"], | ||
}, | ||
}); | ||
}); | ||
|
||
it("Gère le filtre formation_cfds", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
formation_cfds: ["25021000"], | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
"formation.cfd": { | ||
$in: ["25021000"], | ||
}, | ||
}); | ||
}); | ||
|
||
it("Gère le filtre formation_niveaux", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
formation_niveaux: ["2"], | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
"formation.niveau": { | ||
$in: ["2"], | ||
}, | ||
}); | ||
}); | ||
|
||
it("Gère tous les filtres en même temps", () => { | ||
const stages = buildEffectifMongoFilters({ | ||
date: currentDate, | ||
organisme_regions: ["25"], | ||
organisme_departements: ["56"], | ||
organisme_reseaux: ["AGRI"], | ||
formation_cfds: ["25021000"], | ||
formation_niveaux: ["2"], | ||
}); | ||
assert.deepStrictEqual(stages, { | ||
annee_scolaire: { | ||
$in: ["2023-2023", "2022-2023"], | ||
}, | ||
"_computed.organisme.region": { | ||
$in: ["25"], | ||
}, | ||
"_computed.organisme.departement": { | ||
$in: ["56"], | ||
}, | ||
"_computed.organisme.reseaux": { | ||
$in: ["AGRI"], | ||
}, | ||
"formation.cfd": { | ||
$in: ["25021000"], | ||
}, | ||
"formation.niveau": { | ||
$in: ["2"], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
server/src/common/actions/indicateurs/effectifs/effectifs-filters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { subYears } from "date-fns"; | ||
import { Filter } from "mongodb"; | ||
import { assertUnreachable, entries, getAnneesScolaireListFromDate } from "shared"; | ||
|
||
import { SIRET_REGEX } from "@/common/constants/validations"; | ||
import { Effectif } from "@/common/model/@types"; | ||
import { escapeRegExp } from "@/common/utils/regexUtils"; | ||
import { isValidUAI } from "@/common/utils/validationUtils"; | ||
|
||
import { FullEffectifsFilters } from "../../helpers/filters"; | ||
|
||
// [min, max[ | ||
const intervalParTrancheAge = { | ||
"-18": [0, 18], | ||
"18-20": [18, 21], | ||
"21-25": [21, 26], | ||
"26+": [26, 999], | ||
}; | ||
|
||
function buildOrganismeSearchCondition(value: string) { | ||
if (isValidUAI(value)) { | ||
return [{ "_computed.organisme.uai": value }]; | ||
} | ||
if (SIRET_REGEX.test(value)) { | ||
return [{ "_computed.organisme.siret": value }]; | ||
} | ||
if (/^\d{3,}$/.test(value)) { | ||
return [{ "_computed.organisme.siret": new RegExp(escapeRegExp(value)) }]; | ||
} | ||
return [{ "_computed.organisme.nom": new RegExp(escapeRegExp(value)) }]; // TODO probablement ajouter un champ nom (enseigne + raison_sociale) de l'organisme | ||
} | ||
|
||
export function buildEffectifMongoFilters(filters: FullEffectifsFilters): Filter<Effectif> { | ||
return entries(filters).reduce((acc: Filter<Effectif>, [key, value]) => { | ||
switch (key) { | ||
case "date": | ||
acc["annee_scolaire"] = { $in: getAnneesScolaireListFromDate(value) }; | ||
break; | ||
case "organisme_regions": | ||
acc["_computed.organisme.region"] = { $in: value }; | ||
break; | ||
case "organisme_departements": | ||
acc["_computed.organisme.departement"] = { $in: value }; | ||
break; | ||
case "organisme_academies": | ||
acc["_computed.organisme.academie"] = { $in: value }; | ||
break; | ||
case "organisme_bassinsEmploi": | ||
acc["_computed.organisme.bassinEmploi"] = { $in: value }; | ||
break; | ||
case "organisme_search": | ||
acc["$or"] = buildOrganismeSearchCondition(value); | ||
break; | ||
case "organisme_reseaux": | ||
acc["_computed.organisme.reseaux"] = { $in: value }; | ||
break; | ||
case "apprenant_tranchesAge": | ||
acc["$or"] = value.map((key) => { | ||
const [min, max] = intervalParTrancheAge[key]; | ||
return { | ||
"apprenant.date_de_naissance": { | ||
$lt: subYears(new Date(), min), | ||
$gte: subYears(new Date(), max), | ||
}, | ||
}; | ||
}); | ||
break; | ||
case "formation_annees": | ||
acc["formation.annee"] = { $in: value }; | ||
break; | ||
case "formation_niveaux": | ||
acc["formation.niveau"] = { $in: value }; | ||
break; | ||
case "formation_cfds": | ||
acc["formation.cfd"] = { $in: value }; | ||
break; | ||
case "formation_secteursProfessionnels": | ||
acc["_computed.formation.codes_rome"] = { $in: value }; | ||
break; | ||
default: | ||
assertUnreachable(key); | ||
} | ||
|
||
return acc; | ||
}, {}); | ||
} |
Oops, something went wrong.