module.exports = Self => {
    Self.remoteMethodCtx('isEditable', {
        description: 'Check if an state is editable',
        accessType: 'READ',
        accepts: [{
            arg: 'id',
            type: 'number',
            required: true,
            description: 'the state 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 models = Self.app.models;
        const myOptions = {};

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        const state = await models.ClaimState.findById(id, {
            include: {
                relation: 'writeRole'
            }
        }, myOptions);
        const roleWithGrants = state && state.writeRole().name;
        return await models.VnUser.hasRole(userId, roleWithGrants, myOptions);
    };
};