2018-08-02 07:49:00 +00:00
|
|
|
let UserError = require('../../helpers').UserError;
|
|
|
|
|
2018-06-28 13:25:09 +00:00
|
|
|
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))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The value should be a number`);
|
2018-07-16 07:19:38 +00:00
|
|
|
|
2018-10-08 09:02:56 +00:00
|
|
|
let currentLine = await Self.app.models.Sale.findOne({where: {id: id}});
|
2018-06-28 13:25:09 +00:00
|
|
|
if (quantity > currentLine.quantity)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError('The new quantity should be smaller than the old one');
|
2018-06-28 13:25:09 +00:00
|
|
|
|
2018-10-08 09:02:56 +00:00
|
|
|
return await currentLine.updateAttributes({quantity: quantity});
|
2018-06-28 13:25:09 +00:00
|
|
|
};
|
|
|
|
};
|