2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-06-28 13:25:09 +00:00
|
|
|
module.exports = Self => {
|
2021-05-26 07:14:18 +00:00
|
|
|
Self.remoteMethodCtx('updateQuantity', {
|
2018-06-28 13:25:09 +00:00
|
|
|
description: 'Changes the quantity of a sale',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-06-28 13:25:09 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
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))
|
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
|
|
|
|
2021-05-26 07:14:18 +00:00
|
|
|
let currentLine = await 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
|
|
|
};
|
|
|
|
};
|