35 lines
982 B
JavaScript
35 lines
982 B
JavaScript
|
const UserError = require('vn-loopback/util/user-error');
|
||
|
module.exports = Self => {
|
||
|
Self.remoteMethodCtx('calculate', {
|
||
|
description: 'Calculates the price of a sale and its components',
|
||
|
accessType: 'WRITE',
|
||
|
accepts: [{
|
||
|
arg: 'id',
|
||
|
description: 'The sale id',
|
||
|
type: 'number',
|
||
|
required: true,
|
||
|
http: {source: 'path'}
|
||
|
}],
|
||
|
returns: {
|
||
|
type: 'Number',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: `/:id/calculate`,
|
||
|
verb: 'post'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.calculate = async(ctx, id) => {
|
||
|
const models = Self.app.models;
|
||
|
|
||
|
const sale = await Self.findById(id);
|
||
|
const isEditable = await models.Ticket.isEditable(ctx, sale.ticketFk);
|
||
|
|
||
|
if (!isEditable)
|
||
|
throw new UserError(`The sales of this ticket can't be modified`);
|
||
|
|
||
|
return Self.rawSql('CALL vn.ticketCalculateSale(?)', [id]);
|
||
|
};
|
||
|
};
|