All files / react-app/src/api useGetUserProfile.ts

91.89% Statements 34/37
77.77% Branches 14/18
100% Functions 5/5
94.28% Lines 33/35

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                                                                      44x 44x 44x   40x 76x 43x     76x 76x         76x 556x 556x 76x   556x       40x 283x 283x 283x 283x             97x 48x 48x   44x 44x 44x   1x   44x       44x       4x     4x 4x       97x 119x   35x        
import { useQuery } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { StateCode } from "shared-types";
 
import { sendGAEvent } from "@/utils/ReactGA/SendGAEvent";
 
export type Approver = { email: string; fullName: string; territory: StateCode | "N/A" };
type ApproverRaw = {
  role: string;
  statusCode?: number;
  territory: (StateCode | "N/A")[];
  approvers: Approver[];
};
export type StateAccess = {
  id: string;
  eventType: string;
  email: string;
  doneByEmail: string;
  doneByName: string;
  status: string;
  role: string;
  territory: string;
  approverList?: Approver[];
  group?: string;
  division?: string;
};
 
export type OneMacUserProfile = {
  stateAccess?: StateAccess[];
};
 
export function attachApproversToStateAccess(
  stateAccess: StateAccess[],
  approverByRole: ApproverRaw[],
): StateAccess[] {
  const roleTerritoryApproverMap: { [key: string]: any } = {};
  Iif (!approverByRole) return stateAccess;
  if (!approverByRole.length) return stateAccess;
 
  for (const input of approverByRole) {
    if (!roleTerritoryApproverMap[input.role]) {
      roleTerritoryApproverMap[input.role] = {};
    }
 
    const mapByTerritory = roleTerritoryApproverMap[input.role];
    Iif (!input.approvers && input.statusCode) {
      console.log("Error - missing approvers", input);
      continue;
    }
 
    for (const approver of input.approvers) {
      const { territory, ...rest } = approver;
      if (!mapByTerritory[territory]) {
        mapByTerritory[territory] = [];
      }
      mapByTerritory[territory].push(rest);
    }
  }
 
  return stateAccess.map((entry) => {
    const territoryLookup = entry.role === "statesubmitter" ? entry.territory : "N/A";
    const roleMap = roleTerritoryApproverMap[entry.role];
    const approverList = roleMap?.[territoryLookup] || [];
    return {
      ...entry,
      approverList,
    };
  });
}
 
export const getUserProfile = async (userEmail?: string): Promise<OneMacUserProfile> => {
  try {
    const stateAccess = await API.post("os", "/getUserProfile", { body: { userEmail } });
 
    let approvers: any = { approverList: [] };
    try {
      approvers = await API.post("os", "/getApprovers", { body: { userEmail } });
    } catch (approverError) {
      console.log("Error fetching approvers:", approverError);
    }
    const stateAccessWithApprovers = attachApproversToStateAccess(
      stateAccess,
      approvers.approverList || [],
    );
    return {
      stateAccess: stateAccessWithApprovers,
    } as OneMacUserProfile;
  } catch (e) {
    sendGAEvent("api_error", {
      message: "failure /getUserDetails",
    });
    console.error("Error in getUserProfile:", e);
    return {};
  }
};
 
export const useGetUserProfile = () =>
  useQuery({
    queryKey: ["profile"],
    queryFn: () => getUserProfile(),
    refetchOnWindowFocus: false,
    refetchOnMount: false,
  });