const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { Self.remoteMethod('getTickets', { description: 'Returns tickets for one invoiceOut', accessType: 'READ', accepts: [{ arg: 'id', type: 'number', required: true, description: 'The invoiceOut id', http: {source: 'path'} }, { arg: 'filter', type: 'object', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string' } ], returns: { type: ['object'], root: true }, http: { path: `/:id/getTickets`, verb: 'GET' } }); Self.getTickets = async(id, filter, options) => { const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const invoiceOut = await models.InvoiceOut.findById(id, {fields: 'ref'}, myOptions); let defaultFilter = { where: {refFk: invoiceOut.ref}, fields: [ 'id', 'nickname', 'shipped', 'totalWithVat', 'clientFk' ] }; defaultFilter = mergeFilters(defaultFilter, filter); return models.Ticket.find(defaultFilter, myOptions); }; };