diff --git a/modules/ticket/back/methods/sale/updateDiscount.js b/modules/ticket/back/methods/sale/updateDiscount.js index 1c1db1927..e1793241b 100644 --- a/modules/ticket/back/methods/sale/updateDiscount.js +++ b/modules/ticket/back/methods/sale/updateDiscount.js @@ -1,7 +1,7 @@ let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethod('updateDiscount', { + Self.remoteMethodCtx('updateDiscount', { description: 'Changes the discount of a sale', accessType: '', accepts: [{ @@ -21,16 +21,12 @@ module.exports = Self => { } }); - Self.updateDiscount = async params => { + Self.updateDiscount = async(ctx, params) => { if (isNaN(params.editLines[0].discount)) throw new UserError(`The value should be a number`); let model = Self.app.models; let thisTicketIsEditable = await model.Ticket.isEditable(params.editLines[0].ticketFk); - - if (!thisTicketIsEditable) - throw new UserError(`The sales of this ticket can't be modified`); - let ticket = await model.Ticket.find({ where: { id: params.editLines[0].ticketFk @@ -41,9 +37,16 @@ module.exports = Self => { fields: ['salesPersonFk'] } }], - fields: ['id', 'clientFk'] + fields: ['id', 'clientFk', 'refFk'] }); + let userId = ctx.req.accessToken.userId; + let isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant'); + + if ((!thisTicketIsEditable && !isSalesAssistant) || (ticket.refFk && isSalesAssistant)) + throw new UserError(`The sales of this ticket can't be modified`); + + let componentToUse; let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'});