2022-10-04 11:22:11 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
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'],
|
2021-05-26 07:14:18 +00:00
|
|
|
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'
|
2021-05-26 07:14:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
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');
|
2022-10-10 11:06:49 +00:00
|
|
|
|
|
|
|
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-10 11:06:49 +00:00
|
|
|
|
2022-10-31 13:13:06 +00:00
|
|
|
return shouldEditTracked && shouldEditCloned && shouldEditWeekly && shouldEditFloramondo;
|
2021-05-26 07:14:18 +00:00
|
|
|
};
|
|
|
|
};
|