From 0f4bc3f83b33e320bdd7cd748d1e26716902f45c Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 30 Aug 2024 09:42:50 +0200 Subject: [PATCH] feat(salix): refs #7905 #7905 use getBuys toCSV flattened --- loopback/util/flatten.js | 28 ++++++++ .../entry/back/methods/entry/getBuysCsv.js | 65 ++++++++----------- 2 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 loopback/util/flatten.js diff --git a/loopback/util/flatten.js b/loopback/util/flatten.js new file mode 100644 index 000000000..90e9184b7 --- /dev/null +++ b/loopback/util/flatten.js @@ -0,0 +1,28 @@ + +function flatten(dataArray) { + return dataArray.map(item => flatten(item.__data)); +} +function flattenObj(data, prefix = '') { + let result = {}; + try { + for (let key in data) { + if (!data[key]) continue; + + const newKey = prefix ? `${prefix}_${key}` : key; + const value = data[key]; + + if (typeof value === 'object' && value !== null && !Array.isArray(value)) + Object.assign(result, flattenObj(value.__data, newKey)); + else + result[newKey] = value; + } + } catch (error) { + console.error(error); + } + + return result; +} +module.exports = { + flatten, + flattenObj, +}; diff --git a/modules/entry/back/methods/entry/getBuysCsv.js b/modules/entry/back/methods/entry/getBuysCsv.js index 541179a1d..647f41d22 100644 --- a/modules/entry/back/methods/entry/getBuysCsv.js +++ b/modules/entry/back/methods/entry/getBuysCsv.js @@ -1,5 +1,5 @@ -const UserError = require('vn-loopback/util/user-error'); const {toCSV} = require('vn-loopback/util/csv'); +const {flatten} = require('vn-loopback/util/flatten'); module.exports = Self => { Self.remoteMethodCtx('getBuysCsv', { @@ -35,41 +35,32 @@ module.exports = Self => { }); Self.getBuysCsv = async(ctx, id, options) => { - const userId = ctx.req.accessToken.userId; - const models = Self.app.models; - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - const client = await models.Client.findById(userId, myOptions); - const supplier = await models.Supplier.findOne({where: {nif: client.fi}}, myOptions); - if (supplier) { - const isEntryOwner = (await Self.findById(id)).supplierFk === supplier.id; - if (!isEntryOwner) throw new UserError('Access Denied'); - } - - const data = await Self.rawSql(` - SELECT b.id, - b.itemFk, - i.name, - b.stickers, - b.packing, - b.grouping, - b.packing, - b.groupingMode, - b.quantity, - b.packagingFk, - b.weight, - b.buyingValue, - b.price2, - b.price3, - b.printedStickers - FROM buy b - JOIN item i ON i.id = b.itemFk - WHERE b.entryFk = ? - `, [id]); - - return [toCSV(data), 'text/csv', `inline; filename="buys-${id}.csv"`]; + const data = await Self.getBuys(ctx, id, null, options); + const dataFlatted = flatten(data); + return [toCSV(dataFlatted), 'text/csv', `inline; filename="buys-${id}.csv"`]; }; }; +// function flattenJSON(dataArray) { +// return dataArray.map(item => flatten(item.__data)); +// } +// function flatten(data, prefix = '') { +// let result = {}; +// try { +// for (let key in data) { +// if (!data[key]) continue; + +// const newKey = prefix ? `${prefix}_${key}` : key; +// const value = data[key]; + +// if (typeof value === 'object' && value !== null && !Array.isArray(value)) +// Object.assign(result, flatten(value.__data, newKey)); +// else +// result[newKey] = value; +// } +// } catch (error) { +// console.error(error); +// } + +// return result; +// } +