module.exports = Self => { Self.remoteMethod('getSales', { description: 'New filter', accessType: 'READ', accepts: [{ arg: 'ticketFk', type: 'number', required: true, description: 'ticket id', http: {source: 'path'} }], returns: { type: ['Object'], root: true }, http: { path: `/:ticketFk/getSales`, verb: 'get' } }); Self.getSales = async ticketFk => { let query = `CALL vn.ticketGetVisibleAvailable(?)`; let [lines] = await Self.rawSql(query, [ticketFk]); let ids = []; for (line of lines) ids.push(line.itemFk); let filter = { fields: ['id'], where: {id: {inq: ids}}, include: { relation: 'tags', scope: { fields: ['tagFk', 'value', 'priority'], where: {priority: {lte: 6}}, order: 'priority', include: { relation: 'tag', scope: { fields: ['name'] } } } } }; let items = await Self.app.models.Item.find(filter); let map = {}; for (item of items) map[item.id] = item; for (line of lines) line.item = {tags: map[line.itemFk].tags()}; return lines; }; };