feat: refs #7905 Added param toCsv

This commit is contained in:
Guillermo Bonet 2024-08-29 10:07:02 +02:00
parent 6cb6110b14
commit 1e3ab2d31a
1 changed files with 17 additions and 2 deletions

View File

@ -1,5 +1,6 @@
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
const mergeFilters = require('vn-loopback/util/filter').mergeFilters; const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
const {toCSV} = require('vn-loopback/util/csv');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('getBuys', { Self.remoteMethodCtx('getBuys', {
@ -16,6 +17,11 @@ module.exports = Self => {
arg: 'filter', arg: 'filter',
type: 'object', type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string' description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
},
{
arg: 'toCsv',
type: 'boolean',
description: 'If true, return the data in CSV format'
} }
], ],
returns: { returns: {
@ -28,7 +34,7 @@ module.exports = Self => {
} }
}); });
Self.getBuys = async(ctx, id, filter, options) => { Self.getBuys = async(ctx, id, filter, toCsv, options) => {
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
@ -129,6 +135,15 @@ module.exports = Self => {
}; };
defaultFilter = mergeFilters(defaultFilter, filter); defaultFilter = mergeFilters(defaultFilter, filter);
return models.Buy.find(defaultFilter, myOptions); const data = models.Buy.find(defaultFilter, myOptions);
if (toCsv) {
return [
toCSV(data),
'text/csv',
`attachment; filename="${id}.csv"`
];
} else
return data;
}; };
}; };