All files / src/components/ManageMoney/TransactionTable/TableActions/hooks useTransferActions.tsx

45.83% Statements 11/24
25% Branches 2/8
9.09% Functions 1/11
45.83% Lines 11/24

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                                    3x         11x 11x 11x   11x                             11x                     11x                                                 11x                                                 11x       11x       11x              
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { showMessage } from "@common/Toast";
import {
  QKEY_GET_TRANSFER_STATS,
  QKEY_LIST_MERCHANT_STATS,
  QKEY_PROVIDER_PROCESSING,
} from "@constants/queryKeys";
import { useGetCurrentMerchantId } from "@hooks/common";
import { customInstance } from "@services/api";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import { useMutation, useQueryClient } from "react-query";
 
type Props = {
  transactionId: string;
  customerID: number;
  transactionParentId?: string;
};
 
export const useTransferActions = ({
  customerID,
  transactionId,
  transactionParentId,
}: Props) => {
  const queryClient = useQueryClient();
  const { onClose } = useNiceModal();
  const { merchantId } = useGetCurrentMerchantId();
 
  const transactionQueries = [
    "get-transactions-risk-profile",
    "get-risk-transactions-list",
    "has-more-transactions",
    "acquirer-processing-transactions",
    ["get-transaction-history", transactionId],
    "get-all-transaction",
    QKEY_PROVIDER_PROCESSING,
    "get-transaction-stats",
    "manage-money-transactions",
    "get-transfers-list",
    [QKEY_LIST_MERCHANT_STATS, merchantId],
    QKEY_GET_TRANSFER_STATS,
  ];
 
  const refetchQueries = () => {
    transactionQueries.forEach((query) => {
      queryClient.invalidateQueries(query);
    });
    queryClient.refetchQueries([
      "get-transaction-status-history",
      transactionId,
      customerID,
    ]);
  };
 
  const cancelTransferMutation = useMutation(
    ({ reason }: any) => {
      return customInstance({
        url: buildMerchantEndpoints(
          `transfers/${transactionId}`,
          transactionParentId ?? customerID,
        ),
        method: "PATCH",
        data: { cancel: true, canceledReason: reason },
      });
    },
    {
      onSuccess: () => {
        refetchQueries();
        onClose();
      },
      onError: (error: any) => {
        showMessage(
          "Error",
          error?.message || "Whoops.. an error occurred. Please try again",
        );
      },
    },
  );
 
  const expediteTransferMutation = useMutation(
    () => {
      return customInstance({
        url: buildMerchantEndpoints(
          `transactions/${transactionId}/expedite`,
          customerID,
        ),
        method: "PATCH",
        data: {},
      });
    },
    {
      onSuccess: () => {
        refetchQueries();
        onClose();
      },
      onError: (error: any) => {
        showMessage(
          "Error",
          error?.message || "Whoops.. an error occurred. Please try again",
        );
      },
    },
  );
 
  const handleCancelTransfer = (reason: string) => {
    cancelTransferMutation.mutate({ reason });
  };
 
  const handleExpediteTransfer = () => {
    expediteTransferMutation.mutate();
  };
 
  return {
    handleCancelTransfer,
    handleExpediteTransfer,
    isLoading:
      cancelTransferMutation.isLoading || expediteTransferMutation.isLoading,
  };
};