From f27e23455f2cbfe02589e312a2a09edd1be32d1b Mon Sep 17 00:00:00 2001 From: folland87 Date: Tue, 19 Sep 2023 09:02:46 +0200 Subject: [PATCH] fixes annuaires --- src/api/annuaire/annuaire.routes.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/api/annuaire/annuaire.routes.js b/src/api/annuaire/annuaire.routes.js index 8b2b31ff..2421e01f 100644 --- a/src/api/annuaire/annuaire.routes.js +++ b/src/api/annuaire/annuaire.routes.js @@ -2,12 +2,14 @@ import express from 'express'; import { db } from '../../services/mongo.service'; const annuaire = db.collection('annuaire'); +const rt = db.collection('relationtypes'); const router = new express.Router(); const lightProjection = { _id: 0, id: 1, person: "$relatedObject.displayName", + personGender: "$relatedObject.gender", structureName: "$resource.displayName", startDate: 1, endDate: 1, @@ -15,16 +17,33 @@ const lightProjection = { active: 1, endDatePrevisional: 1, mandateEmail: 1, + mandateTemporary: 1, personalEmail: 1, mandatePhonenumber: 1, personId: "$relatedObject.id", structureId: "$resource.id", - relationType: "$relationType.name", + relationType: { + $cond: { + if: { $eq: ["$relatedObject.gender", 'Homme'] }, + then: "$relationType.maleName", + else: { + $cond: { + if: { $eq: ["$relatedObject.gender", 'Homme'] }, + then: "$relationType.feminineName", + else: "$relationType.name" + } + } + } + }, } router.get('/annuaire/aggregations', async (req, res) => { - const relationTypes = await annuaire.distinct('relationType.name'); + const relationTypesDocs = await rt + .find({ for: "persons" }) + .project({ _id: 0, name: 1, priority: { $ifNull: ['$priority', 99] } }) + .toArray(); + const relationTypes = relationTypesDocs.sort((a, b) => a.priority - b.priority).map(rt => rt.name); const structures = await annuaire.distinct('resource.displayName'); const categories = await annuaire.distinct('resource.categories.usualNameFr'); const mandateTypeGroups = await annuaire.distinct('relationType.mandateTypeGroup');