salix/modules/ticket/back/methods/sale/canEdit.js

70 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-10-04 11:22:11 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('canEdit', {
description: 'Check if all the received sales are aditable',
accessType: 'READ',
accepts: [{
arg: 'sales',
2022-10-31 13:13:06 +00:00
type: ['number'],
required: true
}],
returns: {
type: 'boolean',
root: true
},
http: {
2022-10-04 13:02:00 +00:00
path: `/canEdit`,
2022-10-31 13:13:06 +00:00
verb: 'GET'
}
});
Self.canEdit = async(ctx, sales, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2022-10-31 13:13:06 +00:00
const salesData = await models.Sale.find({
fields: ['id', 'itemFk', 'ticketFk'],
where: {id: {inq: sales}},
include:
{
relation: 'item',
scope: {
fields: ['id', 'isFloramondo'],
}
}
}, myOptions);
if (!salesData.length)
2023-05-24 10:59:58 +00:00
throw new UserError(`The sales do not exists`);
2022-10-31 13:13:06 +00:00
const ticketId = salesData[0].ticketFk;
2022-10-19 06:22:36 +00:00
2022-10-31 13:13:06 +00:00
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId, myOptions);
if (!isTicketEditable)
throw new UserError(`The sales of this ticket can't be modified`);
2022-10-19 06:22:36 +00:00
2022-10-31 13:13:06 +00:00
const hasSaleTracking = await models.SaleTracking.findOne({where: {saleFk: {inq: sales}}}, myOptions);
const hasSaleCloned = await models.SaleCloned.findOne({where: {saleClonedFk: {inq: sales}}}, myOptions);
const hasSaleFloramondo = salesData.find(sale => sale.item().isFloramondo);
2022-10-19 06:22:36 +00:00
2022-10-31 14:07:22 +00:00
const canEditTracked = await models.ACL.checkAccessAcl(ctx, 'Sale', 'editTracked');
const canEditCloned = await models.ACL.checkAccessAcl(ctx, 'Sale', 'editCloned');
const canEditFloramondo = await models.ACL.checkAccessAcl(ctx, 'Sale', 'editFloramondo');
const shouldEditTracked = canEditTracked || !hasSaleTracking;
const shouldEditCloned = canEditCloned || !hasSaleCloned;
2022-10-31 13:13:06 +00:00
const shouldEditFloramondo = canEditFloramondo || !hasSaleFloramondo;
if (!shouldEditTracked)
throw new UserError('It is not possible to modify tracked sales');
if (!shouldEditCloned)
throw new UserError('It is not possible to modify cloned sales');
if (!shouldEditFloramondo)
throw new UserError('It is not possible to modify sales that their articles are from Floramondo');
};
};