const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { Self.remoteMethodCtx('editableStates', { description: 'Gets the editable states according the user role ', accessType: 'READ', accepts: { arg: 'filter', type: 'object' }, returns: { type: ['object'], root: true }, http: { path: `/editableStates`, verb: 'GET' } }); Self.editableStates = async(ctx, filter, options) => { const models = Self.app.models; const userId = ctx.req.accessToken.userId; const myOptions = {...(options || {})}; const isProduction = await models.VnUser.hasRole(userId, 'production', myOptions); const isAdministrative = await models.VnUser.hasRole(userId, 'administrative', myOptions); const where = (isProduction || isAdministrative) ? undefined : {alertLevel: 0}; filter = mergeFilters(filter, {where}); const states = await models.State.find(filter, myOptions); return states; }; };