All files / src/features/Merchants/MerchantSidePanel/ApprovalFlowPanel/hooks useUpdateTaskStatus.tsx

25% Statements 8/32
3.22% Branches 1/31
14.28% Functions 1/7
25.8% Lines 8/31

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 105 106 107 108 109 110 111 112 113 114 115                                                  29x                                   29x         2791x 2791x 2791x   2791x 2791x   2791x                                                                                                                      
import { customInstance } from "@services/api";
import { TTaskStatus, TUnderwritingTask } from "../types";
import { useMutation, useQueryClient } from "react-query";
import { UNDERWRITING_CHALLENGE_API_KEYS } from "../../constants";
import { showMessage } from "@common/Toast";
import { UNDERWRITING_TASK_MODAL } from "modals/modal_names";
import { useModal } from "@ebay/nice-modal-react";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_ENTERPRISE_STATS,
} from "@constants/queryKeys";
import { useAppSelector } from "@redux/hooks";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { checkPortals } from "@utils/routing";
 
type TUpdateTaskProps = {
  merchantID: number;
  taskID?: number;
  data: {
    reason?: string;
    status?: TTaskStatus;
    requestAttention?: boolean;
  };
};
 
const useUpdateTaskStatus = async ({
  merchantID,
  taskID,
  data,
}: TUpdateTaskProps) => {
  return await customInstance({
    url: `/merchants/${merchantID}/tasks/${taskID}`,
    method: "PATCH",
    data,
  });
};
 
type Props = {
  isCloseOnSuccess?: boolean;
  customErrorHandling?: boolean;
  onCompletedSuccess?: () => void;
};
 
export const useUpdateUnderwritingTask = ({
  isCloseOnSuccess = true,
  customErrorHandling = false,
  onCompletedSuccess,
}: Props) => {
  const queryClient = useQueryClient();
  const { isEnterpriseTable } = checkPortals();
  const ModalName = UNDERWRITING_TASK_MODAL;
 
  const { remove: removeChallengeModal } = useModal(ModalName);
  const tab = useAppSelector(selectMerchantTab);
 
  return useMutation({
    mutationFn: useUpdateTaskStatus,
    mutationKey: ["update-challenge-status"],
    onSuccess: (res: TUnderwritingTask, variables: TUpdateTaskProps) => {
      if (isCloseOnSuccess && res.status !== "in_progress")
        removeChallengeModal();
      queryClient.invalidateQueries(UNDERWRITING_CHALLENGE_API_KEYS.GET_TASKS);
      queryClient.invalidateQueries(UNDERWRITING_CHALLENGE_API_KEYS.GET_STATS);
      queryClient.invalidateQueries(["item"]);
      if (variables.data.status) {
        queryClient
          .getQueryCache()
          .getAll()
          .forEach((query) => {
            const queryKey = query.queryKey;
            if (
              Array.isArray(queryKey) &&
              queryKey[0] === UNDERWRITING_CHALLENGE_API_KEYS.GET_TASKS[0] &&
              queryKey[1] === UNDERWRITING_CHALLENGE_API_KEYS.GET_TASKS[1]
            ) {
              queryClient.setQueryData(queryKey, (oldData: any) => {
                if (!oldData || !Array.isArray(oldData.data)) return oldData;
 
                return {
                  ...oldData,
                  data: oldData.data.map((task: TUnderwritingTask) => {
                    const isSameTask =
                      (task.taskID !== 0 && task.taskID === res.taskID) ||
                      //for EDD task the taskID is always 0, so we need to check the EDD task name
                      (task.type === "enhanced_due_diligence" &&
                        task.merchantTaskName === res.merchantTaskName);
                    if (isSameTask) {
                      return {
                        ...task,
                        status: res.status,
                      };
                    }
                    return task;
                  }),
                };
              });
            }
          });
 
        if (!isEnterpriseTable) {
          queryClient.invalidateQueries([QKEY_LIST_ACQUIRER_MERCHANTS, tab]);
        } else {
          queryClient.invalidateQueries([QKEY_LIST_ENTERPRISE_STATS, { tab }]);
        }
      }
 
      if (res?.status === "completed" && onCompletedSuccess)
        onCompletedSuccess();
    },
    onError: (err: any) => {
      !customErrorHandling && showMessage("Error", err.message);
    },
  });
};