salix/modules/item/back/methods/item-shelving/updateFromSale.js

49 lines
1.3 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('updateFromSale', {
description: 'Update the visible items',
accessType: 'WRITE',
accepts: [{
arg: 'saleFk',
type: 'number',
required: true,
}],
http: {
path: `/updateFromSale`,
verb: 'POST'
}
});
Self.updateFromSale = async(saleFk, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const itemShelvingSale = await models.ItemShelvingSale.findOne({
where: {saleFk},
include: {relation: 'itemShelving'}
}, myOptions);
const itemShelving = itemShelvingSale.itemShelving();
const quantity = itemShelving.visible + itemShelvingSale.quantity;
await itemShelving.updateAttributes(
{visible: quantity},
myOptions
);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};