let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('moveToTicket', { description: 'Moves a line to a given ticket', accessType: '', accepts: [{ arg: 'params', type: 'object', required: true, description: '[sales IDs], newTicketFk, actualTicketFk', http: {source: 'body'} }], returns: { type: 'string', root: true }, http: { path: `/moveToTicket`, verb: 'post' } }); Self.moveToTicket = async params => { let thisTicketIsEditable = await Self.app.models.Ticket.isEditable(params.actualTicketFk); if (!thisTicketIsEditable) throw new UserError(`The sales of this ticket can't be modified`); let newTicketIsEditable = await Self.app.models.Ticket.isEditable(params.newTicketFk); if (!newTicketIsEditable) throw new UserError(`The sales of this ticket can't be modified`); for (let i = 0; i < params.sales.length; i++) await Self.app.models.Sale.update({id: params.sales[i].id}, {ticketFk: params.newTicketFk}); }; };