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

48 lines
1.6 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',
type: ['object'],
required: true
}],
returns: {
type: 'boolean',
root: true
},
http: {
2022-10-04 13:02:00 +00:00
path: `/canEdit`,
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-04 13:02:00 +00:00
const firstSale = await models.Sale.findById(sales[0], null, myOptions);
2022-10-04 11:22:11 +00:00
const isTicketEditable = await models.Ticket.isEditable(ctx, firstSale.ticketFk, myOptions);
if (!isTicketEditable)
2022-10-04 13:02:00 +00:00
throw new UserError(`The sales of this ticket can't be modified`);
2022-10-04 11:22:11 +00:00
const saleTracking = await models.SaleTracking.find({where: {saleFk: {inq: sales}}}, myOptions);
const hasSaleTracking = saleTracking.length;
2022-10-04 11:22:11 +00:00
const saleCloned = await models.SaleCloned.find({where: {saleClonedFk: {inq: sales}}}, myOptions);
const hasSaleCloned = saleCloned.length;
const canEditTracked = await models.Account.funcionalityAcl(ctx, 'Sale', 'editTracked');
const canEditCloned = await models.Account.funcionalityAcl(ctx, 'Sale', 'editCloned');
2022-10-04 13:02:00 +00:00
const canEdit = (canEditTracked || !hasSaleTracking) && (canEditCloned || !hasSaleCloned);
2022-10-04 13:02:00 +00:00
return canEdit;
};
};