salix/back/methods/collection/setSaleQuantity.js

55 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

module.exports = Self => {
Self.remoteMethod('setSaleQuantity', {
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) => {
const models = Self.app.models;
2022-11-25 10:50:24 +00:00
const myOptions = {};
let tx;
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({
quantity
2022-11-25 10:50:24 +00:00
}, myOptions);
if (tx) await tx.commit();
return saleUpdated;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};