All files / src/components/Merchants/MerchantPreview/modals/shared GiveOFACDetails.tsx

82.05% Statements 32/39
86.95% Branches 60/69
70% Functions 7/10
82.05% Lines 32/39

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381                                                                                                                                1x                 9x                   9x     27x   11x                             1x               13x 5x     8x   8x                                                                     1x                         2x 1x     1x                                                   1x                                                                           1x   20x 20x   20x               20x                   20x                                                               20x               20x             20x 20x 3x 17x 3x 14x       20x     20x                                     20x                                                                                            
import { ArrowLeftIcon, InfoIcon } from "@phosphor-icons/react";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { Stack, Box } from "@mui/material";
import { isEmpty } from "lodash";
 
import GiveText from "@shared/Text/GiveText";
import GiveButton from "@shared/Button/GiveButton";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import GiveChip from "@shared/Chip/GiveChip";
import GiveAlert from "@shared/GiveAlert/GiveAlert";
 
import GiveOFACMatchList from "./GiveOFACMatchList";
import GiveChangeOFACStatus from "./GiveChangeOFACStatus";
import GiveOFACMatchListSkeleton from "./skeletons/GiveOFACMatchListSkeleton";
import GiveOFACEmptyState from "./emptyStates/GiveOFACEmptyState";
import GiveDetailsMatchList from "./GiveDetailsMatchList";
import GiveCheckModalBase from "./GiveCheckModalBase";
 
import { OFACTabType } from "@components/Merchants/MerchantPreview/OFAC/hooks/types";
import { TOFACStatusImproved } from "./types";
import { useOFACDetails } from "@components/Merchants/MerchantPreview/OFAC/hooks/useOFACDetails";
import useGetPEPDetails from "@components/Merchants/MerchantPreview/modals/hooks/useGetPEPDetails";
import { TPEPOwner } from "@components/Merchants/MerchantPreview/PEP/types";
 
import { useAppTheme } from "@theme/v2/Provider";
import { getOfacPepStatus } from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/Accordion.atom";
import { checkoutScrollbarStyles } from "@sections/PayBuilder/Checkout/helpers";
import { formatTimestampWithTimezone } from "@utils/index";
import { mapPepStatusToOfacStatus } from "../helpers";
 
/* -------------------------------------------------------------------------- */
/*                                   TYPES                                    */
/* -------------------------------------------------------------------------- */
 
type GiveOFACDetailsProps = {
  merchantId: number;
  legalEntityId: number;
  currentCheckId: number;
  composed?: string;
  isEditAllowed?: boolean;
  activeTab?: OFACTabType;
  tabCategory?: string;
  onUpdateSuccess?: (data: unknown) => void;
  isEnterprisePanel?: boolean;
  hideBackdrop?: boolean;
  selectedOwner?: TPEPOwner;
  isPEP?: boolean;
  initialStatus?: string;
};
 
type BaseCheckState = {
  currentCheck: any;
  isLoading: boolean;
  ofacStatus: TOFACStatusImproved;
  initialCheckDate: string;
  isNRA: boolean;
  isNotUSResident: boolean;
  handleSubmit: (data?: any) => void;
};
 
/* -------------------------------------------------------------------------- */
/*                               UI COMPONENTS                                */
/* -------------------------------------------------------------------------- */
 
const OFACAlerts = ({
  isNRA,
  isNotUSResident,
  initialCheckDate,
}: {
  isNRA: boolean;
  isNotUSResident: boolean;
  initialCheckDate: string;
}) => {
  const alerts = [
    { label: "Non-citizen of United States", hidden: !isNRA },
    { label: "Non-resident of United States", hidden: !isNotUSResident },
    {
      label: `Initial check ran on ${initialCheckDate}`,
      dataTestId: "ofac-initial-check-date",
      hidden: false,
    },
  ];
 
  return (
    <>
      {alerts
        .filter((alert) => !alert.hidden)
        .map((alert) => (
          <GiveAlert
            key={alert.label}
            type="blue"
            variant="notice"
            Icon={<InfoIcon size={24} />}
            label={alert.label}
            dataTestId={alert.dataTestId}
            sx={{ alignItems: "center" }}
            iconContainerSx={{ width: 24, height: 24, minWidth: 24 }}
          />
        ))}
    </>
  );
};
 
const GiveOFACDetailsContent = ({
  isLoading,
  currentCheck,
  ofacStatus,
  initialCheckDate,
  isNRA,
  isNotUSResident,
}: Omit<BaseCheckState, "handleSubmit">) => {
  if (isLoading || !currentCheck) {
    return <GiveOFACMatchListSkeleton />;
  }
 
  const hasMatches = ofacStatus !== "Clear" && !isEmpty(currentCheck.matchList);
 
  return (
    <Stack
      gap={2}
      height="100%"
      overflow="hidden"
      flex={1}
      minHeight={0}
      pb={2}
    >
      <Box
        sx={{
          flex: 1,
          overflowY: "auto",
          minHeight: 0,
          display: "flex",
          flexDirection: "column",
          gap: 2,
          ...checkoutScrollbarStyles,
        }}
      >
        <OFACAlerts
          isNRA={isNRA}
          isNotUSResident={isNotUSResident}
          initialCheckDate={initialCheckDate}
        />
        {hasMatches ? (
          <GiveOFACMatchList matches={currentCheck.matchList} />
        ) : (
          <GiveOFACEmptyState />
        )}
      </Box>
    </Stack>
  );
};
 
const GivePEPDetailsContent = ({
  isLoading,
  currentCheck,
  isNRA,
  isNotUSResident,
  initialCheckDate,
}: {
  isLoading: boolean;
  currentCheck: any;
  isNRA: boolean;
  isNotUSResident: boolean;
  initialCheckDate: string;
}) => {
  if (isLoading || !currentCheck) {
    return <GiveOFACMatchListSkeleton />;
  }
 
  Iif (isEmpty(currentCheck?.properties)) {
    return (
      <Stack gap={2} height="100%" overflow="hidden" flex={1} minHeight={0}>
        <Box
          sx={{
            flex: 1,
            overflowY: "auto",
            minHeight: 0,
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            justifyContent: "center",
            ...checkoutScrollbarStyles,
          }}
        >
          <OFACAlerts
            isNRA={isNRA}
            isNotUSResident={isNotUSResident}
            initialCheckDate={initialCheckDate}
          />
          <GiveOFACEmptyState />
        </Box>
      </Stack>
    );
  }
 
  return (
    <Stack
      gap={2}
      height="100%"
      overflow="hidden"
      flex={1}
      minHeight={0}
      pb={2}
    >
      <Box
        sx={{
          flex: 1,
          overflowY: "auto",
          minHeight: 0,
          display: "flex",
          flexDirection: "column",
          ...checkoutScrollbarStyles,
        }}
      >
        <OFACAlerts
          isNRA={isNRA}
          isNotUSResident={isNotUSResident}
          initialCheckDate={initialCheckDate}
        />
        <GiveDetailsMatchList
          items={currentCheck.properties}
          testIdPrefix="pep-details"
          useLabelAsTitle={false}
        />
      </Box>
    </Stack>
  );
};
 
/* -------------------------------------------------------------------------- */
/*                                   MODAL                                    */
/* -------------------------------------------------------------------------- */
 
const GiveOFACDetails = NiceModal.create(
  ({ hideBackdrop, isPEP, initialStatus, ...props }: GiveOFACDetailsProps) => {
    const modal = useModal();
    const { palette } = useAppTheme();
 
    const pepDetails = useGetPEPDetails({
      checkId: props.currentCheckId,
      merchantId: props.merchantId,
      selectedOwner: props.selectedOwner!,
      legalEntityID: props.legalEntityId,
      enabled: !!isPEP,
    });
 
    const ofacDetails = useOFACDetails({
      ...props,
      currentCheckId: isPEP ? 0 : props.currentCheckId,
      composed: props.composed ?? "",
      isEditAllowed: props.isEditAllowed ?? false,
      activeTab: props.activeTab ?? OFACTabType.BUSINESS_PROFILE,
      isEnterprisePanel: props.isEnterprisePanel ?? false,
      enabled: !isPEP,
    });
 
    const state: BaseCheckState = isPEP
      ? {
          currentCheck: pepDetails.data
            ? { ...pepDetails.data, ID: props.currentCheckId }
            : null,
          isLoading: pepDetails.isLoading,
          ofacStatus:
            (pepDetails.data
              ? mapPepStatusToOfacStatus(
                  pepDetails.data?.manualPEPStatus === "clear"
                    ? "manually_cleared"
                    : pepDetails.data?.pepStatus,
                )
              : undefined) || mapPepStatusToOfacStatus(initialStatus),
          initialCheckDate: pepDetails.data?.createdAt
            ? formatTimestampWithTimezone(pepDetails.data.createdAt)
            : "",
          isNRA: false,
          isNotUSResident: false,
          handleSubmit: (payload: any) => {
            const status = payload?.data?.status;
            if (status) {
              // PEP API only accepts 'clear' or 'confirmed_match'
              const pepStatus =
                status === "manually_cleared" ? "clear" : status;
              pepDetails.handleUpdateStatus(pepStatus);
            }
          },
        }
      : { ...ofacDetails, ofacStatus: ofacDetails.ofacStatus || initialStatus };
 
    const statusConfig =
      getOfacPepStatus(palette)[
        state.ofacStatus?.toLowerCase().replace(" ", "_") as keyof ReturnType<
          typeof getOfacPepStatus
        >
      ];
 
    // Determine the business owner name to display
    const businessOwnerName =
      props.selectedOwner?.name ||
      (props.selectedOwner?.firstName && props.selectedOwner?.lastName
        ? `${props.selectedOwner.firstName} ${props.selectedOwner.lastName}`
        : "") ||
      state.currentCheck?.resourceFullName ||
      "Business Owner";
 
    const statusChangeEntityId = (() => {
      if (isPEP)
        return (props.selectedOwner?.id as number) ?? props.legalEntityId;
      if (props.activeTab === OFACTabType.BUSINESS_PROFILE)
        return props.legalEntityId;
      return undefined;
    })();
 
    const statusChangeActiveTab =
      props.activeTab ?? (isPEP ? OFACTabType.BUSINESS_OWNER : undefined);
 
    const title = (
      <Stack direction="row" gap={1.5} alignItems="center">
        <GiveIconButton
          Icon={ArrowLeftIcon}
          variant="ghost"
          size="small"
          onClick={() => modal.hide()}
        />
        <GiveText variant="bodyM" data-testid="ofac-details-owner-name">
          {businessOwnerName}
        </GiveText>
        <GiveChip
          variant="light"
          size="small"
          color={statusConfig?.color ?? "default"}
          label={statusConfig?.label ?? "Pending"}
        />
      </Stack>
    );
 
    return (
      <GiveCheckModalBase
        title={title as any}
        testid="ofac-details-modal-container"
        hideBackdrop={hideBackdrop}
        buttons={
          <Stack
            direction="row"
            gap={1.5}
            alignItems="center"
            width="100%"
            justifyContent="flex-end"
          >
            <GiveButton
              label="Close"
              variant="ghost"
              size="large"
              onClick={() => modal.hide()}
            />
 
            {(state.currentCheck || isPEP) && (
              <GiveChangeOFACStatus
                ofacStatus={state.ofacStatus}
                merchantId={props.merchantId}
                legalEntityId={statusChangeEntityId!}
                activeTab={statusChangeActiveTab!}
                currentCheck={state.currentCheck}
                handleSubmit={state.handleSubmit}
                isEnterprisePanel={props.isEnterprisePanel!}
                isPEP={isPEP}
              />
            )}
          </Stack>
        }
      >
        {isPEP ? (
          <GivePEPDetailsContent {...state} />
        ) : (
          <GiveOFACDetailsContent {...state} />
        )}
      </GiveCheckModalBase>
    );
  },
);
 
export default GiveOFACDetails;