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

66.66% Statements 16/24
33.33% Branches 3/9
83.33% Functions 5/6
72.72% Lines 16/22

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                                      97x 11x 11x     4x   7x 7x       97x 49x   49x         4x   4x       4x 4x 4x                   4x       2x              
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { StateCode } from "shared-types";
import { UserRole } from "shared-types/events/legacy-user";
 
import { StateAccess } from "./useGetUserProfile";
 
export type RoleStatus = "active" | "denied" | "pending" | "revoked";
export type RoleRequest = {
  email: string;
  state: StateCode | "N/A";
  role: UserRole;
  eventType: string;
  requestRoleChange: boolean; // is this a role change request? (used in state signup and profile page)
  grantAccess?: RoleStatus; // active, denied, revoked, or pending if undefined (used in user management page)
  group?: string; // used for systemadmins upgrading defaultcmsuser to cmsroleapprover
  division?: string; // used for systemadmins upgrading defaultcmsuser to cmsroleapprover
};
 
export const submitRoleRequests = async (request: RoleRequest): Promise<{ message: string }> => {
  try {
    const roleRequest = await API.post("os", "/submitRoleRequests", {
      body: request,
    });
    return roleRequest;
  } catch (error) {
    console.error(error);
    throw new Error("Failed to submit role request");
  }
};
 
export const useSubmitRoleRequests = () => {
  const queryClient = useQueryClient();
 
  return useMutation<{ message: string }, Error, RoleRequest, { previousRequests?: StateAccess[] }>(
    {
      mutationFn: submitRoleRequests,
 
      onMutate: async (newRequest) => {
        await queryClient.cancelQueries(["roleRequests"]);
        // Save current cache in case there's an error
        const previousRequests: StateAccess[] | undefined = queryClient.getQueryData<StateAccess[]>(
          ["roleRequests"],
        );
        // Updates existing cache if anything changed
        queryClient.setQueryData(["roleRequests"], (old: StateAccess[] = []) => {
          Iif (!Array.isArray(old)) return [];
          return old.map((oldRequest) => {
            if (oldRequest.id === `${newRequest.email}_${newRequest.state}_${newRequest.role}`) {
              let status = "pending";
              if (newRequest.grantAccess !== undefined) status = newRequest.grantAccess;
              return { ...oldRequest, lastModifiedDate: Date.now(), status: status, ...newRequest };
            }
            return oldRequest;
          });
        });
 
        return { previousRequests };
      },
 
      onError: (_error, _variables, context) => {
        Iif (context?.previousRequests) {
          queryClient.setQueryData(["roleRequests"], context.previousRequests);
        }
      },
    },
  );
};