2023-05-18 06:58:13 +00:00
|
|
|
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
2019-08-08 06:33:27 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('editableStates', {
|
|
|
|
description: 'Gets the editable states according the user role ',
|
|
|
|
accessType: 'READ',
|
2020-03-26 11:00:07 +00:00
|
|
|
accepts: {
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'object'
|
|
|
|
},
|
2019-08-08 06:33:27 +00:00
|
|
|
returns: {
|
2021-10-07 07:57:07 +00:00
|
|
|
type: ['object'],
|
2019-08-08 06:33:27 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/editableStates`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
Self.editableStates = async(ctx, filter, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
2023-05-18 06:58:13 +00:00
|
|
|
const myOptions = {...(options || {})};
|
2019-08-08 06:33:27 +00:00
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const isProduction = await models.VnUser.hasRole(userId, 'production', myOptions);
|
|
|
|
const isAdministrative = await models.VnUser.hasRole(userId, 'administrative', myOptions);
|
2019-08-08 06:33:27 +00:00
|
|
|
|
2023-05-18 07:21:30 +00:00
|
|
|
const where = (isProduction || isAdministrative) ? undefined : {alertLevel: 0};
|
2023-05-04 12:51:47 +00:00
|
|
|
|
2023-05-18 06:58:13 +00:00
|
|
|
filter = mergeFilters(filter, {where});
|
2019-08-08 06:33:27 +00:00
|
|
|
|
2023-05-18 07:21:30 +00:00
|
|
|
const states = await models.State.find(filter, myOptions);
|
|
|
|
|
|
|
|
return states;
|
2019-08-08 06:33:27 +00:00
|
|
|
};
|
|
|
|
};
|