-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat(inventory) download log as excel #3999
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,16 @@ | |
* | ||
* 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; | ||
|
@@ -52,6 +56,9 @@ exports.deleteInventory = deleteInventory; | |
exports.importing = importing; | ||
|
||
exports.logs = inventoryLog; | ||
exports.logDownLoad = logDownLoad; | ||
|
||
|
||
// ======================= inventory metadata ============================= | ||
/** | ||
* POST /inventory/metadata | ||
|
@@ -88,6 +95,76 @@ function inventoryLog(req, res, next) { | |
}).catch(next); | ||
} | ||
|
||
// get inventory log as excel | ||
// GET /inventory/download/log/:uuid?rendere=xlsx?lang=fr | ||
|
||
async function logDownLoad(req, res, next) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have an integration test for this functionality, to at least show it still works. Could you write one? |
||
try { | ||
const { lang } = req.query; | ||
const rows = await core.inventoryLog(req.params.uuid); | ||
// inventory columns | ||
|
||
const dictionary = util.loadDictionary(lang); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
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 || '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there ever a case where |
||
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', | ||
jniles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
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/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const helpers = require('../helpers'); | ||
const shared = require('./shared'); | ||
const xlsx = require('../../../server/lib/renderers/xlsx.js'); | ||
|
||
describe('(/inventory/metadata) The inventory metadata http API', () => { | ||
let inventoryList; | ||
|
@@ -19,6 +20,8 @@ describe('(/inventory/metadata) The inventory metadata http API', () => { | |
sellable : 1, | ||
}; | ||
|
||
const inventoryUuid = 'c8a406a9-53d6-429f-84d8-fc497875a580'; | ||
|
||
const metadataUpdate = { | ||
code : '1000012', // code must be unique | ||
text : '[IT] Inventory Article updated', | ||
|
@@ -31,6 +34,16 @@ describe('(/inventory/metadata) The inventory metadata http API', () => { | |
text : 'Albendazo', // should find "Albendazole" | ||
}; | ||
|
||
it('GET /inventory/download/log/?lang=fr donwload log as Ms excel', () => { | ||
return agent.get(`/inventory/download/log/${inventoryUuid}?lang=fr`) | ||
.then(res => { | ||
expect(res).to.have.status(200); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you check the content type to make sure it is the correct content type? |
||
expect(res.headers['content-type']).to.be.equal(xlsx.headers['Content-Type']); | ||
}) | ||
.catch(helpers.handler); | ||
}); | ||
|
||
|
||
it('POST /inventory/metadata create a new inventory metadata', () => { | ||
return agent.post('/inventory/metadata') | ||
.send(metadata) | ||
|
@@ -41,6 +54,7 @@ describe('(/inventory/metadata) The inventory metadata http API', () => { | |
.catch(helpers.handler); | ||
}); | ||
|
||
|
||
it('PUT /inventory/metadata/:uuid update an existing inventory metadata', () => { | ||
return agent.put(`/inventory/metadata/${metadata.uuid}`) | ||
.send(metadataUpdate) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This URL doesn't match what the actual URL is. Can you change the parameters to reflect that?