All files / modules/20-rbac/pages/RoleDetails utils.tsx

73.91% Statements 17/23
50% Branches 13/26
57.14% Functions 4/7
73.68% Lines 14/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53              10x 10x       10x 8x 8x 64x 64x 40x 24x     8x     10x           1x 1x           1x                              
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import produce from 'immer'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { ResourceType } from '@rbac/interfaces/ResourceType'
import type { Permission, PermissionResponse } from 'services/rbac'
 
export const getPermissionMap = (permissionList?: PermissionResponse[]): Map<ResourceType, Permission[]> => {
  const permissionsMap: Map<ResourceType, Permission[]> = new Map()
  permissionList?.map(response => {
    Eif (response.permission.resourceType) {
      if (permissionsMap.has(response.permission.resourceType as ResourceType)) {
        permissionsMap.get(response.permission.resourceType as ResourceType)?.push(response.permission)
      } else permissionsMap.set(response.permission.resourceType as ResourceType, [response.permission])
    }
  })
  return permissionsMap
}
 
export const onPermissionChange = (
  permission: string,
  isAdd: boolean,
  permissions: string[],
  setPermissions: (value: React.SetStateAction<string[]>) => void
): void => {
  Eif (isAdd) {
    Iif (
      permission === PermissionIdentifier.EDIT_DASHBOARD ||
      permissions.indexOf(PermissionIdentifier.EDIT_DASHBOARD) !== -1
    ) {
      setPermissions(_permissions => [...permissions, permission, PermissionIdentifier.VIEW_DASHBOARD])
    } else {
      setPermissions(_permissions => [...permissions, permission])
    }
  } else if (
    !(
      permission === PermissionIdentifier.VIEW_DASHBOARD &&
      permissions.indexOf(PermissionIdentifier.EDIT_DASHBOARD) !== -1
    )
  ) {
    setPermissions(_permissions =>
      produce(_permissions, draft => {
        draft?.splice(permissions.indexOf(permission), 1)
      })
    )
  }
}