7983-testToMaster_2438 #2977

Merged
alexm merged 212 commits from 7983-testToMaster_2438 into master 2024-09-17 05:36:47 +00:00
2 changed files with 56 additions and 37 deletions
Showing only changes of commit 0f4bc3f83b - Show all commits

28
loopback/util/flatten.js Normal file
View File

@ -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,
};

View File

@ -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;
// }