2022-11-23 14:15:40 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('getChanges', {
|
2022-11-24 14:04:15 +00:00
|
|
|
description: 'Check if a ticket is editable',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'the ticket id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}],
|
2022-11-23 14:15:40 +00:00
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/getChanges`,
|
2022-11-24 14:04:15 +00:00
|
|
|
verb: 'get'
|
2022-11-23 14:15:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
Self.getChanges = async(ctx, id, options) => {
|
2022-11-23 14:15:40 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
const ticketLogs = await models.TicketLog.find({where: {originFk: id}}, myOptions);
|
2022-11-23 14:15:40 +00:00
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
const changes = [];
|
|
|
|
for (const ticketLog of ticketLogs) {
|
|
|
|
const isUpdate = ticketLog.action == 'update';
|
2022-11-23 14:15:40 +00:00
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
if (ticketLog.description && isUpdate)
|
|
|
|
changes.push(ticketLog.description);
|
2022-11-23 14:15:40 +00:00
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
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}`);
|
|
|
|
}
|
2022-11-23 14:15:40 +00:00
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
if (ticketLog.changedModel == 'Ticket' && isUpdate && ticketLog.newInstance.quantity)
|
|
|
|
changes.push(`${ticketLog.oldInstance.concept} cambia de ${oldQuantity} a ${newQuantity}`);
|
2022-11-23 14:15:40 +00:00
|
|
|
}
|
2022-11-24 14:04:15 +00:00
|
|
|
return changes.join('\n');
|
2022-11-23 14:15:40 +00:00
|
|
|
};
|
|
|
|
};
|