All files / src/features/Merchants/MerchantSidePanel/hooks useCloseAccount.tsx

42.1% Statements 8/19
0% Branches 0/4
25% Functions 1/4
42.1% Lines 8/19

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                                      1x 3x 3x 3x   3x                         3x                         3x                   3x    
import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_ENTERPRISE_STATS,
  QKEY_LIST_MERCHANT_STATS,
} from "@constants/queryKeys";
import { checkPortals } from "@utils/routing";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
type Props = {
  merchantId: number;
};
type TData = {
  reason: string;
  note: string;
};
 
export const useCloseAccount = ({ merchantId }: Props) => {
  const queryClient = useQueryClient();
  const { isAcquirerPortal } = checkPortals();
  const { handleRefetchCounters } = useRefetchCounters();
 
  const refreshMerchantData = () => {
    const listQueryKey = isAcquirerPortal
      ? QKEY_LIST_ACQUIRER_MERCHANTS
      : QKEY_LIST_ENTERPRISE_STATS;
    queryClient.invalidateQueries([
      MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
      merchantId,
    ]);
    queryClient.invalidateQueries(QKEY_LIST_MERCHANT_STATS);
    handleRefetchCounters();
    queryClient.invalidateQueries(listQueryKey);
  };
 
  const closeAccountMutation = useMutation(
    (data: any) => {
      return customInstance({
        url: `/merchants/${merchantId}/close`,
        method: "POST",
        data,
      });
    },
    {
      onSuccess: refreshMerchantData,
    },
  );
 
  const handleClose = async (data: TData): Promise<boolean> => {
    try {
      await closeAccountMutation.mutateAsync(data);
      return true;
    } catch (error: any) {
      showMessage("Error", error?.response?.data?.message || error?.message);
      return false;
    }
  };
 
  return { handleClose };
};