2019-11-14 13:19:39 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
2019-11-21 11:00:56 +00:00
|
|
|
Self.remoteMethodCtx('recalculatePrice', {
|
2019-11-14 13:19:39 +00:00
|
|
|
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: {
|
2019-11-21 11:00:56 +00:00
|
|
|
path: `/:id/recalculatePrice`,
|
2019-11-14 13:19:39 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-21 11:00:56 +00:00
|
|
|
Self.recalculatePrice = async(ctx, id) => {
|
2019-11-14 13:19:39 +00:00
|
|
|
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`);
|
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
const canEditSale = await models.Sale.canEdit(ctx, [id]);
|
|
|
|
|
|
|
|
if (!canEditSale)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
|
|
|
|
2020-01-15 12:07:33 +00:00
|
|
|
return Self.rawSql('CALL vn.sale_calculateComponent(?, null)', [id]);
|
2019-11-14 13:19:39 +00:00
|
|
|
};
|
|
|
|
};
|