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',
|
|
|
|
type: ['object'],
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/isEditable`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.canEdit = async(ctx, sales, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-10-04 11:22:11 +00:00
|
|
|
/* 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`);*/
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-04 11:22:11 +00:00
|
|
|
const saleTracking = await models.SaleTracking.find({where: {saleFk: {inq: sales}}}, myOptions);
|
2021-05-26 07:14:18 +00:00
|
|
|
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 isProductionRole = await models.Account.hasRole(userId, 'production', myOptions);
|
|
|
|
const canEdit = (isProductionRole || !hasSaleTracking);// && (isRole || !hasSaleCloned);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-04 11:22:11 +00:00
|
|
|
const isRole = await models.Account.hasRole(userId, 'developer', myOptions);*/
|
|
|
|
const editTracked = await models.Account.aclFunc(ctx, 'editTracked');
|
|
|
|
const editCloned = await models.Account.aclFunc(ctx, 'editCloned');
|
|
|
|
console.log(editTracked);
|
|
|
|
const canEdit = (editTracked || !hasSaleTracking) && (editCloned || !hasSaleCloned);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2022-10-04 11:22:11 +00:00
|
|
|
return canEdit;// && isTicketEditable;
|
2021-05-26 07:14:18 +00:00
|
|
|
};
|
|
|
|
};
|