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

77 lines
2.2 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('getChanges', {
2022-11-25 10:51:07 +00:00
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: {
2023-04-21 10:49:03 +00:00
or: [
{
and: [
{originFk: id},
{action: 'update'},
{changedModel: 'Sale'}
]
},
{
and: [
{originFk: id},
{action: 'delete'},
{changedModel: 'Sale'}
]
}
]
},
fields: [
'oldInstance',
'newInstance',
2023-04-21 10:49:03 +00:00
'changedModelId',
'changedModelValue'
],
}, myOptions);
const changes = [];
2023-04-21 10:49:03 +00:00
for (const log of ticketLogs) {
const oldQuantity = log.oldInstance.quantity;
const newQuantity = log.newInstance?.quantity || 0;
2023-04-21 10:49:03 +00:00
if (oldQuantity || newQuantity) {
const changeMessage = $t('Change quantity', {
concept: log.changedModelValue,
oldQuantity: oldQuantity || 0,
newQuantity: newQuantity || 0,
});
changes.push(changeMessage);
}
2023-04-21 10:49:03 +00:00
}
return changes.join('\n');
};
};