54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
|
const {toCSV} = require('vn-loopback/util/csv');
|
||
|
|
||
|
module.exports = Self => {
|
||
|
Self.remoteMethodCtx('unbilledTicketsCsv', {
|
||
|
description: 'Returns the unbilled tickets as .csv',
|
||
|
accessType: 'READ',
|
||
|
accepts: [{
|
||
|
arg: 'unbilledTickets',
|
||
|
type: ['object'],
|
||
|
required: true
|
||
|
},
|
||
|
{
|
||
|
arg: 'from',
|
||
|
type: 'date',
|
||
|
description: 'From date'
|
||
|
},
|
||
|
{
|
||
|
arg: 'to',
|
||
|
type: 'date',
|
||
|
description: 'To date'
|
||
|
}],
|
||
|
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: '/unbilledTicketsCsv',
|
||
|
verb: 'GET'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.unbilledTicketsCsv = async ctx => {
|
||
|
const args = ctx.args;
|
||
|
const content = toCSV(args.unbilledTickets);
|
||
|
|
||
|
return [
|
||
|
content,
|
||
|
'text/csv',
|
||
|
`attachment; filename="unbilled-tickets-${new Date(args.from).toLocaleDateString()}-${new Date(args.to).toLocaleDateString()}.csv"`
|
||
|
];
|
||
|
};
|
||
|
};
|