module.exports = Self => { Self.remoteMethod('getSalesPersonMana', { description: 'Returns the mana amount of a salesperson for a ticket', accessType: 'READ', accepts: [{ arg: 'id', type: 'number', required: true, description: 'The ticket id', http: {source: 'path'} }], returns: { root: true }, http: { path: `/:id/getSalesPersonMana`, verb: 'GET' } }); Self.getSalesPersonMana = async(ticketId, options) => { const myOptions = {}; const models = Self.app.models; if (typeof options == 'object') Object.assign(myOptions, options); const ticket = await models.Ticket.findById(ticketId, { include: [{ relation: 'client', scope: { fields: ['salesPersonFk'] } }], fields: ['id', 'clientFk'] }, myOptions); if (!ticket) return 0; const mana = await models.WorkerMana.findOne({ where: { workerFk: ticket.client().salesPersonFk }, fields: 'amount' }, myOptions); return mana ? mana.amount : 0; }; };