2022-02-08 11:41:55 +00:00
|
|
|
module.exports = Self => {
|
2022-05-06 11:09:37 +00:00
|
|
|
Self.remoteMethod('setSaleQuantity', {
|
2022-02-08 11:41:55 +00:00
|
|
|
description: 'Update sale quantity',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'saleId',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The sale id'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The quantity to picked'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/setSaleQuantity`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-25 09:10:25 +00:00
|
|
|
Self.setSaleQuantity = async(saleId, quantity, options) => {
|
2022-02-08 11:41:55 +00:00
|
|
|
const models = Self.app.models;
|
2022-11-25 10:50:24 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2022-02-08 11:41:55 +00:00
|
|
|
|
2022-11-25 10:50:24 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const sale = await models.Sale.findById(saleId, null, myOptions);
|
|
|
|
const saleUpdated = await sale.updateAttributes({
|
|
|
|
originalQuantity: sale.quantity,
|
|
|
|
quantity: quantity
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return saleUpdated;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2022-02-08 11:41:55 +00:00
|
|
|
};
|
|
|
|
};
|