/
index.js
467 lines (415 loc) · 11.1 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
* TODO
* I would like to have a breakdown of usage by service. How do I do this?
* What is the best HTTP API for this sort of complex linked data?
*
* It is meant to be a high-level API to data about inventory data.
*
* As per REST conventions, the routes with a UUID return a single
* JSON instance or 404 NOT FOUND. The others return an array of
* results.
*
* TODO: We should migrate the inventory to using the regular bhima guidelines.
*/
const _ = require('lodash');
const core = require('./inventory/core');
const groups = require('./inventory/groups');
const types = require('./inventory/types');
const units = require('./inventory/units');
const importing = require('./import');
const util = require('../../lib/util');
const xlsx = require('../../lib/renderers/xlsx');
const ReportManager = require('../../lib/ReportManager');
// exposed routes
exports.createInventoryItems = createInventoryItems;
exports.updateInventoryItems = updateInventoryItems;
exports.getInventoryItems = getInventoryItems;
exports.getInventoryItemsById = getInventoryItemsById;
// expose inventory group methods
exports.createInventoryGroups = createInventoryGroups;
exports.updateInventoryGroups = updateInventoryGroups;
exports.listInventoryGroups = listInventoryGroups;
exports.detailsInventoryGroups = detailsInventoryGroups;
exports.deleteInventoryGroups = deleteInventoryGroups;
exports.countInventoryGroups = countInventoryGroups;
// expose inventory types methods
exports.createInventoryTypes = createInventoryTypes;
exports.updateInventoryTypes = updateInventoryTypes;
exports.listInventoryTypes = listInventoryTypes;
exports.detailsInventoryTypes = detailsInventoryTypes;
exports.deleteInventoryTypes = deleteInventoryTypes;
// expose inventory units methods
exports.createInventoryUnits = createInventoryUnits;
exports.updateInventoryUnits = updateInventoryUnits;
exports.listInventoryUnits = listInventoryUnits;
exports.detailsInventoryUnits = detailsInventoryUnits;
exports.deleteInventoryUnits = deleteInventoryUnits;
exports.deleteInventory = deleteInventory;
// expose routes for import
exports.importing = importing;
exports.logs = inventoryLog;
exports.logDownLoad = logDownLoad;
// ======================= inventory metadata =============================
/**
* POST /inventory/metadata
* Create a new inventory data entry
*/
function createInventoryItems(req, res, next) {
core.createItemsMetadata(req.body, req.session)
.then((identifier) => {
res.status(201).json({ uuid : identifier });
})
.catch((error) => {
core.errorHandler(error, req, res, next);
});
}
/**
* PUT /inventory/:uuid/metadata
* Update an inventory data entry
*/
function updateInventoryItems(req, res, next) {
core.updateItemsMetadata(req.body, req.params.uuid, req.session)
.then((metadata) => {
res.status(200).json(metadata);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
});
}
function inventoryLog(req, res, next) {
core.inventoryLog(req.params.uuid).then(logs => {
res.status(200).json(logs);
}).catch(next);
}
// get inventory log as excel
// GET /inventory/download/log/:uuid?rendere=xlsx?lang=fr
async function logDownLoad(req, res, next) {
try {
const { lang } = req.query;
const rows = await core.inventoryLog(req.params.uuid);
// inventory columns
const dictionary = util.loadDictionary(lang);
const inventory = await core.getItemsMetadata({ uuid : req.params.uuid });
const lines = [
{ column1 : '', column2 : '', column3 : '' },
];
lines.push({
column1 : _.get(dictionary, 'FORM.LABELS.INVENTORY'),
column2 : inventory[0].label || '',
column3 : '',
});
lines.push({ column1 : '', column2 : '', column3 : '' });
rows.forEach(r => {
const text = JSON.parse(r.text);
lines.push({
column1 : _.get(dictionary, 'FORM.LABELS.USER'),
column2 : _.get(dictionary, 'FORM.LABELS.DATE'),
column3 : '',
});
lines.push({ column1 : r.userName, column2 : r.log_timestamp, column3 : '' });
lines.push({
column1 : '',
column2 : _.get(dictionary, 'FORM.LABELS.FROM'),
column3 : _.get(dictionary, 'FORM.LABELS.TO'),
});
const currentchanges = Object.keys(text.current);
currentchanges.forEach(cc => {
const line2 = {
column1 : _.get(dictionary, core.inventoryColsMap[cc]),
column2 : text.last[cc],
column3 : text.current[cc],
};
lines.push(line2);
});
lines.push({ column1 : '', column2 : '', column3 : '' });
});
const options = {
csvKey : 'rows',
suppressDefaultFormatting : true,
suppressDefaultFiltering : true,
renderer : 'xlsx',
filename : 'FORM.LABELS.CHANGES',
};
const report = new ReportManager('', req.session, options);
const result = await report.render({ rows : lines }, null, { lang });
res.set(xlsx.headers).send(result.report);
} catch (error) {
next(error);
}
}
/**
* GET /inventory/metadata/
* Returns a description all inventory items in the inventory table.
* Returns a description the inventory items filter by params.
*
* @function searchInventoryItems
*/
function getInventoryItems(req, res, next) {
const params = req.query;
core.getItemsMetadata(params)
.then((row) => {
res.status(200).json(row);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/metadata/:uuid
* Returns a description of the item from the inventory table.
*
* @function getInventoryItemsById
*/
function getInventoryItemsById(req, res, next) {
const { uuid } = req.params;
core.getItemsMetadataById(uuid)
.then((row) => {
res.status(200).json(row);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* DELETE /inventory/metadata/:uuid
*
* @description
* Delete an inventory item from the database
*/
async function deleteInventory(req, res, next) {
try {
await core.remove(req.params.uuid);
res.sendStatus(204);
} catch (err) {
core.errorHandler(err, req, res, next);
}
}
// ======================= inventory group =============================
/**
* POST /inventory/groups
* Create a new inventory group
*/
function createInventoryGroups(req, res, next) {
groups.create(req.body)
.then((identifier) => {
res.status(201).json({ uuid : identifier });
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* PUT /inventory/groups/:uuid
* Create a new inventory group
*/
function updateInventoryGroups(req, res, next) {
groups.update(req.body, req.params.uuid)
.then((rows) => {
res.status(201).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/groups
* get the list of inventory groups
*/
function listInventoryGroups(req, res, next) {
groups.list(req.query.include_members)
.then((rows) => {
res.status(200).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/groups/:uuid
* get the list of inventory groups
*/
function detailsInventoryGroups(req, res, next) {
groups.details(req.params.uuid)
.then((rows) => {
res.status(200).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* DELETE /inventory/groups/:uuid
* delete an inventory group
*/
function deleteInventoryGroups(req, res, next) {
groups.remove(req.params.uuid)
.then(() => {
res.sendStatus(204);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/groups/:uuid/count
* count inventory in the group
*/
function countInventoryGroups(req, res, next) {
groups.countInventory(req.params.uuid)
.then((rows) => {
res.status(200).json(rows[0].inventory_counted);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
// ======================= inventory type =============================
/**
* POST /inventory/types
* Create a new inventory types
*/
function createInventoryTypes(req, res, next) {
types.create(req.body)
.then((id) => {
res.status(201).json({ id });
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* PUT /inventory/types/:id
* Create a new inventory types
*/
function updateInventoryTypes(req, res, next) {
types.update(req.body, req.params.id)
.then((rows) => {
res.status(201).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/types
* get the list of inventory types
*/
function listInventoryTypes(req, res, next) {
types.list()
.then((rows) => {
res.status(200).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/types/:id
* get the list of inventory types
*/
function detailsInventoryTypes(req, res, next) {
types.details(req.params.id)
.then((rows) => {
res.status(200).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* DELETE /inventory/types/:id
* delete an inventory types
*/
function deleteInventoryTypes(req, res, next) {
types.remove(req.params.id)
.then(() => {
res.sendStatus(204);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
// ======================= inventory unit =============================
/**
* POST /inventory/units
* Create a new inventory units
*/
function createInventoryUnits(req, res, next) {
units.create(req.body)
.then((id) => {
res.status(201).json({ id });
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* PUT /inventory/units/:id
* Create a new inventory units
*/
function updateInventoryUnits(req, res, next) {
units.update(req.body, req.params.id)
.then((rows) => {
res.status(201).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/units
* get the list of inventory units
*/
function listInventoryUnits(req, res, next) {
units.list()
.then((rows) => {
res.status(200).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* GET /inventory/units/:id
* get the list of inventory units
*/
function detailsInventoryUnits(req, res, next) {
units.details(req.params.id)
.then((rows) => {
res.status(200).json(rows);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}
/**
* DELETE /inventory/units/:id
* delete an inventory unit
*/
function deleteInventoryUnits(req, res, next) {
units.remove(req.params.id)
.then(() => {
res.sendStatus(204);
})
.catch((error) => {
core.errorHandler(error, req, res, next);
})
.done();
}