All files / src/features/TransactionPanel/hooks usePrechargebackRefund.ts

45% Statements 9/20
11.11% Branches 2/18
75% Functions 3/4
47.36% Lines 9/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 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                                            1x                     1x                     1x             31x 31x 31x   31x   1x                                                                               31x    
import { useMutation, useQueryClient } from "react-query";
import { TRDRTransaction } from "../types";
import { customInstance } from "@services/api";
import { processTransactionData } from "@components/ManageMoney/TransactionTable/TransactionInfoModal/utils";
import NiceModal from "@ebay/nice-modal-react";
import { TRANSACTION_INFO_MODAL } from "modals/modal_names";
import {
  QKEY_GET_TRANSACTION_STATS,
  QKEY_PROVIDER_PROCESSING,
} from "@constants/queryKeys";
import { checkPortals } from "@utils/routing";
import { TransactionType } from "../utils";
import { useGetCurrentMerchantId } from "@hooks/common";
 
type Props = {
  onSuccess: () => void;
  onError?: () => void;
  isFirst?: boolean;
  isLast?: boolean;
  setSelectedRow?: (newIdx: string | number) => void;
};
 
const preChargebackRefundTransaction = ({
  merchantId,
  id,
  reason,
  notifyCustomer,
}: {
  merchantId: number;
  id: string;
  reason: string;
  notifyCustomer: boolean;
}) => {
  return customInstance({
    url: `/merchants/${merchantId}/pre-chargeback-refunds`,
    method: "POST",
    data: {
      transactionID: id,
      reason: reason ? reason : "",
      notifyCustomer,
    },
  });
};
 
export const usePrechargebackRefund = ({
  onSuccess,
  onError,
  isFirst,
  isLast,
  setSelectedRow,
}: Props) => {
  const queryClient = useQueryClient();
  const { isAcquirerPortal, isEnterprisePortal } = checkPortals();
  const { merchantId } = useGetCurrentMerchantId();
 
  const handlePreChargebackRefundMutation = useMutation(
    (data: TRDRTransaction) => {
      return preChargebackRefundTransaction({
        merchantId: data.merchantId,
        id: data.id,
        reason: data.reason || "",
        notifyCustomer: data.notifyCustomer,
      });
    },
    {
      onSuccess: (transactionData) => {
        const parsedData = transactionData
          ? processTransactionData(transactionData)
          : null;
 
        if (
          parsedData?.transactionType == TransactionType.PRE_CHARGEBACK_REFUND
        ) {
          NiceModal.show(TRANSACTION_INFO_MODAL, {
            data: { ...parsedData, transactionID: parsedData?.id },
            isFirst: setSelectedRow ? isFirst : true,
            isLast: setSelectedRow ? isLast : true,
            setSelectedRow,
          });
        }
 
        if (isAcquirerPortal)
          queryClient.invalidateQueries("acquirer-processing-transactions");
        if (isEnterprisePortal)
          queryClient.invalidateQueries(QKEY_PROVIDER_PROCESSING);
 
        queryClient.invalidateQueries([
          "get-transaction-history",
          transactionData?.id,
        ]);
        queryClient.invalidateQueries([QKEY_GET_TRANSACTION_STATS, merchantId]);
        if (onSuccess) onSuccess();
      },
      onError,
    },
  );
 
  return { handlePreChargebackRefundMutation };
};