2018-08-02 07:49:00 +00:00
|
|
|
let UserError = require('../../helpers').UserError;
|
|
|
|
|
2018-02-23 12:04:44 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('updateTaxes', {
|
|
|
|
description: 'Updates the item taxes',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The item id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}, {
|
2018-08-23 07:09:21 +00:00
|
|
|
arg: 'taxes',
|
2018-02-23 12:04:44 +00:00
|
|
|
type: ['object'],
|
|
|
|
required: true,
|
|
|
|
description: 'The list of taxes to update',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/updateTaxes`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-23 07:09:21 +00:00
|
|
|
Self.updateTaxes = async taxes => {
|
2018-02-23 12:04:44 +00:00
|
|
|
let promises = [];
|
2018-02-26 11:56:42 +00:00
|
|
|
for (let tax of taxes) {
|
|
|
|
if (!tax.taxClassFk)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError('Tax class cannot be blank');
|
2018-02-26 11:56:42 +00:00
|
|
|
|
2018-02-23 12:04:44 +00:00
|
|
|
promises.push(Self.app.models.ItemTaxCountry.updateAll(
|
|
|
|
{id: tax.id},
|
|
|
|
{taxClassFk: tax.taxClassFk}
|
|
|
|
));
|
2018-02-26 11:56:42 +00:00
|
|
|
}
|
2018-02-23 12:04:44 +00:00
|
|
|
await Promise.all(promises);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|