77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
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: {
|
|
or: [
|
|
{
|
|
and: [
|
|
{originFk: id},
|
|
{action: 'update'},
|
|
{changedModel: 'Sale'}
|
|
]
|
|
},
|
|
{
|
|
and: [
|
|
{originFk: id},
|
|
{action: 'delete'},
|
|
{changedModel: 'Sale'}
|
|
]
|
|
}
|
|
]
|
|
},
|
|
fields: [
|
|
'oldInstance',
|
|
'newInstance',
|
|
'changedModelId',
|
|
'changedModelValue'
|
|
],
|
|
}, myOptions);
|
|
|
|
const changes = [];
|
|
|
|
for (const log of ticketLogs) {
|
|
const oldQuantity = log.oldInstance.quantity;
|
|
const newQuantity = log.newInstance?.quantity || 0;
|
|
|
|
if (oldQuantity || newQuantity) {
|
|
const changeMessage = $t('Change quantity', {
|
|
concept: log.changedModelValue,
|
|
oldQuantity: oldQuantity || 0,
|
|
newQuantity: newQuantity || 0,
|
|
});
|
|
changes.push(changeMessage);
|
|
}
|
|
}
|
|
|
|
return changes.join('\n');
|
|
};
|
|
};
|