Skip to content

Commit

Permalink
annuaire
Browse files Browse the repository at this point in the history
  • Loading branch information
folland87 committed Sep 14, 2023
1 parent bd3f8be commit e8b5fce
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/api/annuaire/annuaire.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express';
import { db } from '../../services/mongo.service';

const annuaire = db.collection('annuaire');
const router = new express.Router();

router.get('/annuaire', async (req, res) => {
const { relationType, structure, category, mandateTypeGroup, keepInactive, skip = "0", limit = "0" } = req.query;
const filters = {
...(relationType && { relationType }),
...(structure && { structureName: structure }),
...(category && { category }),
...(mandateTypeGroup && { mandateTypeGroup }),
};
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()
: [];
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 });
});

export default router;
2 changes: 2 additions & 0 deletions src/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import YAML from 'yamljs';
import { authenticate } from './commons/middlewares/authenticate.middlewares';
import { handleErrors } from './commons/middlewares/handle-errors.middlewares';

import annuaireRoutes from './annuaire/annuaire.routes';
import apiKeysRoutes from './apikeys/apikeys.routes';
import assetsRoutes from './assets/assets.routes';
import authRoutes from './auth/auth.routes';
Expand Down Expand Up @@ -96,6 +97,7 @@ app.use(requireAuth);
app.use(forbidReadersToWrite);

// Register api routes
app.use(annuaireRoutes);
app.use(apiKeysRoutes);
app.use(authRoutes);
app.use(assetsRoutes);
Expand Down
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const production = {
jwtSecret: process.env.JWT_SECRET,
defaultAccountConfirmation: false,
totpWindow: [20, 0],
accessTokenExpiresIn: '10d',
refreshTokenExpiresIn: '20d',
accessTokenExpiresIn: process.env.ACCESS_TOKEN_EXPRIES_IN || '10d',
refreshTokenExpiresIn: process.env.REFRESH_TOKEN_EXPRIES_IN || '20d',
otpHeader: 'x-paysage-otp',
otpMethodHeader: 'x-paysage-otp-method',
systemName: 'paysage',
Expand Down
2 changes: 2 additions & 0 deletions src/jobs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from './opendata';
import synchronizeFrEsrReferentielGeographique from './synchronize/fr-esr-referentiel-geographique';
import synchronizeCuriexploreActors from './synchronize/curiexplore-actors';
import synchronizeAnnuaireCollection from './synchronize/annuaire-collection';
import askForEmailRevalidation from './ask-for-email-validation';
import deletePassedGouvernancePersonnalInformation from './treatments/delete-passed-gouvernance-personal-infos';

Expand All @@ -40,6 +41,7 @@ agenda.define('synchronize fr-esr-referentiel-geographique', { shouldSaveResult:
agenda.define('synchronize curiexplore actors', { shouldSaveResult: true }, synchronizeCuriexploreActors);
agenda.define('ask for email revalidation with otp', { shouldSaveResult: true }, askForEmailRevalidation);
agenda.define('delete passed gouvernance personal info', { shouldSaveResult: true }, deletePassedGouvernancePersonnalInformation);
agenda.define('Syncronize governance collection', { shouldSaveResult: true }, synchronizeAnnuaireCollection);

agenda
.on('ready', () => { logger.info('Agenda connected to mongodb'); })
Expand Down
82 changes: 82 additions & 0 deletions src/jobs/synchronize/annuaire-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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';

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' } },
{ $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,
},
},
{ $out: 'annuaire' },
]).toArray().catch((e) => {
job.fail(`La synchronisation a échouée: ${e.message}`);
return null;
});
return { ok: 1 }
}

0 comments on commit e8b5fce

Please sign in to comment.