-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.js
58 lines (51 loc) · 1.73 KB
/
requests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const axios = require('axios').default;
const usersUrl = 'http://www.mocky.io/v2/5808862710000087232b75ac';
const policiesUrl = 'http://www.mocky.io/v2/580891a4100000e8242b75c5';
/** Get first user by id and/or name
* @param {String} [name] - user.id
* @param {String} [policyId] - policy.id
* @return {Promise} Array of policies */
function getOneUser({ name, id }) { return new Promise((resolve, reject) =>
axios.get(usersUrl).then(function (response) {
const usersFound = response.data.clients.filter(e => {
if ((id == undefined) && (name == undefined)) {
return false;
}
if ((id != undefined) && (id !== e.id)) {
return false;
}
return !((name != undefined) && (name !== e.name));
});
console.log(usersFound.length + ' users found for', {name, id});
resolve(usersFound[0]);
}).catch(function (error) {
console.log('requests catch 1');
reject(error);
})
)}
/** Get policies by user id and/or policy id
* @param {String} [userId] - user.id
* @param {String} [policyId] - policy.id
* @return {Promise} Array of policies */
function getPolicies({ userId, policyId }) { return new Promise((resolve, reject) =>
axios.get(policiesUrl).then(function (response) {
const policies = response.data.policies.filter(e => {
if ((userId == undefined) && (policyId == undefined)) {
return false;
}
if ((userId != undefined) && (userId !== e.clientId)) {
return false;
}
return !((policyId != undefined) && (policyId !== e.id));
});
console.log(policies.length + ' policies found');
resolve(policies);
}).catch(function (error) {
console.log(error);
reject(error);
})
)}
module.exports = {
getOneUser,
getPolicies
};