salix/services/loopback/common/methods/sale/updateQuantity.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

let UserError = require('../../helpers').UserError;
module.exports = Self => {
Self.remoteMethod('updateQuantity', {
description: 'Changes the quantity of a sale',
accessType: '',
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(id, quantity) => {
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 Self.app.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});
};
};