Update naming & simplify filter

This commit is contained in:
Alexandre Bodin 2021-04-09 16:20:53 +02:00
parent 8fa8c9b4fa
commit d09fe6f492
3 changed files with 10 additions and 16 deletions

View File

@ -37,7 +37,7 @@ module.exports = {
const { sectionsBuilder, conditionProvider } = getService('permission');
const actions = await getService('action').getActionsByRoleId(roleId);
const actions = await getService('action').getAllowedActionsForRole(roleId);
const conditions = conditionProvider.values();
const sections = await sectionsBuilder.build(actions);

View File

@ -1,6 +1,6 @@
'use strict';
const { getActionsByRoleId } = require('../action');
const { getAllowedActionsForRole } = require('../action');
const { AUTHOR_CODE, PUBLISH_ACTION } = require('../constants');
const fixtures = [
@ -16,7 +16,7 @@ const fixtures = [
];
describe('Action', () => {
describe('getActionByRoleId', () => {
describe('getAllowedActionsForRole', () => {
test('returns every action if role is not provided', async () => {
global.strapi = {
admin: {
@ -32,7 +32,7 @@ describe('Action', () => {
},
};
const actions = await getActionsByRoleId();
const actions = await getAllowedActionsForRole();
expect(actions.length).toBe(fixtures.length);
expect(actions).toEqual(expect.arrayContaining(fixtures));
});
@ -58,7 +58,7 @@ describe('Action', () => {
},
};
const actions = await getActionsByRoleId(roleId);
const actions = await getAllowedActionsForRole(roleId);
expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId });
expect(actions.length).toBe(fixtures.length);
@ -86,7 +86,7 @@ describe('Action', () => {
},
};
const actions = await getActionsByRoleId(roleId);
const actions = await getAllowedActionsForRole(roleId);
expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId });
expect(actions.length).toBe(fixtures.length - 1);

View File

@ -8,10 +8,10 @@ const { AUTHOR_CODE, PUBLISH_ACTION } = require('./constants');
/**
* Returns actions available for a role.
* @param {object} roleId
* @param {string|number} roleId
* @returns {object[]}
*/
const getActionsByRoleId = async roleId => {
const getAllowedActionsForRole = async roleId => {
const { actionProvider } = getService('permission');
if (!isNil(roleId)) {
@ -22,13 +22,7 @@ const getActionsByRoleId = async roleId => {
}
if (role.code === AUTHOR_CODE) {
return actionProvider.values().filter(action => {
if (action.actionId !== PUBLISH_ACTION) {
return true;
}
return false;
});
return actionProvider.values().filter(({ actionId }) => actionId !== PUBLISH_ACTION);
}
}
@ -36,5 +30,5 @@ const getActionsByRoleId = async roleId => {
};
module.exports = {
getActionsByRoleId,
getAllowedActionsForRole,
};