let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('removes', { description: 'Change the state of a ticket', accessType: 'WRITE', accepts: [{ arg: 'params', type: 'object', required: true, description: '[sales IDs], actualTicketFk', http: {source: 'body'} }], returns: { type: 'string', root: true }, http: { path: `/removes`, verb: 'post' } }); Self.removes = async(ctx, params) => { let thisTicketIsEditable = await Self.app.models.Ticket.isEditable(ctx, params.actualTicketFk); if (!thisTicketIsEditable) throw new UserError(`The sales of this ticket can't be modified`); let promises = []; for (let i = 0; i < params.sales.length; i++) promises.push(Self.app.models.Sale.destroyById(params.sales[i].id)); return Promise.all(promises); }; };