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 = []; let salesIds = []; for (line of lines) { ids.push(line.itemFk); salesIds.push(line.id); } let filter = { fields: ['id', 'name', 'tag5', 'value5', 'tag6', 'value6', 'tag7', 'value7', 'tag8', 'value8', 'tag9', 'value9', 'tag10', 'value10'], where: {id: {inq: ids}} }; let items = await Self.app.models.Item.find(filter); filter = { fields: ['claimFk', 'saleFk'], where: {saleFk: {inq: salesIds}}, }; let claims = await Self.app.models.ClaimBeginning.find(filter); let map = {}; for (item of items) map[item.id] = item; let claimMap = {}; for (claim of claims) claimMap[claim.saleFk] = claim; for (line of lines) { line.tags = map[line.itemFk]; line.claim = claimMap[line.id]; } return lines; }; };