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

56 lines
2.0 KiB
JavaScript

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: {
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);
const firstSale = await models.Sale.findById(sales[0], null, myOptions);
const isTicketEditable = await models.Ticket.isEditable(ctx, firstSale.ticketFk, myOptions);
if (!isTicketEditable)
throw new UserError(`The sales of this ticket can't be modified`);
const saleTracking = await models.SaleTracking.find({where: {saleFk: {inq: sales}}}, myOptions);
const hasSaleTracking = saleTracking.length;
const saleCloned = await models.SaleCloned.find({where: {saleClonedFk: {inq: sales}}}, myOptions);
const hasSaleCloned = saleCloned.length;
const isTicketWeekly =
await models.TicketWeekly.findOne({where: {ticketFk: firstSale.ticketFk}}, myOptions);
const canEditTracked = await models.Account.hasFuncionalityAcl(ctx, 'Sale', 'editTracked');
const canEditCloned = await models.Account.hasFuncionalityAcl(ctx, 'Sale', 'editCloned');
const canEditWeekly = await models.Account.hasFuncionalityAcl(ctx, 'Ticket', 'editWeekly');
const shouldEditTracked = canEditTracked || !hasSaleTracking;
const shouldEditCloned = canEditCloned || !hasSaleCloned;
const shouldEditWeekly = canEditWeekly || !isTicketWeekly;
const canEdit = shouldEditTracked && shouldEditCloned && shouldEditWeekly;
return canEdit;
};
};