diff --git a/src/api/annuaire/annuaire.routes.js b/src/api/annuaire/annuaire.routes.js index 2bd1b25b..8039e9ce 100644 --- a/src/api/annuaire/annuaire.routes.js +++ b/src/api/annuaire/annuaire.routes.js @@ -4,24 +4,59 @@ import { db } from '../../services/mongo.service'; const annuaire = db.collection('annuaire'); const router = new express.Router(); +const lightProjection = { + _id: 0, + id: 1, + person: "$relatedObject.displayName", + structureName: "$resource.displayName", + startDate: 1, + endDate: 1, + status: 1, + active: 1, + previsionalEndDate: 1, + mandateEmail: 1, + personalEmail: 1, + mandatePhonenumber: 1, + personId: "$relatedObject.id", + structureId: "resource.id", + relationType: "$relationType.name", +} + + +router.get('/annuaire/aggregations', async (req, res) => { + const relationTypes = await annuaire.distinct('relationType.name'); + const structures = await annuaire.distinct('resource.displayName'); + const categories = await annuaire.distinct('resource.categories.usualNameFr'); + const mandateTypeGroups = await annuaire.distinct('relationType.mandateTypeGroup'); + return res.json({ relationTypes, structures, categories, mandateTypeGroups }); +}); + router.get('/annuaire', async (req, res) => { - const { relationType, structure, category, mandateTypeGroup, keepInactive, skip = "0", limit = "0" } = req.query; + const { relationType, structure, category, mandateTypeGroup, skip = "0", limit = "0" } = req.query; const filters = { - ...(relationType && { relationType }), - ...(structure && { structureName: structure }), - ...(category && { category }), - ...(mandateTypeGroup && { mandateTypeGroup }), + ...(relationType && { "relationType.name": { $in: relationType.split(',') } }), + ...(structure && { "resource.displayName": { $in: structure.split(',') } }), + ...(category && { "resource.categories.usualNameFr": { $in: category.split(',') } }), + ...(mandateTypeGroup && { "relationType.mandateTypeGroup": { $in: mandateTypeGroup.split(',') } }), }; - filters.active = keepInactive === 'true' ? { $in: [true, false] } : true; - console.log(filters); const data = (parseInt(limit, 10) > 0) - ? await annuaire.find(filters).skip(parseInt(skip, 10)).limit(parseInt(limit, 10)).toArray() + ? await annuaire.find(filters).project(lightProjection).skip(parseInt(skip, 10)).limit(parseInt(limit, 10)).toArray() : []; - const relationTypes = await annuaire.distinct('relationType', filters); - const structures = await annuaire.distinct('structureName', filters); - const categories = await annuaire.distinct('category', filters); - const mandateTypeGroups = await annuaire.distinct('mandateTypeGroup', filters); - return res.json({ data, relationTypes, structures, categories, mandateTypeGroups }); + const totalCount = await annuaire.countDocuments(filters); + return res.json({ data, totalCount }); +}); + +router.get('/annuaire/export', async (req, res) => { + const { relationType, structure, category, mandateTypeGroup } = req.query; + const filters = { + ...(relationType && { "relationType.name": { $in: relationType.split(',') } }), + ...(structure && { "resource.displayName": { $in: structure.split(',') } }), + ...(category && { "resource.categories.usualNameFr": { $in: category.split(',') } }), + ...(mandateTypeGroup && { "relationType.mandateTypeGroup": { $in: mandateTypeGroup.split(',') } }), + }; + const data = await annuaire.find(filters).toArray() + console.log(data); + return res.json(data); }); export default router; \ No newline at end of file diff --git a/src/jobs/synchronize/annuaire-collection.js b/src/jobs/synchronize/annuaire-collection.js index eb0f0f5c..6e7ab5db 100644 --- a/src/jobs/synchronize/annuaire-collection.js +++ b/src/jobs/synchronize/annuaire-collection.js @@ -1,78 +1,23 @@ import { db } from '../../services/mongo.service'; -import structuresLightQuery from '../../api/commons/queries/structures.light.query'; -import personLightQuery from '../../api/commons/queries/persons.light.query'; -import relationTypesLightQuery from '../../api/commons/queries/relation-types.light.query'; +import relationsQuery from '../../api/commons/queries/relations.query'; + export default async function synchronizeAnnuaireCollection(job) { await db.collection('relationships') .aggregate([ { $match: { relationTag: 'gouvernance' } }, - { - $lookup: { - from: 'persons', - localField: 'relatedObjectId', - foreignField: 'id', - pipeline: personLightQuery, - as: 'persons', - }, - }, - { - $lookup: { - from: 'structures', - localField: 'resourceId', - foreignField: 'id', - pipeline: structuresLightQuery, - as: 'structures', - }, - }, - { $set: { structures: { $arrayElemAt: ['$structures', 0] } } }, - { $set: { persons: { $arrayElemAt: ['$persons', 0] } } }, - { $set: { category: '$structures.category.usualNameFr' } }, - { $set: { structureId: '$structures.id' } }, - { $set: { structureName: '$structures.displayName' } }, - { $set: { person: '$persons.displayName' } }, - { $set: { personId: '$persons.id' } }, - { $set: { gender: '$persons.gender' } }, - { - $lookup: { - from: 'relationtypes', - localField: 'relationTypeId', - foreignField: 'id', - pipeline: relationTypesLightQuery, - as: 'relationType', - }, - }, - { $set: { relationType: { $arrayElemAt: ['$relationType', 0] } } }, - { $set: { mandateTypeGroup: '$relationType.mandateTypeGroup' } }, - { $set: { relationType: '$relationType.name' } }, + ...relationsQuery, { $set: { endDate: { $ifNull: ['$endDate', null] } } }, - { $set: { active: { $ifNull: ['$active', false] } } }, - { $set: { active: { $or: [{ $eq: ['$active', true] }, { $eq: ['$endDate', null] }, { $gte: ['$endDate', new Date().toISOString().split('T')[0]] }] } } }, - { - $project: { - _id: 0, - id: 1, - person: 1, - personId: 1, - structureId: 1, - gender: 1, - category: 1, - structureName: 1, - relationType: 1, - mandateTypeGroup: 1, - startDate: { $ifNull: ['$startDate', null] }, - endDate: { $ifNull: ['$endDate', null] }, - endDatePrevisional: { $ifNull: ['$endDatePrevisional', null] }, - mandatePosition: { $ifNull: ['$mandatePosition', null] }, - mandateReason: { $ifNull: ['$mandateReason', null] }, - mandateEmail: { $ifNull: ['$mandateEmail', null] }, - personalEmail: { $ifNull: ['$personalEmail', null] }, - mandatePhonenumber: { $ifNull: ['$mandatePhonenumber', null] }, - mandateTemporary: { $ifNull: ['$mandateTemporary', null] }, - mandatePrecision: { $ifNull: ['$mandatePrecision', null] }, - active: 1, - }, - }, + // { $set: { isActive: { $ifNull: ['$active', false] } } }, + // { $set: { isActive: { $or: [{ $eq: ['$sActive', true] }, { $eq: ['$endDate', null] }, { $gte: ['$endDate', new Date().toISOString().split('T')[0]] }] } } }, + // { $set: { forthcomming: { $gte: ['$startDate', new Date().toISOString().split('T')[0]] } } }, + // { $set: { status: { $cond: [{ $eq: ['$forthcomming', true] }, 'forthcomming', { $cond: [{ $eq: ['$isActive', false] }, 'inactive', 'current'] }] } } }, + // { + // $project: { + // _id: 0, + // isActive: 0, + // }, + // }, { $out: 'annuaire' }, ]).toArray().catch((e) => { job.fail(`La synchronisation a échouée: ${e.message}`);