All files / src/features/Permissions/AccessControl formatPermissions.ts

5.88% Statements 1/17
0% Branches 0/19
0% Functions 0/2
7.14% Lines 1/14

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          499x                                                                                    
import { authorizations } from "@redux/slices/accessControl/initialState";
import { TUserPermissions } from "@redux/slices/accessControl/types";
import { store } from "@redux/store";
import { getAccessOperation } from "./utils";
 
const formatPermissions = (data?: any[]): TUserPermissions => {
  if (!data) return authorizations;
 
  return data.reduce((acc: TUserPermissions, v: any) => {
    const name = v?.resourceName?.replace("gs:rn:", "");
    const operation = getAccessOperation(v?.actionName);
    const action = v?.isDeny ? "deny" : "allow";
 
    if (!name || !operation || !action) return acc;
 
    if (!acc[name]) {
      return {
        ...acc,
        [name]: {
          [operation]: action,
        },
      };
    }
    if (acc[name][operation] === "deny") return acc;
    const {
      auth: {
        user: { role },
      },
    } = store.getState();
    if (
      (name in acc &&
        operation in acc[name] &&
        role.toLocaleLowerCase() !== v.categoryName?.toLowerCase()) ||
      ""
    )
      return acc;
 
    return {
      ...acc,
      [name]: {
        ...acc[name],
        [operation]: action,
      },
    };
  }, {});
};
 
export default formatPermissions