-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
212 lines (199 loc) · 7.23 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require('dotenv').config();
const _ = require('lodash');
const path = require('path');
const axios = require('axios');
const moment = require('moment');
const fs = require('fs');
const xlsx = require('xlsx');
const logUpdate = require('log-update');
const rumer = require(`${process.env.PATH_RUMER_FUNCTION}`);
const db = require(`${process.env.PATH_DB}`);
const DHIS2_SERVER_URL = process.env.DHIS2_SERVER_URL;
const DHIS2_USERNAME = process.env.DHIS2_USERNAME;
const DHIS2_PASSWORD = process.env.DHIS2_PASSWORD;
const frames = ['-', '\\', '|', '/'];
let index = 0;
async function formatData(startDate, endDate, depot) {
try {
const pathLink = path.join(__dirname, './list_medicament_colonne_dhis2.xls');
const wb = xlsx.readFile(pathLink);
const ws1 = wb.SheetNames[0];
const ws2 = wb.SheetNames[1];
const listInventory = _.uniqBy(xlsx.utils.sheet_to_json(wb.Sheets[ws1]), 'DE_UID');
const colonnes = xlsx.utils.sheet_to_json(wb.Sheets[ws2]);
const result = [];
await depot.reduce(async (promise, item) => {
await promise;
const data = await rumer.getData({
depot_uuid : item.uuid,
start_date : startDate,
end_date : endDate,
condensed_report : 1,
});
const period = moment(startDate).format('YYYY-MM-DD').split('-');
if (data.data.configurationData.length > 0) {
data.data.configurationData.forEach((field) => {
listInventory.forEach((inv) => {
if (inv.DE_NAME === field.inventoryText) {
colonnes.forEach((col) => {
if (col.label === 'quantityOpening') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.quantityOpening,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
if (col.label === 'quantityEnding') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.quantityEnding,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
if (col.label === 'outQuantityConsumption') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.outQuantityConsumption,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
if (col.label === 'quantityTotalEntry') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.quantityTotalEntry,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
if (col.label === 'quantityTotalExit') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.quantityTotalExit,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
if (col.label === 'outQuantityExit') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.outQuantityExit,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
if (col.label === 'numStockOutDays') {
result.push({
period: `${period[0]}${period[1]}`,
orgUnit: item.dhis2_uid,
dataElement: inv.DE_UID,
categoryOptionCombo: col.uids,
value: field.numStockOutDays,
storedBy: 'BHIMA SYSTEM',
lastUpdated: moment(new Date()).format('YYYY-MM-DD'),
comment: '',
deleted: null,
followup: false,
});
}
});
}
});
});
}
}, Promise.resolve());
return _.uniqWith(result, _.isEqual);
} catch (error) {
throw error;
}
}
async function importToDhis2(data) {
try {
const form = {
dataValues : data,
};
fs.writeFileSync('./dhis2.json', JSON.stringify(form));
const response = await axios.post(
`${DHIS2_SERVER_URL}/api/dataValueSets?async=true&skipAudit=true`,
form,
{
auth : {
username : DHIS2_USERNAME,
password : DHIS2_PASSWORD,
},
},
);
console.log('Importation des données réussie:', response.data);
} catch (error) {
console.error(
'Erreur lors de l\'importation des données sur DHIS2:',
error.message,
);
throw error;
}
}
/**
* @method main
* @description This script allows you to send data from rumer bhima to
* dhis2 for all the structures that are linked
*/
async function main() {
const sql = `SELECT * FROM depot WHERE dhis2_uid IS NOT NULL;`;
const log = logUpdate.createLogUpdate(process.stdout);
setInterval(() => {
const frame = frames[(index = ++index % frames.length)];
log(`${frame} please wait`);
}, 80);
log.done('Data import in progress...');
try {
const depots = await db.exec(sql);
const startDate = process.env.START_DATE;
const endDate = process.env.END_DATE;
const data = await formatData(startDate, endDate, depots);
await importToDhis2(data);
log.clear();
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
}
main();