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

82.5% Statements 33/40
75% Branches 24/32
100% Functions 9/9
83.33% Lines 25/30

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              98x                         98x 38x   58x 58x   18x 80x 80x 80x   80x 34x               18x   18x         98x 39x 17x 55x       98x 66x 187x       98x       34x 2x               32x               32x            
import { StateCode } from "shared-types";
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",
];
 
// In the backend we named the prop "StateAcess", but this is used for both state and CMS users
// it is confusing me so on the frontend we will convert StateAcess -> RoleStatus as that is the more appropriate name
 
export const orderRoleStatus = (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) => {
    Eif (a.territory !== "N/A" && b.territory !== "N/A") {
      const stateA = convertStateAbbrToFullName(a.territory);
      const stateB = convertStateAbbrToFullName(b.territory);
 
      if (stateA < stateB) return -1;
      Eif (stateA > stateB) return 1;
      return 0;
    }
    if (a.role === "defaultcmsuser") return 1;
    if (b.role === "defaultcmsuser") 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 filterRoleStatus = (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");
};
 
// get which confirmation text to use
export const getConfirmationModalText = (
  selfRevokeState: StateCode | null,
  selfWithdrawPending: boolean,
) => {
  if (selfRevokeState) {
    return {
      dialogTitle: "Withdraw State Access?",
      dialogBody: `This action cannot be undone. ${convertStateAbbrToFullName(
        selfRevokeState,
      )} State System Admin will be notified.`,
      ariaLabelledBy: "Self Revoke Access Modal",
    };
  }
  Iif (selfWithdrawPending) {
    return {
      dialogTitle: "Withdraw Role Request?",
      dialogBody: "This role is still pending approval. Withdrawing it will cancel your request.",
      ariaLabelledBy: "Self Withdraw Pending Access Modal",
    };
  }
 
  return {
    dialogTitle: "",
    dialogBody: "",
    ariaLabelledBy: "",
  };
};