Skip to content

Commit

Permalink
Add hl parameter (People API) (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
williambelle authored Jun 26, 2023
1 parent 0c3a27c commit b368891
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 28 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@

### Parameters

| Name | Type | Comments |
| ---- | -------- | -------- |
| `q` | `String` | Query |
| Name | Type | Comments |
| ---- | -------- | --------------------------------- |
| `q` | `String` | Query |
| `hl` | `String` | Sets the user interface language. |

### Examples

- [/api/ldap?q=278890][ldap-1]
- [/api/ldap?q=[email protected]][ldap-2]
- [/api/ldap?q=[email protected]&hl=en][ldap-2]

## EPFL Graph

Expand Down Expand Up @@ -81,6 +82,6 @@ See [Contributing](CONTRIBUTING.md).
[cse-2]: http://127.0.0.1:5555/api/cse?q=math&hl=fr&searchSite=actu.epfl.ch
[cse-3]: http://127.0.0.1:5555/api/cse?q=math&hl=en&sort=date&searchType=image
[ldap-1]: http://127.0.0.1:5555/api/ldap?q=278890
[ldap-2]: http://127.0.0.1:5555/api/[email protected]
[ldap-2]: http://127.0.0.1:5555/api/[email protected]&hl=en
[graphsearch-1]: http://127.0.0.1:5555/api/graphsearch?q=math
[graphsearch-2]: http://127.0.0.1:5555/api/graphsearch?q=vetterli
2 changes: 1 addition & 1 deletion src/controllers/people.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function get (req, res) {
} else if (/^[^@]+@[^@]+$/.test(q)) {
ldapResults = await peopleService.getPersonByEmail(q);
}
return res.json(ldapUtil.ldap2api(ldapResults));
return res.json(ldapUtil.ldap2api(ldapResults, req.query.hl));
} catch (err) {
console.error('[error] ', err.message);
return res.status(400).json({
Expand Down
25 changes: 17 additions & 8 deletions src/utils/ldap.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ const ldapUserMapper = {
givenName: ['firstname', (val) => val[0]]
};

const ldapAccredMapper = {
ou: ['name', (val) => val[1]],
EPFLAccredOrder: ['rank', (val) => val[0]],
title: ['position', (val) => val[0]],
roomNumber: ['officeList', (val) => val],
telephoneNumber: ['phoneList', (val) => val]
};
function newLdapAccredMapper (lang) {
const ldapAccredMapper = {
EPFLAccredOrder: ['rank', (val) => val[0]],
roomNumber: ['officeList', (val) => val],
telephoneNumber: ['phoneList', (val) => val]
};
if (lang === 'en') {
ldapAccredMapper['description;lang-en'] = ['position', (val) => val[0]];
ldapAccredMapper['ou;lang-en'] = ['name', (val) => val[0]];
} else {
ldapAccredMapper.description = ['position', (val) => val[0]];
ldapAccredMapper.ou = ['name', (val) => val[1]];
}
return ldapAccredMapper;
}

function sortAccreds (obj) {
return obj.sort((a, b) => a.rank - b.rank);
Expand Down Expand Up @@ -97,8 +105,9 @@ function getProfile (mail, sciper) {
* @param {object} ldapResults The result from the LDAP search.
* @returns {object} Return the result for the API.
*/
function ldap2api (ldapResults) {
function ldap2api (ldapResults, hl) {
const list = [];
const ldapAccredMapper = newLdapAccredMapper(hl);

for (const [sciper, entry] of Object.entries(ldapResults)) {
const person = { sciper, rank: 0 };
Expand Down
15 changes: 11 additions & 4 deletions tests/people.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ describe('Test API People ("/api/ldap")', () => {
expect(response.text).toMatch('[]');
});

test('It should find sciper 670001', async () => {
const jsonResult = require('./resources/people/json-sciper-670001.json');
const response = await request(app).get('/api/ldap?q=670001');
test('It should find sciper 670001 (fr)', async () => {
const jsonResult = require('./resources/people/json-sciper-670001-fr.json');
const response = await request(app).get('/api/ldap?q=670001&hl=fr');
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.text)).toStrictEqual(jsonResult);
});

test('It should find sciper 670001 (en)', async () => {
const jsonResult = require('./resources/people/json-sciper-670001-en.json');
const response = await request(app).get('/api/ldap?q=670001&hl=en');
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.text)).toStrictEqual(jsonResult);
});

test('It should find mail [email protected]', async () => {
const jsonResult = require('./resources/people/json-sciper-670001.json');
const jsonResult = require('./resources/people/json-sciper-670001-fr.json');
const response = await request(app).get('/api/[email protected]');
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.text)).toStrictEqual(jsonResult);
Expand Down
25 changes: 15 additions & 10 deletions tests/resources/ldap/directory.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
"roomNumber": "Base 1468",
"telephoneNumber": "+41 21 0004321",
"EPFLAccredOrder": "1",
"title": "Chasseur de prime",
"description": "Chasseur de prime",
"description;lang-en": "Bounty hunter",
"ou": [
"Kamino",
"Kamino est une planète aquatique située dans le secteur Abrion de la bordure extérieure de la galaxie."
]
],
"ou;lang-en": "Kamino is an aquatic planet located in the Abrion sector of the galaxy's Outer Rim."
}
},
{
Expand All @@ -31,12 +32,13 @@
"roomNumber": "Cloud City 016",
"telephoneNumber": "+41 21 0000321",
"EPFLAccredOrder": "1",
"title": "Chasseur de prime",
"description": "Chasseur de prime",
"description;lang-en": "Bounty hunter",
"ou": [
"Bespin",
"Bespin est une géante gazeuse du système stellaire du même nom."
]
],
"ou;lang-en": "Bespin is a gas giant in the star system of the same name."
}
},
{
Expand All @@ -51,12 +53,13 @@
"roomNumber": "Dune Sea 001",
"telephoneNumber": "+41 21 0000321",
"EPFLAccredOrder": "2",
"title": "Chasseur de prime",
"description": "Chasseur de prime",
"description;lang-en": "Bounty hunter",
"ou": [
"Tatooine",
"Tatooine est une planète désertique située dans les territoires de la bordure extérieure de la galaxie."
]
],
"ou;lang-en": "Tatooine is a desert planet located in the Outer Rim territories of the galaxy."
}
},
{
Expand All @@ -71,12 +74,13 @@
"roomNumber": "Sundari 231",
"telephoneNumber": "+41 21 0054321",
"EPFLAccredOrder": "1",
"title": "Chasseur de prime",
"description": "Chasseur de prime",
"description;lang-en": "Bounty hunter",
"ou": [
"Mandalore",
"Mandalore est une planète située dans les territoires de la bordure extérieure de la galaxie."
]
],
"ou;lang-en": "Mandalore is a planet located in the Outer Rim territories of the galaxy."
}
},
{
Expand All @@ -91,12 +95,13 @@
"roomNumber": "Ronion 001",
"telephoneNumber": "+41 21 0654321",
"EPFLAccredOrder": "1",
"title": "Infanterie lourde",
"description": "Infanterie lourde",
"description;lang-en": "Heavy infantry",
"ou": [
"Mandalore",
"Mandalore est une planète située dans les territoires de la bordure extérieure de la galaxie."
]
],
"ou;lang-en": "Mandalore is a planet located in the Outer Rim territories of the galaxy."
}
}
]
30 changes: 30 additions & 0 deletions tests/resources/people/json-sciper-670001-en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"sciper": "670001",
"rank": 0,
"name": "Fett",
"firstname": "Boba",
"profile": "boba.fett",
"email": "[email protected]",
"accreds": [
{
"phoneList": ["+41 21 0000321"],
"officeList": ["Cloud City 016"],
"path": "EPFL/OT/EP-5/BESPIN",
"rank": "1",
"position": "Bounty hunter",
"acronym": "BESPIN",
"name": "Bespin is a gas giant in the star system of the same name."
},
{
"phoneList": ["+41 21 0000321"],
"officeList": ["Dune Sea 001"],
"path": "EPFL/OT/EP-6/TATOOINE",
"rank": "2",
"position": "Bounty hunter",
"acronym": "TATOOINE",
"name": "Tatooine is a desert planet located in the Outer Rim territories of the galaxy."
}
]
}
]

0 comments on commit b368891

Please sign in to comment.