Skip to content

Commit

Permalink
Rewrite to display more infos
Browse files Browse the repository at this point in the history
  • Loading branch information
williambelle committed Feb 26, 2025
1 parent a475379 commit ab9f34b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/controllers/people.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function checkRoom (query) {
if (!/^[A-Z].*[0-9]/i.test(room)) {
return false;
}
const results = await apimdService.getRooms(query);
const results = await apimdService.getRoomsRaw(query);
return results.data.rooms.length > 0;
}

Expand All @@ -34,7 +34,9 @@ async function buildHashUnit () {
}

async function buildHashPhoneRoom (apiResults) {
const persons = await apimdService.getPersonsBySciper(listSciper(apiResults));
const persons = await apimdService.getPersonsBySciperRaw(
listSciper(apiResults)
);

const phoneHash = {};
const roomHash = {};
Expand Down
79 changes: 65 additions & 14 deletions src/services/apimd.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ const axiosConfig = {
timeout: 10000
};

const roles = {
cosec: {
M: {
fr: 'Correspondant de sécurité (COSEC)',
en: 'Safety Delegate (COSEC)'
},
F: {
fr: 'Correspondante de sécurité (COSEC)',
en: 'Safety Delegate (COSEC)'
},
X: {
fr: 'Correspondant/Correspondante de sécurité (COSEC)',
en: 'Safety Delegate (COSEC)'
}
}
};

function getPosition (accred, gender, lang) {
const position = accred.origin === 's'
? {
Expand Down Expand Up @@ -66,19 +83,12 @@ function getOfficeList (person, unitId) {
}
}

async function getPersonsByUnit (unitId, lang) {
const url = '/v1/epfl-search/unit/' + unitId;
const response = await axios.get(`${apimdConfig.baseURL}${url}`, axiosConfig);
const data = response.data;
function md2api (data, unitId, lang) {
// authid 1 → botweb (Appear in the unit's web directory)
const authorizedScipers = data.authorizations
.filter(a => a.authid === 1 && a.value.includes('y'))
.map(a => a.persid.toString());
const people = [];
const cosec = data.cosec.authorizations
.filter(a => a.authid === 97 && a.value.includes('y'))
.map(a => a.persid.toString());

data.persons.forEach((person) => {
if (authorizedScipers.includes(person.id)) {
const p = {
Expand All @@ -105,13 +115,30 @@ async function getPersonsByUnit (unitId, lang) {
people.push(p);
}
});
return people;
}

async function getPersonsByUnitRaw (unitId) {
const url = '/v1/epfl-search/unit/' + unitId;
return await axios.get(`${apimdConfig.baseURL}${url}`, axiosConfig);
}

async function getPersonsByUnit (unitId, lang) {
const response = await getPersonsByUnitRaw(unitId);
const people = md2api(response.data, unitId, lang);

// authid 97 → cosec (Safety Delegate (COSEC))
const cosec = response.data.cosec.authorizations
.filter(a => a.authid === 97 && a.value.includes('y'))
.map(a => a.persid.toString());

return [people.sort((a, b) =>
a.name.localeCompare(b.name) ||
a.firstname.localeCompare(b.firstname)
), cosec];
}

async function getUnits (query) {
async function getUnitsRaw (query) {
const route = '/v1/units';
const config = structuredClone(axiosConfig);
config.params = {
Expand All @@ -121,7 +148,7 @@ async function getUnits (query) {
return await axios.get(`${apimdConfig.baseURL}${route}`, config);
}

async function getRooms (query) {
async function getRoomsRaw (query) {
const route = '/v1/rooms';
const config = structuredClone(axiosConfig);
config.params = {
Expand All @@ -131,7 +158,7 @@ async function getRooms (query) {
return await axios.get(`${apimdConfig.baseURL}${route}`, config);
}

async function getPersonsBySciper (sciperList) {
async function getPersonsBySciperRaw (sciperList) {
const route = '/v1/persons';
const config = structuredClone(axiosConfig);
config.params = {
Expand All @@ -140,9 +167,33 @@ async function getPersonsBySciper (sciperList) {
return await axios.get(`${apimdConfig.baseURL}${route}`, config);
}

async function getCosecDetails (sciperList, unitId, lang) {
const response = await getPersonsBySciperRaw(sciperList);
const people = [];

response.data.persons.forEach((person) => {
const p = {
sciper: person.id,
profile: ldapUtils.getProfile(person.profile, person.id),
email: person.email ? person.email : '',
name: person.lastnameusual ? person.lastnameusual : person.lastname,
firstname: person.firstnameusual
? person.firstnameusual
: person.firstname,
phoneList: getPhoneList(person, unitId),
officeList: getOfficeList(person, unitId),
role: roles.cosec[person.gender][lang]
};
people.push(p);
});
return people;
};

module.exports = {
getPersonsBySciper,
getCosecDetails,
getPersonsBySciperRaw,
getPersonsByUnit,
getRooms,
getUnits
getPersonsByUnitRaw,
getRoomsRaw,
getUnitsRaw
};
19 changes: 7 additions & 12 deletions src/services/unit.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,13 @@ async function getUnit (acro, lang, isInternal) {
`&hl=${lang}`;
}
if (cosec.length > 0) {
const ldapCosecPerson = await peopleService.getPersonBySciper(
cosec[0]
const cosecPersons = await apimdService.getCosecDetails(
cosec,
dict.id_unite,
lang
);
const cosecPerson = ldapUtil.ldap2api(ldapCosecPerson, '', lang);
if (cosecPerson.length > 0) {
unitFullDetails.cosec = {
sciper: cosecPerson[0].sciper,
profile: cosecPerson[0].profile,
email: cosecPerson[0].email ? cosecPerson[0].email : '',
name: cosecPerson[0].name,
firstname: cosecPerson[0].firstname
};
if (cosecPersons.length > 0) {
unitFullDetails.cosec = cosecPersons[0];
}
}
}
Expand Down Expand Up @@ -297,7 +292,7 @@ function sortSuggestions (array, q) {
}

async function getSuggestions (query) {
const results = await apimdService.getUnits(query);
const results = await apimdService.getUnitsRaw(query);
const units = results.data.units;
const suggestions = [];
for (const unit of units) {
Expand Down
8 changes: 8 additions & 0 deletions tests/resources/apimd/persons.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"persons": [
{
"id": "670001",
"gender": "M",
"rooms": [
{
"unitid": "14001",
Expand Down Expand Up @@ -29,6 +30,7 @@
},
{
"id": "670002",
"gender": "M",
"rooms": [
{
"unitid": "14000",
Expand All @@ -46,6 +48,7 @@
},
{
"id": "670003",
"gender": "M",
"rooms": [
{
"unitid": "11301",
Expand All @@ -63,6 +66,7 @@
},
{
"id": "670004",
"gender": "M",
"phones": [
{
"unitid": "11301",
Expand All @@ -73,6 +77,7 @@
},
{
"id": "670005",
"gender": "M",
"rooms": [
{
"unitid": "14100",
Expand All @@ -83,6 +88,7 @@
},
{
"id": "670006",
"gender": "F",
"rooms": [
{
"unitid": "11401",
Expand All @@ -100,6 +106,7 @@
},
{
"id": "670007",
"gender": "M",
"rooms": [
{
"unitid": "11501",
Expand All @@ -117,6 +124,7 @@
},
{
"id": "999999",
"gender": "M",
"rooms": [
{
"unitid": "99999",
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/apimd/unit-mandalore.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
"authorizations": [
{
"authid": 97,
"persid": 670004,
"persid": 670003,
"value": "y:U10000"
}
]
Expand Down
5 changes: 4 additions & 1 deletion tests/resources/unit/unit-kalevala-fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"firstname": "Bo-Katan",
"name": "Kryze",
"profile": "670006",
"sciper": "670006"
"sciper": "670006",
"officeList": ["Castle Kryze"],
"phoneList": ["+41210054323"],
"role": "Correspondante de sécurité (COSEC)"
}
}
13 changes: 8 additions & 5 deletions tests/resources/unit/unit-mandalore-fr-internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@
"fmt-list": "https://search-backend.epfl.ch/api/unit/csv?q=MANDALORE&hl=fr"
},
"cosec": {
"email": "[email protected]",
"firstname": "Paz",
"name": "Vizsla",
"profile": "paz.vizsla",
"sciper": "670004"
"email": "[email protected]",
"firstname": "Din",
"name": "Djarin",
"officeList": ["Sundari 231", "Bounty Hunter Lane"],
"phoneList": ["+41210054321", "+41210054324"],
"profile": "670003",
"sciper": "670003",
"role": "Correspondant de sécurité (COSEC)"
}
}

0 comments on commit ab9f34b

Please sign in to comment.