feat: refs #7905 Added new method getBuysCsv
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Guillermo Bonet 2024-08-29 12:19:06 +02:00
parent 1e3ab2d31a
commit 72b4607d54
4 changed files with 80 additions and 17 deletions

View File

@ -0,0 +1,2 @@
INSERT INTO salix.ACL (model,property,principalId)
VALUES ('Entry','getBuysCsv','supplier');

View File

@ -1,6 +1,5 @@
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', {
@ -17,11 +16,6 @@ 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: {
@ -34,7 +28,7 @@ module.exports = Self => {
}
});
Self.getBuys = async(ctx, id, filter, toCsv, options) => {
Self.getBuys = async(ctx, id, filter, options) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
const myOptions = {};
@ -135,15 +129,6 @@ module.exports = Self => {
};
defaultFilter = mergeFilters(defaultFilter, filter);
const data = models.Buy.find(defaultFilter, myOptions);
if (toCsv) {
return [
toCSV(data),
'text/csv',
`attachment; filename="${id}.csv"`
];
} else
return data;
return models.Buy.find(defaultFilter, myOptions);
};
};

View File

@ -0,0 +1,75 @@
const UserError = require('vn-loopback/util/user-error');
const {toCSV} = require('vn-loopback/util/csv');
module.exports = Self => {
Self.remoteMethodCtx('getBuys', {
description: 'Returns buys for one entry',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The entry id',
http: {source: 'path'}
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: `/:id/getBuysCsv`,
verb: 'GET'
}
});
Self.getBuys = 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"`];
};
};

View File

@ -3,6 +3,7 @@ module.exports = Self => {
require('../methods/entry/filter')(Self);
require('../methods/entry/getEntry')(Self);
require('../methods/entry/getBuys')(Self);
require('../methods/entry/getBuysCsv')(Self);
require('../methods/entry/importBuys')(Self);
require('../methods/entry/importBuysPreview')(Self);
require('../methods/entry/lastItemBuys')(Self);