All files / src/components/Disputes/GiveDisputePanel helpers.ts

88.88% Statements 16/18
72.22% Branches 13/18
100% Functions 5/5
88.88% Lines 16/18

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        1x                 55x   55x                 1x                               55x     55x                       55x   23x                             55x                           55x                       55x 78x 23x   55x 55x     55x    
import { getRDRRefundLabel } from "@features/TransactionPanel/constants";
import { BuildHistoryItemsParams, EvidenceItem, HistoryItem } from "./types";
import { DisputeTagType } from "../utils/data.types";
 
export const getRDRFlags = ({
  status,
  outcome,
  preChargebackRefundTransaction,
}: {
  status?: string;
  outcome?: string;
  preChargebackRefundTransaction?: unknown;
}) => {
  const isFromRDR = preChargebackRefundTransaction !== null;
 
  return {
    isFromRDR,
    isRDRAccepted: isFromRDR && status !== "prevented",
    isRDRSuccessfull: isFromRDR && status === "prevented",
    isCaseClosed: status === "closed",
    isRDRLost: isFromRDR && status === "closed" && outcome === "lost",
  };
};
 
export const buildDisputeHistoryItems = ({
  data,
  disputeCaseType,
  submittedEvidences,
  evidencesCount,
  isWorldpay,
  status,
  notes,
  useDescription,
  mspNotes,
  isFromRDR,
  isChargebackAccepted,
  isRDRSuccessfull,
  isCaseClosed,
  cardholder,
}: BuildHistoryItemsParams): HistoryItem[] => {
  const items: HistoryItem[] = [];
 
  // Merchant chargeback accepted
  Iif (isChargebackAccepted) {
    items.push({
      id: `merchant-accepted-${data?.id}`,
      kind: "merchantAction",
      createdAt: data?.createdAt,
      mspCaseType: disputeCaseType,
      notes,
      isFromRDR,
    });
  }
 
  // Evidences
  items.push(
    ...submittedEvidences.map(
      (evidence): EvidenceItem => ({
        id: evidence.id,
        kind: "evidence",
        createdAt: evidence.createdAt,
        evidence,
        mspCaseType: disputeCaseType,
        evidencesCount,
        isWorldpay,
        status: status as DisputeTagType,
        isFromRDR,
      }),
    ),
  );
 
  // Issuer previous log (RDR successful)
  Iif (isRDRSuccessfull) {
    items.push({
      id: `issuer-prev-${data?.id}`,
      kind: "issuer",
      createdAt: data?.createdAt,
      mspCaseType: disputeCaseType,
      notes: `${getRDRRefundLabel(cardholder?.cardBrand)} Accepted`,
      subNotes: "Pre-Chargeback Refund sent",
      isFromRDR,
      isDone: true,
    });
  }
 
  // Issuer main item
  items.push({
    id: `issuer-main-${data?.id}`,
    kind: "issuer",
    createdAt: data?.createdAt,
    mspCaseType: disputeCaseType,
    notes: useDescription,
    subNotes: isChargebackAccepted ? "" : (mspNotes as string),
    isFromRDR,
    isDone: isRDRSuccessfull || isCaseClosed || status === "under_review",
  });
 
  // Sort + mark last
  const sorted = items
    .filter((item) => item.createdAt != null)
    .sort((a, b) => b.createdAt - a.createdAt);
 
  Eif (sorted.length > 0) {
    sorted[sorted.length - 1].isLast = true;
  }
 
  return sorted;
};