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 = {}; if (typeof options == 'object') Object.assign(myOptions, options); const isProduction = await models.VnUser.hasRole(userId, 'production', myOptions); const isSalesPerson = await models.VnUser.hasRole(userId, 'salesPerson', myOptions); const isAdministrative = await models.VnUser.hasRole(userId, 'administrative', myOptions); filter.where = {alertLevel: 0}; if (isSalesPerson) filter.where = {or: [{alertLevel: 0}, {code: 'PICKER_DESIGNED'}]}; if (isProduction || isAdministrative) delete filter.where; return models.State.find(filter, myOptions); }; };