All files / src/features/Minibuilders/Conversations/hooks useUpdateChallenge.tsx

10.71% Statements 3/28
0% Branches 0/14
0% Functions 0/9
10.71% Lines 3/28

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 116 117 118 119 120                                            21x           21x                           21x                                                                                                                                                          
import { showMessage } from "@common/Toast";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_MERCHANTS,
} from "@constants/queryKeys";
import {
  MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS,
  PROVIDER_PANEL_KEYS,
} from "@features/Merchants/MerchantSidePanel/constants";
import { customInstance } from "@services/api";
import { useEffect } from "react";
import { useMutation, useQueryClient } from "react-query";
 
type TStatuses = "open" | "ready_for_verification" | "closed";
 
type TUpdateChallenge = {
  merchantId: number;
  challengeId: number;
  data: { status: TStatuses } | { message?: string };
  isNotificationAction?: boolean;
};
 
const buildUrlAndMethod = (isNotificationAction: boolean) => {
  const action = isNotificationAction ? "notify" : "status";
  const method = isNotificationAction ? "POST" : "PATCH";
  return { action, method };
};
 
const updateChallengeFn = async ({
  merchantId,
  challengeId,
  data,
  isNotificationAction,
}: TUpdateChallenge) => {
  const { action, method } = buildUrlAndMethod(isNotificationAction || false);
  return await customInstance({
    url: `/merchants/${merchantId}/underwriting-challenges/${challengeId}/${action}`,
    method,
    data,
  });
};
 
export const useUpdateChallenge = ({
  challengeId,
}: {
  challengeId: number;
}) => {
  const queryClient = useQueryClient();
  const updateChallenge = useMutation({
    mutationFn: updateChallengeFn,
    mutationKey: ["update-challenge"],
  });
 
  const handleUpdateChallengeStatus = ({
    merchantId,
    data,
    onClose,
  }: {
    merchantId: number;
    data: { status: TStatuses; type?: string };
    onClose?: () => void;
  }) => {
    return updateChallenge.mutate(
      { challengeId, data, merchantId },
      {
        onSuccess: async () => {
          const keysToInvalidate = [
            [PROVIDER_PANEL_KEYS.CHALLENGE_STATS, merchantId],
            [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET, merchantId],
            [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY, merchantId],
            [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_BANK_ACCOUNTS, merchantId],
            [PROVIDER_PANEL_KEYS.GET, merchantId],
          ];
          keysToInvalidate.forEach((key) => {
            queryClient.invalidateQueries(key);
          });
          if (data.status === "closed") {
            showMessage("Success", "Challenge completed", false);
          }
          onClose && onClose();
          queryClient.invalidateQueries(QKEY_LIST_MERCHANTS);
          queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
        },
        onSettled: () => {
          if (data.status === "open") {
            queryClient.invalidateQueries([
              PROVIDER_PANEL_KEYS.GET_CHALLENGES,
              merchantId,
            ]);
          }
        },
      },
    );
  };
 
  useEffect(() => {
    if (updateChallenge.isSuccess) {
      queryClient.invalidateQueries(PROVIDER_PANEL_KEYS.GET_CHALLENGES);
    }
  }, [updateChallenge.isSuccess]);
 
  const handleNotifyMerchant = (
    data: { message?: string },
    merchantId: number,
  ) => {
    updateChallenge.mutate({
      isNotificationAction: true,
      challengeId,
      merchantId,
      data,
    });
  };
 
  return {
    handleUpdateChallengeStatus,
    handleNotifyMerchant,
    isLoading: updateChallenge.isLoading,
  };
};