salix/modules/claim/back/methods/claim/updateClaimAction.js

60 lines
1.5 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('updateClaimAction', {
description: 'Update a claim with privileges',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'Claim id',
http: {source: 'path'}
}, {
arg: 'responsibility',
type: 'number',
required: false
}, {
arg: 'isChargedToMana',
type: 'boolean',
required: false
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/updateClaimAction`,
verb: 'patch'
}
});
Self.updateClaimAction = async(ctx, id, options) => {
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const models = Self.app.models;
const claim = await models.Claim.findById(id, null, myOptions);
const args = ctx.args;
delete args.ctx;
const updatedClaim = await claim.updateAttributes(args, myOptions);
if (tx) await tx.commit();
return updatedClaim;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};