salix/modules/ticket/back/methods/sale/updateQuantity.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('updateQuantity', {
description: 'Changes the quantity of a sale',
2019-05-22 10:35:24 +00:00
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'sale ID',
http: {source: 'path'}
}, {
arg: 'quantity',
type: 'number',
required: true,
description: 'newQuantity'
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/:id/updateQuantity`,
verb: 'post'
}
});
Self.updateQuantity = async(ctx, id, quantity) => {
const models = Self.app.models;
const canEditSale = await models.Sale.canEdit(ctx, [id]);
if (!canEditSale)
throw new UserError(`Sale(s) blocked, please contact production`);
2018-07-16 12:24:03 +00:00
if (isNaN(quantity))
throw new UserError(`The value should be a number`);
2018-07-16 07:19:38 +00:00
let currentLine = await models.Sale.findOne({where: {id: id}});
if (quantity > currentLine.quantity)
throw new UserError('The new quantity should be smaller than the old one');
return await currentLine.updateAttributes({quantity: quantity});
};
};