All files / react-app/src/features/user-roles utils.tsx

94.28% Statements 33/35
86.95% Branches 20/23
100% Functions 9/9
92.3% Lines 24/26

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                                5x 7x   155x 155x   6x 375x 375x   375x 220x 114x   6x   6x     5x         5x     4x 4x 10x       2x   6x   2x         4x 12x   10x   12x    
export type StatusType = "active" | "pending" | "denied" | "revoked";
export type UserRoleType = {
  id: string;
  email: string;
  fullName: string;
  role: string;
  territory: string;
  doneByEmail: string;
  doneByName: string;
  lastModifiedDate: number;
  status: StatusType;
  eventType: string;
  group?: string;
  division?: string;
};
 
export const initSortUserData = (userData: UserRoleType[]) => {
  if (!userData.length) return [];
  // separate pending / other
  const pendingRoles = userData.filter((x: UserRoleType) => x.status === "pending");
  const remainingRoles = userData.filter((x: UserRoleType) => x.status !== "pending");
 
  const compare = (a: UserRoleType, b: UserRoleType) => {
    const nameA = a.fullName.toLocaleLowerCase();
    const nameB = b.fullName.toLocaleLowerCase();
 
    if (nameA < nameB) return -1;
    if (nameA > nameB) return 1;
    return 0;
  };
  const sorted = pendingRoles.sort(compare).concat(remainingRoles.sort(compare));
 
  return sorted;
};
 
export const sortUserData = (
  sortByKey: keyof UserRoleType,
  direction: boolean,
  data: UserRoleType[],
) => {
  if (!data.length) return [];
 
  // when direction is true, that means we are descending
  const [last, first] = direction ? [-1, 1] : [1, -1];
  const sortStatus = (a: UserRoleType, b: UserRoleType) => {
    switch (a.status) {
      case "pending":
        return first;
      case "active":
        return b.status === "pending" ? last : first;
      case "denied":
        return b.status === "revoked" ? first : last;
      case "revoked":
        return last;
      default:
        return last;
    }
  };
  const mainSort = (a: UserRoleType, b: UserRoleType) =>
    a[sortByKey] < b[sortByKey] ? first : last;
 
  if (sortByKey === "status") return data.sort((a, b) => sortStatus(a, b));
 
  return data.sort((a, b) => mainSort(a, b));
};