All files / src/features/Merchants/MerchantSidePanel/UnderwritingTasks/components CustomAccordion.tsx

86.11% Statements 31/36
61.81% Branches 34/55
80% Functions 4/5
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 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                                                                8x           8x   8x                                   73x 73x 73x     73x 4x     73x     13x       13x 13x                 13x                                                                                   73x   73x   73x 73x   73x     73x 73x   73x 4x                 73x 73x 73x                                 73x 73x                               73x   73x 7x                                         66x                                                                                                                                                      
import { Accordion, AccordionDetails, Box, Stack } from "@mui/material";
import { CaretDownIcon, CaretUpIcon } from "@phosphor-icons/react";
 
import GiveDivider from "@shared/GiveDivider/GiveDivider";
import { useAppTheme } from "@theme/v2/Provider";
import React, { useState } from "react";
import { formatTimestampWithTimezone } from "@utils/index";
import {
  ActionButtonsProps,
  CustomAccordionProps,
  ExtendedPEPStatusType,
  ofacDataTypes,
} from "../types";
import { useQuery } from "react-query";
import { customInstance } from "@services/api";
import { isEmpty } from "lodash";
import {
  AccordionDetailsHeader,
  AccordionHeader,
  AccordionListItems,
  getOfacPepStatus,
  HideOrShow,
  LastCheckArray,
  LoadingComponent,
} from "./Accordion.atom";
import { GET_HISTORY_LIST } from "../constants";
import GiveText from "@shared/Text/GiveText";
import { SUMMARY_TEXT } from "../../constants";
import GiveButton from "@shared/Button/GiveButton";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { THIRTY_DAYS_IN_SECONDS } from "@components/Merchants/MerchantPreview/constants";
 
const resourceMapToType = {
  PAH: "merchant_owners",
  BO: "legal_principals",
  BUSINESS_CHECK: "legal_entities",
};
 
const maxListToShow = 5;
 
const MANUAL_PEP_STATUS_MAP: Record<string, string> = {
  manually_cleared: "clear_manually",
};
 
function CustomAccordion({
  data,
  merchantID,
  PEP_OFAC,
  handleViewMore,
  isMATCH,
  isBO_OFAC,
  isPEP_BO,
  isPAH_OFAC,
  handleCheckNowFunctions,
  isLoadingCheckNow,
  businessProfileId,
  isOFAC_MERCHANT_CHECK,
}: CustomAccordionProps) {
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const [expanded, setExpanded] = useState(
    isMATCH || isPAH_OFAC || isOFAC_MERCHANT_CHECK,
  );
  const handleChange = (event: React.SyntheticEvent, isExpanded: boolean) => {
    setExpanded(isExpanded);
  };
 
  const { data: ofacData, isLoading } = useQuery(
    [GET_HISTORY_LIST, merchantID, data?.id, businessProfileId],
    async () => {
      const main = `/merchants/${merchantID}/ofac-checks?filter=resourceTypeName:"${
        resourceMapToType[PEP_OFAC as keyof typeof resourceMapToType]
      }"&sort=-createdAt`;
 
      const PEP_URL = `/merchants/${merchantID}/legal-entities/${businessProfileId}/principals/${data?.id}/pep-checks?sort=-createdAt`;
      const urlObjects = {
        MATCH: `/merchants/${
          merchantID || data?.id
        }/match-reports?sort=-createdAt`,
        PAH: main,
        BO: `/merchants/${merchantID}/ofac-checks?filter=%3B(resourceTypeName:"legal_principals")%3B(resourceID:${data?.id})&sort=-createdAt`,
        BUSINESS_CHECK: `/merchants/${merchantID}/ofac-checks?filter=resourceTypeName:"legal_entities"&sort=-createdAt`,
        PEP: PEP_URL, // to check
      };
      const ofac = await customInstance({
        url: urlObjects[PEP_OFAC || "BO"],
      });
 
      const arr: ofacDataTypes[] = ofac?.data?.map(
        (item: {
          createdAt: any;
          statusName: string;
          ID: any;
          displayName: string;
          manualPEPStatus: any;
          id: any;
          pepStatus: string;
        }) => {
          const manualPEPStatus =
            item?.manualPEPStatus &&
            (MANUAL_PEP_STATUS_MAP[item.manualPEPStatus] ??
              item.manualPEPStatus);
 
          const transformedStatus = (
            manualPEPStatus ||
            item?.pepStatus ||
            item?.statusName ||
            item?.displayName ||
            ""
          ).toLocaleLowerCase();
 
          return {
            createdAt: item?.createdAt,
            statusName: transformedStatus,
            pepStatusName: item?.statusName,
            id: item?.ID || item?.id,
          };
        },
      );
 
      return arr?.slice(0, maxListToShow);
    },
    {
      enabled: !!merchantID && !!data?.id,
    },
  );
  const ICON = expanded ? CaretUpIcon : CaretDownIcon;
 
  const lastCheckStatusName: any = ofacData?.[0]?.statusName;
 
  const ofacPepStatus = getOfacPepStatus(palette);
  const isValidStatus = lastCheckStatusName in ofacPepStatus;
 
  const { color, label } = isValidStatus
    ? ofacPepStatus[lastCheckStatusName as ExtendedPEPStatusType]
    : ofacPepStatus["pending"];
  const name = `${data?.firstName || ""} ${data?.lastName || ""}`;
  const isEmptyMatch = isMATCH && !isValidStatus;
 
  const viewMore = () =>
    handleViewMore({
      id: data?.id,
      name,
      pepStatusName: lastCheckStatusName,
      createdAt: ofacData?.[0]?.createdAt,
      countryOfResidence: data?.countryOfResidence,
      citizenship: data?.citizenship,
    });
 
  const isPossibleMatch = lastCheckStatusName === "possible_match";
  const isConfirmedMatch = lastCheckStatusName === "confirmed_match";
  const buttons: ActionButtonsProps[] = [
    {
      label: "View Details",
      variant: "filled",
      color: "light",
      onClick: viewMore,
      dataTestId: "view-details-button",
    },
    {
      label: isMATCH ? "New Report" : "Check Now",
      onClick: handleCheckNowFunctions,
      isLoading: isLoadingCheckNow,
      dataTestId: "action-button",
      hidden: isPossibleMatch || isConfirmedMatch,
    },
  ];
 
  const lastCheck = ofacData?.[0]?.createdAt;
  const lastNextCheckArray = [
    {
      title: isPEP_BO ? "" : name,
      subTitle: "Latest Check",
      date: lastCheck ? formatTimestampWithTimezone(lastCheck) : "",
    },
    {
      title: "OFAC Check will run automatically on a monthly basis.",
      subTitle: "Next Check",
      date: lastCheck
        ? formatTimestampWithTimezone(lastCheck + THIRTY_DAYS_IN_SECONDS)
        : "",
      isIcon: true,
      hidden: isMATCH || isPEP_BO,
    },
  ];
  const previousChecks = ofacData?.slice(1);
 
  if (isEmptyMatch) {
    return (
      <Stack
        padding="20px"
        direction={isMobileView ? "column" : "row"}
        gap="16px"
        alignItems="start"
      >
        <GiveText variant="bodyS" color="secondary">
          {SUMMARY_TEXT}
        </GiveText>
        <GiveButton
          label="New Report"
          size="small"
          sx={{ minWidth: "103px" }}
          onClick={handleCheckNowFunctions}
          data-testid="match-new-report-button"
        />
      </Stack>
    );
  }
 
  return (
    <Accordion
      onChange={handleChange}
      expanded={expanded}
      sx={{
        boxShadow: "none",
        "&:before": { display: "none" },
      }}
    >
      <HideOrShow
        component={
          <AccordionHeader
            name={name}
            data={data}
            color={color}
            label={label}
            ICON={ICON}
          />
        }
        hidden={isPAH_OFAC || isMATCH || isOFAC_MERCHANT_CHECK}
      />
      <AccordionDetails sx={{ padding: "20px" }}>
        <>
          <HideOrShow
            component={
              <AccordionDetailsHeader
                lastCheckStatusName={
                  lastCheckStatusName as ExtendedPEPStatusType
                }
                isTextBelowIcon={isMobileView}
                buttons={buttons}
              />
            }
            hidden={false}
          />
 
          <HideOrShow
            component={
              <LastCheckArray lastNextCheckArray={lastNextCheckArray} />
            }
            hidden={isEmpty(ofacData) || !ofacData}
          />
 
          <HideOrShow
            hidden={isEmpty(previousChecks) || !previousChecks}
            component={
              <GiveDivider
                sx={{
                  my: "20px",
                  bgcolor: "transparent",
                  borderColor: palette.border?.primary,
                }}
              />
            }
          />
 
          <Box>
            <HideOrShow hidden={!isLoading} component={<LoadingComponent />} />
            <HideOrShow
              hidden={isEmpty(previousChecks) || !ofacData || isLoading}
              component={
                <AccordionListItems
                  handleViewMore={viewMore}
                  ofacData={previousChecks}
                />
              }
            />
          </Box>
        </>
      </AccordionDetails>
    </Accordion>
  );
}
 
export default CustomAccordion;