All files / react-app/src/features/profile utils.ts

96.42% Statements 27/28
90% Branches 18/20
100% Functions 8/8
95% Lines 19/20

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            94x                   94x 36x   55x 55x   17x 76x 76x   76x 32x       17x   17x         94x 37x 16x 52x       94x 59x 160x    
import { UserRole } from "shared-types/events/legacy-user";
 
import { StateAccess } from "@/api";
import { convertStateAbbrToFullName } from "@/utils";
 
// TODO: rename? all roles should see either a State or Status Access Card
export const stateAccessRoles: UserRole[] = [
  "statesubmitter",
  "statesystemadmin",
  "cmsroleapprover",
  "helpdesk",
  "defaultcmsuser",
  "cmsreviewer",
  "norole",
];
 
export const orderStateAccess = (accesses: StateAccess[]) => {
  if (!accesses || !accesses.length) return;
  // sort revoked states seprately and add to
  const activeStates = accesses.filter((x: StateAccess) => x.status != "revoked");
  const revokedStates = accesses.filter((x: StateAccess) => x.status == "revoked");
 
  const compare = (a: StateAccess, b: StateAccess) => {
    const stateA = convertStateAbbrToFullName(a.territory);
    const stateB = convertStateAbbrToFullName(b.territory);
 
    if (stateA < stateB) return -1;
    Eif (stateA > stateB) return 1;
    return 0;
  };
 
  const sorted = activeStates.sort(compare).concat(revokedStates.sort(compare));
 
  return sorted;
};
 
// if user has no active roles, show pending state(s)
// show state(s) for latest active role
export const filterStateAccess = (userDetails, userProfile) => {
  if (!userProfile?.stateAccess || userProfile.stateAccess.length < 1) return [];
  return userDetails?.role && userDetails?.role !== "norole"
    ? userProfile.stateAccess.filter((access: StateAccess) => access.role === userDetails.role)
    : userProfile.stateAccess;
};
 
export const hasPendingRequests = (stateAccess) => {
  if (!stateAccess || stateAccess.length < 1) return false;
  return stateAccess.some((role) => role.status === "pending");
};