feat(salix): refs #7905 #7905 use getBuys toCSV flattened
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
This commit is contained in:
parent
e50c67c305
commit
0f4bc3f83b
|
@ -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,
|
||||
};
|
|
@ -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;
|
||||
// }
|
||||
|
||||
|
|
Loading…
Reference in New Issue