-
Notifications
You must be signed in to change notification settings - Fork 2
/
score_80.js
62 lines (52 loc) · 1.89 KB
/
score_80.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
59
60
61
62
const fs = require('fs');
const _ = require('lodash');
const mailer = require('./mailer');
const API = require('./lib/dhis2-api');
module.exports.postData = (auth) => {
const result = { dataValues: [] };
const api = new API({
credentials: auth,
url: 'https://ima-assp.org/api/analytics/dataValueSet.json',
});
const query = 'dimension=dx:kSZql3OdY28&dimension=pe:LAST_4_QUARTERS&dimension=ou:LEVEL-3;D15NtionqkH;I8CuQpdBQfP;iu4Zj3Zq39m&displayProperty=NAME';
// const query = `dimension=dx:kSZql3OdY28&dimension=pe:LAST_QUARTER&dimension=ou:LEVEL-3;D15NtionqkH;I8CuQpdBQfP;iu4Zj3Zq39m&displayProperty=NAME`;
// download the Data
api.analytics({
query,
})
.then((source) => {
const { dataValues } = source;
const score80 = 'Lceopb5AECI';
const orgs = _.groupBy(dataValues, 'orgUnit');
const orgKeys = Object.keys(orgs);
// orgKeys.forEach(orgKey => {
let donne = 0;
dataValues.forEach((source) => {
// const orgPeriod = _.groupBy(orgs[orgKey], 'period');
// const orgPeriodKeys = Object.keys(orgPeriod);
if (source.value >= 80) {
const score = 1;
donne += 1;
result.dataValues.push({
orgUnit: source.orgUnit,
period: source.period,
value: score,
dataElement: score80,
storedBy: `IMA ${source.created}`,
created: source.created,
});
}
});
console.log(`nombre : ${donne}`);
fs.writeFileSync('./score_80.json', JSON.stringify(result));
return api.postData({
data: result,
url: 'https://ima-assp.org/api/dataValueSets?skipAudit=true',
});
})
.then(() => {
mailer.sendMail('success!!! Import Score 80', 'Import Score 80');
}).catch((err) => {
mailer.sendMail(`Fail!!! Import Score 80${JSON.stringify(err)}`, 'Fail!!! Import Score 80');
});
};