Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add people (Unit API) #108

Merged
merged 6 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/services/people.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
7 changes: 5 additions & 2 deletions src/services/unit.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,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);
}
Expand Down
61 changes: 60 additions & 1 deletion src/utils/ldap.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -281,12 +298,54 @@ 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,
dn2path,
getCorrectName,
getProfile,
ldap2api,
ldapAddress2api
ldapAddress2api,
ldapUnit2api
};
4 changes: 2 additions & 2 deletions tests/resources/cadidb/getUnit-mandalore.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions tests/resources/cadidb/getUnit-nevarro.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
22 changes: 22 additions & 0 deletions tests/resources/cadidb/getUnitPath-nevarro.json
Original file line number Diff line number Diff line change
@@ -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": ""
}
]
8 changes: 8 additions & 0 deletions tests/resources/cadidb/searchUnits-nevarro.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
34 changes: 29 additions & 5 deletions tests/resources/unit/unit-mandalore-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,34 @@
"CH-1015 Lausanne"
],
"head": {
"sciper": "670005",
"name": "Grogu",
"sciper": "670003",
"name": "Djarin",
"firstname": "Din",
"email": "[email protected]",
"profile": "din.grogu"
}
"email": "[email protected]",
"profile": "din.djarin"
},
"people": [
{
"email": "[email protected]",
"firstname": "Din",
"name": "Djarin",
"officeList": ["Sundari 231"],
"phoneList": ["+41 21 0054321"],
"position": "Bounty hunter",
"profile": "din.djarin",
"rank": 0,
"sciper": "670003"
},
{
"email": "[email protected]",
"firstname": "Paz",
"name": "Vizsla",
"officeList": ["Ronion 001"],
"phoneList": ["+41 21 0654321"],
"position": "Heavy infantry",
"profile": "paz.vizsla",
"rank": 0,
"sciper": "670004"
}
]
}
34 changes: 29 additions & 5 deletions tests/resources/unit/unit-mandalore-fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,34 @@
"CH-1015 Lausanne"
],
"head": {
"sciper": "670005",
"name": "Grogu",
"sciper": "670003",
"name": "Djarin",
"firstname": "Din",
"email": "[email protected]",
"profile": "din.grogu"
}
"email": "[email protected]",
"profile": "din.djarin"
},
"people": [
{
"email": "[email protected]",
"firstname": "Din",
"name": "Djarin",
"officeList": ["Sundari 231"],
"phoneList": ["+41 21 0054321"],
"position": "Chasseur de prime",
"profile": "din.djarin",
"rank": 0,
"sciper": "670003"
},
{
"email": "[email protected]",
"firstname": "Paz",
"name": "Vizsla",
"officeList": ["Ronion 001"],
"phoneList": ["+41 21 0654321"],
"position": "Infanterie lourde",
"profile": "paz.vizsla",
"rank": 0,
"sciper": "670004"
}
]
}
39 changes: 39 additions & 0 deletions tests/resources/unit/unit-nevarro-fr.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"profile": "din.djarin"
}
}
26 changes: 26 additions & 0 deletions tests/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down