All files / src/features/Permissions/Modal utils.ts

47.36% Statements 18/38
4.76% Branches 1/21
42.85% Functions 6/14
48.64% Lines 18/37

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                        9x                                         9x                                                                             9x     9x       17x 18x 18x             9x 171x   171x 284x   171x 170x       171x 284x   284x     171x     9x                                  
import sha1 from "sha1";
import { IUpdatedData, PermisionHashMap } from "./api/types";
 
type IFilterAssigned = -1 | 0 | 1 | 2;
type IFilter = {
  assigned: IFilterAssigned;
  custom: 0 | 1;
};
 
/*
Copy the assigned value from second PermisionHashMap into first PermisionHashMap
*/
export const copyData = (
  obj1: PermisionHashMap,
  obj2: PermisionHashMap,
  fieldKey: string,
) => {
  return Object.entries(obj1).reduce((acc, v) => {
    const [permissionKey, permissionValue] = v;
 
    return {
      ...acc,
      [permissionKey]: {
        ...permissionValue,
        [fieldKey]:
          obj2?.[permissionKey]?.isDeny === undefined
            ? !permissionValue.isDeny
            : !obj2[permissionKey].isDeny,
      },
    };
  }, {});
};
 
export const mergeData = (
  obj1: PermisionHashMap,
  obj2: { [key: string]: boolean },
) => {
  return Object.entries(obj1).reduce((acc, v) => {
    const [permissionKey, permissionValue] = v;
 
    return {
      ...acc,
      [permissionKey]: {
        ...permissionValue,
        assigned: obj2[permissionKey] ?? !permissionValue.isDeny,
      },
    };
  }, {});
};
 
/*
Update the field passed as input with the value laso from input into a PermisionHashMap
*/
export function updatePermissionObject(
  obj: PermisionHashMap,
  field: [key: string, value: any],
) {
  const [key, value] = field;
 
  return Object.entries(obj).reduce((acc, v) => {
    const [permissionKey, permissionValue] = v;
 
    return {
      ...acc,
      [permissionKey]: {
        ...permissionValue,
        [key]: value,
      },
    };
  }, {});
}
 
const defaultHashFn = (v: IUpdatedData): string =>
  `${v.id}-${v.permissionName.replace(/ /g, "")}`;
 
export const hashData = (
  data: IUpdatedData[],
  hashFn = defaultHashFn,
): PermisionHashMap => {
  return data.reduce((acc, v) => {
    const compoundKey = hashFn(v);
    return {
      ...acc,
      [sha1(compoundKey)]: v,
    };
  }, {} as PermisionHashMap);
};
 
export const groupData = (hashes: PermisionHashMap) => {
  Iif (!hashes) return {};
 
  const uniqueGroups = Array.from(
    new Set(Object.values(hashes).map((x) => x.group)),
  );
  const groupedData: { [k: string]: string[] } = uniqueGroups.reduce(
    (acc, v) => ({ ...acc, [v]: [] }),
    {},
  );
 
  Object.entries(hashes).forEach(([k, v]) => {
    const g = v.group;
 
    groupedData[g].push(k);
  });
 
  return groupedData;
};
 
export const filterViewedItems = (item: IUpdatedData, filter: IFilter) => {
  if (filter.assigned === -1 && filter.custom === 0) {
    return true;
  }
  if (filter.assigned !== -1 && filter.custom === 1) {
    return (
      !item.isDeny === Boolean(filter.assigned) &&
      (item as any)?.custom === Boolean(filter.custom)
    );
  }
  if (filter.assigned !== -1) {
    return !item.isDeny === Boolean(filter.assigned);
  }
  if (filter.custom === 1) {
    return (item as any)?.custom === Boolean(filter.custom);
  }
};