module.exports = Self => { Self.remoteMethodCtx('getChanges', { description: 'Get changues in the sales of a ticket', accessType: 'READ', accepts: [{ arg: 'id', type: 'number', required: true, description: 'the ticket id', http: {source: 'path'} }], returns: { type: 'object', root: true }, http: { path: `/:id/getChanges`, verb: 'get' } }); Self.getChanges = async(ctx, id, options) => { const models = Self.app.models; const myOptions = {}; const $t = ctx.req.__; // $translate if (typeof options == 'object') Object.assign(myOptions, options); const ticketLogs = await models.TicketLog.find( { where: { and: [ {originFk: id}, {action: 'update'}, {changedModel: 'Sale'} ] }, fields: [ 'oldInstance', 'newInstance', 'changedModelId' ], }, myOptions); const changes = []; for (const ticketLog of ticketLogs) { const oldQuantity = ticketLog.oldInstance.quantity; const newQuantity = ticketLog.newInstance.quantity; if (oldQuantity || newQuantity) { const sale = await models.Sale.findById(ticketLog.changedModelId, null, myOptions); const message = $t('Change quantity', { concept: sale.concept, oldQuantity: oldQuantity || 0, newQuantity: newQuantity || 0, }); changes.push(message); } } return changes.join('\n'); }; };