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

65 lines
2.4 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);
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 isTicketWeekly = await models.TicketWeekly.findOne({where: {ticketFk: ticketId}}, myOptions);
const hasSaleFloramondo = salesData.find(sale => sale.item().isFloramondo);
2022-10-19 06:22:36 +00:00
2022-10-31 13:13:06 +00:00
const canEditTracked = await models.ACL.checkAccess(ctx, 'Sale', 'editTracked');
const canEditCloned = await models.ACL.checkAccess(ctx, 'Sale', 'editCloned');
const canEditWeekly = await models.ACL.checkAccess(ctx, 'Ticket', 'editWeekly');
const canEditFloramondo = await models.ACL.checkAccess(ctx, 'Sale', 'editFloramondo');
const shouldEditTracked = canEditTracked || !hasSaleTracking;
const shouldEditCloned = canEditCloned || !hasSaleCloned;
const shouldEditWeekly = canEditWeekly || !isTicketWeekly;
2022-10-31 13:13:06 +00:00
const shouldEditFloramondo = canEditFloramondo || !hasSaleFloramondo;
2022-10-31 13:13:06 +00:00
return shouldEditTracked && shouldEditCloned && shouldEditWeekly && shouldEditFloramondo;
};
};