All files / src/features/TransactionPanel/components/history TransactionStateHistory.tsx

100% Statements 42/42
94.36% Branches 67/71
100% Functions 1/1
100% Lines 42/42

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                            53x 128x 128x 128x 128x 128x 128x     128x         128x       128x     128x 126x   126x 126x               126x   126x   126x       126x   126x         126x             126x           126x       126x 126x       126x           126x 126x 126x 126x 126x 126x       126x 4x     122x           122x         126x       126x               126x         126x 49x 77x                                                         53x          
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import PanelLoadingSkeleton from "@features/Merchants/MerchantSidePanel/components/PanelLoadingSkeleton";
import { getRDRRefundLabel } from "@features/TransactionPanel/constants";
import { useTransactionHistory } from "@features/TransactionPanel/hooks/useTransactionHistory";
import { useGetCurrentMerchantId } from "@hooks/common";
import {
  TransactionStatus,
  TransactionType,
} from "@features/TransactionPanel/shared.types";
import { Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { styled } from "@theme/v2/Provider";
import { checkPortals } from "@utils/routing";
import { SummaryContainerBase } from "../atoms";
import { HistoryTimeline } from "./HistoryTimeline";
import { moveFundsArray } from "@components/ManageMoney/TransactionTable/transactions.types";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
type Props = {
  data: any;
  panelLoaded: boolean;
};
 
const TransactionStateHistory = ({ data, panelLoaded }: Props) => {
  const { accID, id } = data || {};
  const isChargeback = data?.transactionType === "chargeback";
  const isVoided = data?.processingState === "voided";
  const isCanceled = data?.processingState === "canceled";
  const isDeclined = data?.processingState === "declined";
  const isRequested = data?.processingState === "enqueued";
 
  const isPreChargebackRefund =
    data?.transactionType === TransactionType.PRE_CHARGEBACK_REFUND;
 
  // For chargebacks and pre-chargeback refunds, money is reversed TO the card (destinationMethod)
  // For other transactions, money comes FROM the card (sourceMethod)
  const cardBrandSource =
    isChargeback || isPreChargebackRefund
      ? data?.destinationMethod?.card?.brand
      : data?.sourceMethod?.card?.brand;
 
  const cardBrand = cardBrandSource ?? "visa";
 
  const { isEnterprisePortal, isAcquirerPortal, isMerchantPortal } =
    checkPortals();
  const { merchantId: loggedInMerchantId } = useGetCurrentMerchantId();
 
  const { risk_monitoring } = useEnterprisePermissions();
  const { isMinimumReserveBPSEnabled } = useGetFeatureFlagValues();
 
  // From Ayment instructions:
  /**
   * show email undeliverable when the email status is rejected and risk level is 2
   * show assessment for the voiding reason in case it is voided because of an escalation
   */
  const isEmailRejected =
    data?.customer?.emailStatus?.toLowerCase() === "rejected";
 
  const preChargebackRefundReasonLabel = getRDRRefundLabel(cardBrand);
 
  const normalReason = isPreChargebackRefund
    ? preChargebackRefundReasonLabel
    : data?.reversalReason;
  const checkForCanceledReason =
    isVoided || isCanceled ? data?.canceledReason : normalReason;
  const reasonIfNotMerchantPortal =
    (isEnterprisePortal && !risk_monitoring && isEmailRejected) ||
    (isMerchantPortal && isEmailRejected)
      ? ""
      : checkForCanceledReason;
  const reasonConditioned =
    isEnterprisePortal &&
    typeof data?.ipProfileSnapshot?.riskLevel === "number" &&
    isEmailRejected &&
    risk_monitoring
      ? "Email Undeliverable"
      : reasonIfNotMerchantPortal;
  const computedReason =
    isEmailRejected && isAcquirerPortal && !isPreChargebackRefund
      ? "Email Undeliverable"
      : reasonConditioned;
 
  // 1- how to know it is a void due to an an escalaltion => quarantine : false && risk level != "low" && assessment != ""
  const reason =
    !data?.isQuarantined && data?.riskLevel !== "low" && data?.escalationReason
      ? data?.escalationReason
      : computedReason;
 
  const isRefund = data?.transactionType === "refund";
  const isReserve = ["reserve_deposit", "reserve_release"].includes(
    data?.transactionType || "",
  );
  const isMinimumReserveTransfer =
    isReserve &&
    Boolean(data?.originalTransactionID) &&
    isMinimumReserveBPSEnabled;
 
  // isPreChargebackRefund is also a type of refund
  const isRefunded =
    isRefund || isPreChargebackRefund || isVoided || isCanceled;
  const isQuarantined = data?.isQuarantined;
  const isTransfer = data?.displayType === TransactionStatus.MONEY_TRANSFER;
  const isMoveFunds = data?.transactionType === "move_funds";
  const isAcquirerSource = data?.sourceAccount?.merchant?.type === "acquirer";
  const isAcquirerMoveFunds = isMoveFunds && isAcquirerSource;
 
  let merchantID: number | undefined;
  // a merchant can never access acquirere or provider history
  if (isMerchantPortal) {
    merchantID =loggedInMerchantId;
  } else {
    const useDestinationAccount =
      (!isChargeback &&
        !isRefunded &&
        !isTransfer &&
        !isPreChargebackRefund &&
        !isMoveFunds) ||
      (isVoided && !isPreChargebackRefund);
    merchantID = useDestinationAccount
      ? data?.destinationAccount?.id
      : data?.sourceAccount?.id;
  }
 
  const isTransferFunds = moveFundsArray.includes(data?.transactionType);
 
  // only in Chargeback Refund we show description
  // and reason for it is a default one, while description is the reason input by user
  const refundDisplayedData = {
    reason: reason,
    description:
      isPreChargebackRefund && data?.reversalReason
        ? `Reason: ${data.reversalReason}`
        : "",
  };
 
  const { data: historyList, isLoading } = useTransactionHistory(
    merchantID,
    accID ?? id,
    panelLoaded,
  );
  if (isLoading)
    return <PanelLoadingSkeleton variant={"transaction_history"} />;
  return (
    <Stack spacing="21px">
      <GiveText variant="bodyM">History</GiveText>
      <SummaryContainer>
        <HistoryTimeline
          historyList={historyList?.data}
          isQuarantined={isQuarantined}
          isRefunded={isRefunded}
          refundDisplayedData={refundDisplayedData}
          isVoidedOrCanceled={isVoided || isCanceled}
          isDeclined={isDeclined}
          isTransferFunds={isTransferFunds}
          isRefund={isRefund}
          mspStatusDetails={data?.mspStatusDetails ?? {}}
          holdingUntilAt={data?.holdingUntilAt}
          isRequested={isRequested}
          isPreChargebackRefund={isPreChargebackRefund}
          isMinimumReserveTransfer={isMinimumReserveTransfer}
          isReserve={isReserve}
          isManual={Boolean(data?.isManual)}
          authUser={data?.sourceAccount?.user}
          initiatingUser={data?.authUser}
          historyDescription={data?.description}
        />
      </SummaryContainer>
    </Stack>
  );
};
 
const SummaryContainer = styled(SummaryContainerBase)({
  padding: "20px",
});
 
export default TransactionStateHistory;