11import { IntlShape } from '@edx/frontend-platform/i18n' ;
22import { actionKeys } from '@src/authz-module/components/RoleCard/constants' ;
3+ import { PermissionMetadata , ResourceMetadata , Role } from '@src/types' ;
34import actionMessages from '../components/RoleCard/messages' ;
4- import { PermissionMetadata , ResourceMetadata , Role } from 'types' ;
55
66/**
77 * Derives the localized label and action key for a given permission.
@@ -18,13 +18,13 @@ import { PermissionMetadata, ResourceMetadata, Role } from 'types';
1818 *
1919 * @returns An object containing:
2020 * - `label`: The human-readable, localized label for the permission.
21- * - `actionKey`: A string representing icon to be displayed (e.g., `'Read'`, `'Edit'`), or `undefined` if not matched.
21+ * - `actionKey`: A string representing icon to be displayed (e.g., `'Read'`, `'Edit'`), or '' if not matched.
2222 */
2323function getPermissionMetadata (
2424 permission : PermissionMetadata ,
2525 intl : IntlShape ,
26- ) : { label : string ; actionKey : string | undefined } {
27- const actionKey = actionKeys . find ( action => permission . key . includes ( action ) ) ;
26+ ) : { label : string ; actionKey : string } {
27+ const actionKey = actionKeys . find ( action => permission . key . includes ( action ) ) || '' ;
2828 let messageKey = `authz.permissions.actions.${ actionKey } ` ;
2929 let messageResource = '' ;
3030
@@ -48,13 +48,12 @@ const buildPermissionsByRoleMatrix = ({
4848 const allowedPermissions = new Set ( rolePermissions ) ;
4949
5050 permissions . forEach ( ( permission ) => {
51- const resourceLabel =
52- resources . find ( ( r ) => r . key === permission . resource ) ?. label ||
53- permission . resource ;
51+ const resourceLabel = resources . find ( ( r ) => r . key === permission . resource ) ?. label
52+ || permission . resource ;
5453
5554 const { label, actionKey } = getPermissionMetadata ( permission , intl ) ;
5655
57- if ( ! actionKey ) return ; // Skip unknown actions
56+ if ( ! actionKey ) { return ; } // Skip unknown actions
5857
5958 // Initialize resource group if not already present
6059 if ( ! permissionsMatrix [ permission . resource ] ) {
@@ -75,15 +74,13 @@ const buildPermissionsByRoleMatrix = ({
7574 return Object . values ( permissionsMatrix ) ;
7675} ;
7776
78-
79-
80-
81- type PermissionMatrix = {
77+ export type PermissionMatrix = {
8278 resource : string ;
8379 resourceLabel : string ;
8480 permissions : {
8581 key : string ;
8682 label : string ;
83+ actionKey : string ;
8784 roles : Record < string , boolean > ;
8885 } [ ] ;
8986} [ ] ;
@@ -109,12 +106,12 @@ export function buildPermissionMatrix(
109106 intl : IntlShape ,
110107) : PermissionMatrix {
111108 const permissionsByResource = permissions . reduce < Record < string , PermissionMetadata [ ] > > ( ( acc , perm ) => {
112- if ( ! acc [ perm . resource ] ) acc [ perm . resource ] = [ ] ;
109+ if ( ! acc [ perm . resource ] ) { acc [ perm . resource ] = [ ] ; }
113110 acc [ perm . resource ] . push ( perm ) ;
114111 return acc ;
115112 } , { } ) ;
116113
117- const matrix : PermissionMatrix = resources . map ( resource => {
114+ const matrix = resources . map ( resource => {
118115 const resourcePermissions = permissionsByResource [ resource . key ] || [ ] ;
119116
120117 const permissionRows = resourcePermissions . map ( permission => {
@@ -144,5 +141,4 @@ export function buildPermissionMatrix(
144141 return matrix ;
145142}
146143
147-
148144export { buildPermissionsByRoleMatrix } ;
0 commit comments