All files / src/components/Merchants/MerchantPreview/modals GivePEPCheckModal.tsx

80.48% Statements 33/41
46.66% Branches 14/30
80% Functions 12/15
86.11% Lines 31/36

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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                                      2x                 62x   62x 23x       62x 23x     23x         62x                 62x           62x     62x 1x   62x 41x 41x   41x                             62x                           62x 62x   62x               62x   61x     8x               62x 62x   62x                                                                               2x             2x       23x       2x 41x      
import { useMemo, useState } from "react";
import NiceModal from "@ebay/nice-modal-react";
import GiveCheckModalBase from "./shared/GiveCheckModalBase";
import GivePEPNavigation from "./shared/GivePEPNavigation";
import GiveLatestCheckSection from "./shared/GiveLatestCheckSection";
import GivePreviousChecksSection from "./shared/GivePreviousChecksSection";
import { usePoliticallyExposed } from "@components/Merchants/MerchantPreview/modals/hooks/usePoliticallyExposed";
import { TPEPOwner } from "@components/Merchants/MerchantPreview/PEP/types";
import { formatTimestampWithTimezone } from "@utils/index";
import {
  LastCheckArrayProps,
  PepCheck,
} from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/types";
import { GIVE_OFAC_DETAILS_MODAL } from "modals/modal_names";
import { isUS, renderTitleWithNameAndTags } from "./shared/residencyUtils";
import { buildCheckButtons } from "./shared/buildCheckButtons";
 
interface GivePEPCheckModalProps {
  selectedOwner: TPEPOwner;
  owners?: TPEPOwner[];
  merchantId: number;
  legalEntityID: number;
  isEnterprise?: boolean;
  isCheckAllowed?: boolean;
  onCloseParentModal?: () => void;
}
 
const GivePEPCheckModal = NiceModal.create(
  ({
    selectedOwner,
    owners: ownersProp,
    merchantId,
    legalEntityID,
    isEnterprise = false,
    isCheckAllowed = true,
  }: GivePEPCheckModalProps) => {
    Iif (!selectedOwner) return null;
 
    const owners = useMemo(
      () => normalizeOwners(ownersProp, selectedOwner),
      [ownersProp, selectedOwner],
    );
 
    const [currentOwnerIndex, setCurrentOwnerIndex] = useState(() =>
      Math.max(
        0,
        owners.findIndex(
          (o): o is TPEPOwner => !!o && o.id === selectedOwner.id,
        ),
      ),
    );
 
    const currentOwner = owners[currentOwnerIndex];
 
    const {
      data = [],
      isLoading,
      checkPEP,
      isRunningCheck,
      latestStatus,
      latestCheckDate,
    } = usePoliticallyExposed({
      selectedOwner: currentOwner,
      merchantId,
      legalEntityID,
    });
 
    const handlePrevOwner = () =>
      setCurrentOwnerIndex((i) => Math.max(0, i - 1));
 
    const handleNextOwner = () =>
      setCurrentOwnerIndex((i) => Math.min(owners.length - 1, i + 1));
 
    const latestCheckInfo = useMemo<LastCheckArrayProps[]>(() => {
      const isResident = isUS(currentOwner?.countryOfResidence);
      const isCitizen = isUS(currentOwner?.citizenship);
 
      return [
        {
          title: renderTitleWithNameAndTags(
            resolveOwnerName(currentOwner),
            isResident,
            isCitizen,
          ),
          subTitle: "Latest Check",
          date: latestCheckDate
            ? formatTimestampWithTimezone(latestCheckDate)
            : "No check yet",
        },
      ];
    }, [currentOwner, latestCheckDate]);
 
    const handleViewDetails = () => {
      const checkId = data[0]?.checkID;
      if (!checkId) return;
 
      NiceModal.show(GIVE_OFAC_DETAILS_MODAL, {
        merchantId,
        legalEntityId: legalEntityID,
        currentCheckId: checkId,
        selectedOwner: currentOwner,
        isPEP: true,
        initialStatus: latestStatus,
      });
    };
 
    const buttons = useMemo(() => {
      const isClear = CLEAR_STATUSES.has(latestStatus ?? "");
 
      return buildCheckButtons({
        onCheck: checkPEP,
        onViewDetails: handleViewDetails,
        showCheckNow: isClear,
        showViewDetails: !isClear,
      });
    }, [latestStatus, checkPEP]);
 
    const previousChecks = useMemo(
      () =>
        (data as PepCheck[])
          .slice(1)
          .filter(Boolean)
          .map((check: PepCheck) => ({
            id: (check.checkID || check.id) ?? 0,
            statusName: (check.manualPepStatus || check.status) ?? "",
            createdAt: check.date ?? 0,
          })),
      [data],
    );
 
    const isEmpty = data.length === 0;
    const isPreviousEmpty = data.length <= 1;
 
    return (
      <GiveCheckModalBase
        title="Politically Exposed Person"
        testid="pep-panel-container"
      >
        <GivePEPNavigation
          pepOwnersCount={owners.length}
          currentIndex={currentOwnerIndex}
          onPrevious={handlePrevOwner}
          onNext={handleNextOwner}
        />
 
        <GiveLatestCheckSection
          statusName={latestStatus}
          buttons={buttons}
          latestCheckInfo={latestCheckInfo}
          merchantId={merchantId}
          legalEntityId={legalEntityID}
          currentCheckId={data[0]?.checkID}
          isCheckAllowed={isCheckAllowed}
          isEnterprisePanel={isEnterprise}
          isLoading={isLoading}
          isEmpty={isEmpty}
          isCheckingNow={isRunningCheck}
          isPEP
          selectedOwner={currentOwner}
        />
 
        <GivePreviousChecksSection
          previousChecks={previousChecks}
          isLoading={isLoading}
          isEmpty={isPreviousEmpty}
        />
      </GiveCheckModalBase>
    );
  },
);
 
export default GivePEPCheckModal;
 
const CLEAR_STATUSES = new Set([
  "clear",
  "manually_cleared",
  "clear_manually",
  "",
]);
 
const normalizeOwners = (
  owners?: TPEPOwner[],
  selectedOwner?: TPEPOwner,
): TPEPOwner[] => {
  Eif (Array.isArray(owners) && owners.length > 0) return owners;
  return selectedOwner ? [selectedOwner] : [];
};
 
const resolveOwnerName = (owner?: TPEPOwner) =>
  owner?.name ||
  [owner?.firstName, owner?.lastName].filter(Boolean).join(" ") ||
  "Business Owner";