74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
const {toCSV} = require('vn-loopback/util/csv');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('negativeBasesCsv', {
|
|
description: 'Returns the negative bases as .csv',
|
|
accessType: 'READ',
|
|
accepts: [
|
|
{
|
|
arg: 'from',
|
|
type: 'date',
|
|
description: 'From date',
|
|
required: true
|
|
}, {
|
|
arg: 'to',
|
|
type: 'date',
|
|
description: 'To date',
|
|
required: true
|
|
}, {
|
|
arg: 'filter',
|
|
type: 'object',
|
|
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
|
|
},
|
|
],
|
|
returns: [
|
|
{
|
|
arg: 'body',
|
|
type: 'file',
|
|
root: true
|
|
}, {
|
|
arg: 'Content-Type',
|
|
type: 'String',
|
|
http: {target: 'header'}
|
|
}, {
|
|
arg: 'Content-Disposition',
|
|
type: 'String',
|
|
http: {target: 'header'}
|
|
}
|
|
],
|
|
http: {
|
|
path: '/negativeBasesCsv',
|
|
verb: 'GET'
|
|
},
|
|
accessScopes: ['DEFAULT', 'read:multimedia']
|
|
});
|
|
|
|
Self.negativeBasesCsv = async(ctx, options) => {
|
|
const $t = ctx.req.__; // $translate
|
|
const args = ctx.args;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const negativeBases = await Self.app.models.InvoiceOut.negativeBases(ctx, myOptions);
|
|
const locatedFields = [];
|
|
negativeBases.forEach(element => {
|
|
locatedFields.push(Object.keys(element).map(key => {
|
|
return {newName: $t(key), value: element[key]};
|
|
}).filter(item => item !== null)
|
|
.reduce((result, item) => {
|
|
result[item.newName] = item.value;
|
|
return result;
|
|
}, {}));
|
|
});
|
|
const content = toCSV(locatedFields);
|
|
|
|
return [
|
|
content,
|
|
'text/csv',
|
|
`attachment; filename="negative-bases-${new Date(args.from).toLocaleDateString()}-${new Date(args.to).toLocaleDateString()}.csv"`
|
|
];
|
|
};
|
|
};
|