diff --git a/modules/entry/back/methods/entry/getBuys.js b/modules/entry/back/methods/entry/getBuys.js index 245dada09..65fb047c6 100644 --- a/modules/entry/back/methods/entry/getBuys.js +++ b/modules/entry/back/methods/entry/getBuys.js @@ -1,5 +1,6 @@ const UserError = require('vn-loopback/util/user-error'); const mergeFilters = require('vn-loopback/util/filter').mergeFilters; +const {toCSV} = require('vn-loopback/util/csv'); module.exports = Self => { Self.remoteMethodCtx('getBuys', { @@ -16,6 +17,11 @@ module.exports = Self => { arg: 'filter', type: 'object', 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: { @@ -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 models = Self.app.models; const myOptions = {}; @@ -129,6 +135,15 @@ module.exports = Self => { }; 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; }; };