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

46 lines
1.2 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('isEditable', {
description: 'Check if a claim is editable',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'the claim id',
http: {source: 'path'}
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/isEditable`,
verb: 'get'
}
});
Self.isEditable = async(ctx, id, options) => {
const userId = ctx.req.accessToken.userId;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager', myOptions);
const claim = await Self.app.models.Claim.findById(id, {
fields: ['claimStateFk'],
include: [{
relation: 'claimState'
}]
}, myOptions);
const isClaimResolved = claim && claim.claimState().code == 'resolved';
if (!claim || (isClaimResolved && !isClaimManager))
return false;
return true;
};
};