From 73f6745da646dcc32b65c3b7046f7a6aba955420 Mon Sep 17 00:00:00 2001 From: William Belle Date: Tue, 7 Nov 2023 11:57:09 +0100 Subject: [PATCH] Add people (Unit API) (#108) --- src/services/people.service.js | 7 ++- src/services/unit.service.js | 7 ++- src/utils/ldap.util.js | 61 ++++++++++++++++++- tests/resources/cadidb/getUnit-mandalore.json | 4 +- tests/resources/cadidb/getUnit-nevarro.json | 20 ++++++ .../resources/cadidb/getUnitPath-nevarro.json | 22 +++++++ .../resources/cadidb/searchUnits-nevarro.json | 8 +++ tests/resources/unit/unit-mandalore-en.json | 34 +++++++++-- tests/resources/unit/unit-mandalore-fr.json | 34 +++++++++-- tests/resources/unit/unit-nevarro-fr.json | 39 ++++++++++++ tests/unit.test.js | 26 ++++++++ 11 files changed, 246 insertions(+), 16 deletions(-) create mode 100644 tests/resources/cadidb/getUnit-nevarro.json create mode 100644 tests/resources/cadidb/getUnitPath-nevarro.json create mode 100644 tests/resources/cadidb/searchUnits-nevarro.json create mode 100644 tests/resources/unit/unit-nevarro-fr.json diff --git a/src/services/people.service.js b/src/services/people.service.js index ecbe8d8..5b74598 100644 --- a/src/services/people.service.js +++ b/src/services/people.service.js @@ -33,9 +33,14 @@ function getPersonByName (name) { return getPerson(ldapQuery); } +function getPersonByUnit (unit) { + return getPerson(`(ou=${unit})`); +} + module.exports = { getPersonByEmail, getPersonByName, getPersonByPhone, - getPersonBySciper + getPersonBySciper, + getPersonByUnit }; diff --git a/src/services/unit.service.js b/src/services/unit.service.js index 7780352..8f10797 100644 --- a/src/services/unit.service.js +++ b/src/services/unit.service.js @@ -120,8 +120,11 @@ async function getUnit (acro, lang) { unitFullDetails.url = dict.url; } if (dict.has_accreds) { - // TODO: Get people from ldap - + const ldapUnitPersons = await peopleService.getPersonByUnit(dict.sigle); + const UnitPersons = ldapUtil.ldapUnit2api(ldapUnitPersons, lang); + if (UnitPersons.length > 0) { + unitFullDetails.people = UnitPersons; + } } else { unitFullDetails.subunits = await getSubunits(dict.id_unite, lang); } diff --git a/src/utils/ldap.util.js b/src/utils/ldap.util.js index 8e3b653..fdb80df 100644 --- a/src/utils/ldap.util.js +++ b/src/utils/ldap.util.js @@ -37,6 +37,23 @@ function newLdapAddressMapper () { return ldapAddressMapper; } +function newLdapUnitMapper (lang) { + const ldapUnitMapper = { + mail: ['email', (val) => val[0]], + sn: ['name', (val) => val], + givenName: ['firstname', (val) => val], + displayName: ['displayName', (val) => val[0]], + roomNumber: ['officeList', (val) => val], + telephoneNumber: ['phoneList', (val) => val] + }; + if (lang === 'fr') { + ldapUnitMapper.description = ['position', (val) => val[0]]; + } else { + ldapUnitMapper['description;lang-en'] = ['position', (val) => val[0]]; + } + return ldapUnitMapper; +} + function sortAccreds (obj) { return obj.sort((a, b) => a.rank - b.rank); } @@ -281,6 +298,47 @@ function ldapAddress2api (ldapResults) { return person; } +/** + * Convert LDAP Unit result into API result. + * + * @example + * const ldapUtil = require('../utils/ldap.util'); + * const persons = ldapUtil.ldapUnit2api(ldapResults, 'en'); + * + * @param {object} ldapResults The result from the LDAP Unit search. + * @param {string} hl The user interface language. + * @returns {object} Return the result for the API. + */ +function ldapUnit2api (ldapResults, hl) { + const list = []; + const ldapUnitMapper = newLdapUnitMapper(hl); + + for (const [sciper, entry] of Object.entries(ldapResults)) { + const person = { sciper, rank: 0 }; + for (let acc = 0; acc < entry.length; acc++) { + for (let att = 0; att < entry[acc].attributes.length; att++) { + if (entry[acc].attributes[att].type in ldapUnitMapper) { + person[ldapUnitMapper[entry[acc].attributes[att].type][0]] = + ldapUnitMapper[ + entry[acc].attributes[att].type + ][1](entry[acc].attributes[att].values); + } + } + } + const correctName = getCorrectName( + person.firstname, + person.name, + person.displayName + ); + person.firstname = correctName[0]; + person.name = correctName[1]; + delete person.displayName; + person.profile = getProfile(person.email, sciper); + list.push(person); + } + return sortPersons(list, ''); +} + module.exports = { buildLdapQueryForPerson, dn2acronym, @@ -288,5 +346,6 @@ module.exports = { getCorrectName, getProfile, ldap2api, - ldapAddress2api + ldapAddress2api, + ldapUnit2api }; diff --git a/tests/resources/cadidb/getUnit-mandalore.json b/tests/resources/cadidb/getUnit-mandalore.json index 89d5fb0..1991f99 100644 --- a/tests/resources/cadidb/getUnit-mandalore.json +++ b/tests/resources/cadidb/getUnit-mandalore.json @@ -5,8 +5,8 @@ "libelle": "Mandalore est une planète située dans les territoires de la bordure extérieure de la galaxie.", "libelle_en": "", "hierarchie": "EPFL SO TV-3 MANDALORE", - "resp_sciper": "670005", - "resp_nom": "Grogu", + "resp_sciper": "670003", + "resp_nom": "Djarin", "resp_nom_usuel": null, "resp_prenom": "Din", "resp_prenom_usuel": null, diff --git a/tests/resources/cadidb/getUnit-nevarro.json b/tests/resources/cadidb/getUnit-nevarro.json new file mode 100644 index 0000000..6b25a99 --- /dev/null +++ b/tests/resources/cadidb/getUnit-nevarro.json @@ -0,0 +1,20 @@ +[ + { + "sigle": "NEVARRO", + "id_unite": 10111, + "libelle": "Nevarro est une planète volcanique située dans les territoires de la bordure extérieure de la galaxie.", + "libelle_en": "", + "hierarchie": "EPFL SO TV-3 NEVARRO", + "resp_sciper": "670003", + "resp_nom": "Djarin", + "resp_nom_usuel": null, + "resp_prenom": "Din", + "resp_prenom_usuel": null, + "url": "", + "faxes": "", + "adresse": "EPFL SO TV-3 NEVARRO $ Outer Rim $ Station 12 $ CH-1015 Lausanne $ $ ", + "cmpl_type": "F", + "ghost": null, + "has_accreds": "1" + } +] diff --git a/tests/resources/cadidb/getUnitPath-nevarro.json b/tests/resources/cadidb/getUnitPath-nevarro.json new file mode 100644 index 0000000..8a7338c --- /dev/null +++ b/tests/resources/cadidb/getUnitPath-nevarro.json @@ -0,0 +1,22 @@ +[ + { + "sigle": "SO", + "libelle": "", + "libelle_en": "Spin-off" + }, + { + "sigle": "EPFL", + "libelle": "Ecole polytechnique fédérale de Lausanne", + "libelle_en": "Ecole polytechnique fédérale de Lausanne" + }, + { + "sigle": "TV-3", + "libelle": "Série TV 3", + "libelle_en": "TV Series 3" + }, + { + "sigle": "NEVARRO", + "libelle": "Nevarro est une planète volcanique située dans les territoires de la bordure extérieure de la galaxie.", + "libelle_en": "" + } +] diff --git a/tests/resources/cadidb/searchUnits-nevarro.json b/tests/resources/cadidb/searchUnits-nevarro.json new file mode 100644 index 0000000..8868d34 --- /dev/null +++ b/tests/resources/cadidb/searchUnits-nevarro.json @@ -0,0 +1,8 @@ +[ + { + "sigle": "NEVARRO", + "libelle": "Nevarro est une planète volcanique située dans les territoires de la bordure extérieure de la galaxie.", + "libelle_en": "Nevarro is a volcanic planet located in the Outer Rim territories of the galaxy.", + "hierarchie": "EPFL SO TV-3 NEVARRO" + } +] diff --git a/tests/resources/unit/unit-mandalore-en.json b/tests/resources/unit/unit-mandalore-en.json index 1c592cb..ae918c4 100644 --- a/tests/resources/unit/unit-mandalore-en.json +++ b/tests/resources/unit/unit-mandalore-en.json @@ -30,10 +30,34 @@ "CH-1015 Lausanne" ], "head": { - "sciper": "670005", - "name": "Grogu", + "sciper": "670003", + "name": "Djarin", "firstname": "Din", - "email": "din.grogu@epfl.ch", - "profile": "din.grogu" - } + "email": "din.djarin@epfl.ch", + "profile": "din.djarin" + }, + "people": [ + { + "email": "din.djarin@epfl.ch", + "firstname": "Din", + "name": "Djarin", + "officeList": ["Sundari 231"], + "phoneList": ["+41 21 0054321"], + "position": "Bounty hunter", + "profile": "din.djarin", + "rank": 0, + "sciper": "670003" + }, + { + "email": "paz.vizsla@epfl.ch", + "firstname": "Paz", + "name": "Vizsla", + "officeList": ["Ronion 001"], + "phoneList": ["+41 21 0654321"], + "position": "Heavy infantry", + "profile": "paz.vizsla", + "rank": 0, + "sciper": "670004" + } + ] } diff --git a/tests/resources/unit/unit-mandalore-fr.json b/tests/resources/unit/unit-mandalore-fr.json index 9309a47..1077256 100644 --- a/tests/resources/unit/unit-mandalore-fr.json +++ b/tests/resources/unit/unit-mandalore-fr.json @@ -30,10 +30,34 @@ "CH-1015 Lausanne" ], "head": { - "sciper": "670005", - "name": "Grogu", + "sciper": "670003", + "name": "Djarin", "firstname": "Din", - "email": "din.grogu@epfl.ch", - "profile": "din.grogu" - } + "email": "din.djarin@epfl.ch", + "profile": "din.djarin" + }, + "people": [ + { + "email": "din.djarin@epfl.ch", + "firstname": "Din", + "name": "Djarin", + "officeList": ["Sundari 231"], + "phoneList": ["+41 21 0054321"], + "position": "Chasseur de prime", + "profile": "din.djarin", + "rank": 0, + "sciper": "670003" + }, + { + "email": "paz.vizsla@epfl.ch", + "firstname": "Paz", + "name": "Vizsla", + "officeList": ["Ronion 001"], + "phoneList": ["+41 21 0654321"], + "position": "Infanterie lourde", + "profile": "paz.vizsla", + "rank": 0, + "sciper": "670004" + } + ] } diff --git a/tests/resources/unit/unit-nevarro-fr.json b/tests/resources/unit/unit-nevarro-fr.json new file mode 100644 index 0000000..3e76b58 --- /dev/null +++ b/tests/resources/unit/unit-nevarro-fr.json @@ -0,0 +1,39 @@ +{ + "code": 10111, + "acronym": "NEVARRO", + "name": "Nevarro est une planète volcanique située dans les territoires de la bordure extérieure de la galaxie.", + "unitPath": "EPFL SO TV-3 NEVARRO", + "path": [ + { + "acronym": "EPFL", + "name": "Ecole polytechnique fédérale de Lausanne" + }, + { + "acronym": "SO", + "name": "Spin-off" + }, + { + "acronym": "TV-3", + "name": "Série TV 3" + }, + { + "acronym": "NEVARRO", + "name": "Nevarro est une planète volcanique située dans les territoires de la bordure extérieure de la galaxie." + } + ], + "terminal": "1", + "ghost": null, + "address": [ + "EPFL SO TV-3 NEVARRO", + "Outer Rim", + "Station 12", + "CH-1015 Lausanne" + ], + "head": { + "sciper": "670003", + "name": "Djarin", + "firstname": "Din", + "email": "din.djarin@epfl.ch", + "profile": "din.djarin" + } +} diff --git a/tests/unit.test.js b/tests/unit.test.js index a89345e..5ba6e7c 100644 --- a/tests/unit.test.js +++ b/tests/unit.test.js @@ -133,6 +133,32 @@ describe('Test API Unit ("/api/unit")', () => { expect(JSON.parse(response.text)).toStrictEqual(jsonResult); }); + test('It should return a unit without people', async () => { + const mockConnection = { + query: jest.fn().mockImplementation((query, values, referrer) => { + let jsonData; + switch (referrer) { + case 'searchUnits': + jsonData = require('./resources/cadidb/searchUnits-nevarro.json'); + break; + case 'getUnit': + jsonData = require('./resources/cadidb/getUnit-nevarro.json'); + break; + case 'getUnitPath': + jsonData = require('./resources/cadidb/getUnitPath-nevarro.json'); + } + return Promise.resolve([jsonData]); + }), + release: jest.fn() + }; + mysql.createPool().getConnection.mockResolvedValue(mockConnection); + + const jsonResult = require('./resources/unit/unit-nevarro-fr.json'); + const response = await request(app).get('/api/unit?q=nevarro&hl=fr'); + expect(response.statusCode).toBe(200); + expect(JSON.parse(response.text)).toStrictEqual(jsonResult); + }); + test('It should return a unit with subunits', async () => { const mockConnection = { query: jest.fn().mockImplementation((query, values, referrer) => {