From 1e3ab2d31a1034183c572ead9ef8a51e60462b50 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 29 Aug 2024 10:07:02 +0200 Subject: [PATCH] feat: refs #7905 Added param toCsv --- modules/entry/back/methods/entry/getBuys.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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; }; };