salix/modules/ticket/back/methods/ticket-log/getChanges.js

52 lines
1.8 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('getChanges', {
description: 'Check if a ticket is editable',
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 = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const ticketLogs = await models.TicketLog.find({where: {originFk: id}}, myOptions);
const changes = [];
for (const ticketLog of ticketLogs) {
const isUpdate = ticketLog.action == 'update';
if (ticketLog.description && isUpdate)
changes.push(ticketLog.description);
const oldQuantity = ticketLog.oldInstance ? ticketLog.oldInstance.quantity : null;
const newQuantity = ticketLog.newInstance ? ticketLog.newInstance.quantity : null;
if (ticketLog.changedModel == 'Sale' && isUpdate && ticketLog.newInstance.quantity) {
const item = await models.Item.findById(ticketLog.changedModelId, null, myOptions);
changes.push(`${item.name} cambia de ${oldQuantity} a ${newQuantity}`);
}
if (ticketLog.changedModel == 'Ticket' && isUpdate && ticketLog.newInstance.quantity)
changes.push(`${ticketLog.oldInstance.concept} cambia de ${oldQuantity} a ${newQuantity}`);
}
return changes.join('\n');
};
};