59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethod('updateTaxes', {
|
|
description: 'Updates the item taxes',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'taxes',
|
|
type: ['object'],
|
|
required: true,
|
|
description: 'The list of taxes to update',
|
|
http: {source: 'body'}
|
|
}],
|
|
returns: {
|
|
type: 'boolean',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/updateTaxes`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.updateTaxes = async(taxes, options) => {
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const promises = [];
|
|
|
|
for (let tax of taxes) {
|
|
if (!tax.taxClassFk)
|
|
throw new UserError('Tax class cannot be blank');
|
|
|
|
promises.push(Self.app.models.ItemTaxCountry.updateAll(
|
|
{id: tax.id},
|
|
{taxClassFk: tax.taxClassFk}
|
|
), myOptions);
|
|
}
|
|
await Promise.all(promises);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return true;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|