salix/modules/ticket/back/methods/ticket/recalculateComponents.js

48 lines
1.3 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('recalculateComponents', {
description: 'Calculates the price of a sale and its components',
accessType: 'WRITE',
accepts: [{
arg: 'id',
description: 'The ticket id',
type: 'number',
required: true,
http: {source: 'path'}
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/:id/recalculateComponents`,
verb: 'POST'
}
});
Self.recalculateComponents = async(ctx, id, options) => {
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
await Self.isEditableOrThrow(ctx, id, myOptions);
const recalculation = await Self.rawSql('CALL vn.ticket_recalcComponents(?, NULL)', [id], myOptions);
if (tx) await tx.commit();
return recalculation;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};