2022-11-23 14:15:40 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('getChanges', {
|
2022-11-25 10:51:07 +00:00
|
|
|
description: 'Get changues in the sales of a ticket',
|
2022-11-24 14:04:15 +00:00
|
|
|
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 = {};
|
2022-11-30 12:29:39 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2022-11-23 14:15:40 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-11-30 12:29:39 +00:00
|
|
|
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'}
|
|
|
|
]
|
|
|
|
}
|
2022-11-30 12:29:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
'oldInstance',
|
|
|
|
'newInstance',
|
2023-04-21 10:49:03 +00:00
|
|
|
'changedModelId',
|
|
|
|
'changedModelValue'
|
2022-11-30 12:29:39 +00:00
|
|
|
],
|
|
|
|
}, myOptions);
|
2022-11-23 14:15:40 +00:00
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
const changes = [];
|
2022-11-23 14:15:40 +00:00
|
|
|
|
2023-04-21 10:49:03 +00:00
|
|
|
for (const log of ticketLogs) {
|
|
|
|
const oldQuantity = log.oldInstance.quantity;
|
|
|
|
const newQuantity = log.newInstance?.quantity || 0;
|
2022-11-23 14:15:40 +00:00
|
|
|
|
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);
|
2022-11-30 12:29:39 +00:00
|
|
|
}
|
2023-04-21 10:49:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 14:04:15 +00:00
|
|
|
return changes.join('\n');
|
2022-11-23 14:15:40 +00:00
|
|
|
};
|
|
|
|
};
|