56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
|
|
module.exports = Self => {
|
|
Self.remoteMethod('updateClaimDestination', {
|
|
description: 'Update a claim with privileges',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'rows',
|
|
type: ['object'],
|
|
required: true,
|
|
description: `the sales which will be modified the claimDestinationFk`
|
|
}, {
|
|
arg: 'claimDestinationFk',
|
|
type: 'number',
|
|
required: true
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/updateClaimDestination`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.updateClaimDestination = async(rows, claimDestinationFk, options) => {
|
|
const tx = await Self.beginTransaction({});
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction)
|
|
myOptions.transaction = tx;
|
|
|
|
try {
|
|
const models = Self.app.models;
|
|
const promises = [];
|
|
for (let row of rows) {
|
|
const claimEnd = await models.ClaimEnd.findById(row.id, null, myOptions);
|
|
const updatedClaimEnd = claimEnd.updateAttribute('claimDestinationFk', claimDestinationFk, myOptions);
|
|
promises.push(updatedClaimEnd);
|
|
}
|
|
|
|
const updatedSales = await Promise.all(promises);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedSales;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|