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 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');
|
2022-10-10 11:06:49 +00:00
|
|
|
|
|
|
|
const shouldEditTracked = canEditTracked || !hasSaleTracking;
|
|
|
|
const shouldEditCloned = canEditCloned || !hasSaleCloned;
|
2022-10-31 13:13:06 +00:00
|
|
|
const shouldEditFloramondo = canEditFloramondo || !hasSaleFloramondo;
|
2022-10-10 11:06:49 +00:00
|
|
|
|
2023-01-31 08:04:45 +00:00
|
|
|
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');
|
|
|
|
|
|
|
|
return true;
|
2021-05-26 07:14:18 +00:00
|
|
|
};
|
|
|
|
};
|