All files / src/features/Merchants/MerchantSidePanel/GiveMerchantFile/hooks useGetSimpleCardSectionData.tsx

84% Statements 42/50
86.84% Branches 33/38
62.5% Functions 10/16
84% Lines 42/50

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                                                                                    21x 103x 103x 103x   103x   103x 309x   103x   103x         103x     103x       103x 1x                 103x       103x 103x 103x 86x   103x 86x         103x 103x 103x 86x     103x           103x           103x             103x               103x           103x 2x                   103x         103x 103x   103x   103x   103x 103x               1x                       2x                                 103x     103x        
import NiceModal from "@ebay/nice-modal-react";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import {
  NEW_MATCH_REPORT_MODAL,
  GIVE_OFAC_CHECK_MODAL,
} from "modals/modal_names";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import { useGetMATCHReports } from "@features/Merchants/MerchantSidePanel/Modals/MatchReport/hooks/useMATCHReports";
import { useConversationsModal } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import { SyntheticEvent } from "react";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { checkPortals } from "utils/routing";
import { PEPStatusType } from "@components/Merchants/MerchantPreview/PEP/types";
import {
  MATCH_DISABLED,
  MATCH_DISABLED_PENDING_STATUS,
  MATCH_NO_PERMISSION,
} from "@constants/stringConstants";
import { ContentViewTypes } from "@features/Merchants/MerchantSidePanel/Modals/MatchReport/match.types";
 
export type TVariant = "ofac" | "match" | "website_monitoring";
 
type TValuesObject = {
  title: string;
  description: string;
  descriptionOnDisable: string;
  lastCheck: string;
  onClick: () => void;
  onClickConv: (e: SyntheticEvent) => void;
  permissionDenied?: boolean;
  status?: PEPStatusType;
  tooltipMessage?: string;
  isLocked?: boolean;
  hideTooltip?: boolean;
};
 
const useGetSimpleCardSectionData = (variant: TVariant) => {
  const { data } = useMerchantSidePanelContext();
  const { isMastercardMatchEnabled } = useGetFeatureFlagValues();
  const { data: matchData } = useGetMATCHReports(data.merchantInfo.merchantID);
 
  const { openConversationHandler } = useConversationsModal();
 
  const getFormattedDate = (date?: number) =>
    date ? moment.unix(date).tz(PLATFORM_TIMEZONE).format("MMMM DD, YYYY") : "";
 
  const { isAcquirerEnterprises } = checkPortals();
 
  const composedOfacPermission = composePermission(
    isAcquirerEnterprises ? RESOURCE_BASE.ENTERPRISE : RESOURCE_BASE.MERCHANT,
    RESOURCE_BASE.OFAC,
  );
 
  const isPending = data?.status?.statusName === "onboarding";
 
  //OFAC logic
  const isViewOfacAllowed = useAccessControl({
    resource: composedOfacPermission,
    operation: OPERATIONS.LIST,
  });
  const handleOpenOFAC = () => {
    NiceModal.show(GIVE_OFAC_CHECK_MODAL, {
      headerData: data?.header,
      merchantId: data?.merchantInfo.merchantID,
      profileData: data?.businessProfile,
      PAH: data?.primaryAccountHolder,
      businessOwners: data?.businessOwnersList,
    });
  };
 
  const onClickOfacConv = (e: SyntheticEvent) => {
    e.stopPropagation();
    openConversationHandler({ id: 0, name: "OFAC Check", paths: [] });
  };
  const BPlastOfacCheck = data?.businessProfile?.ofac?.lastCheckAt;
  const PAHlastOfacCheck = data?.primaryAccountHolder?.ofac?.lastCheckAt;
  const BOOfacLastCheckValues = data?.businessOwnersList?.map(
    (owner) => owner.ofac?.lastCheckAt,
  );
  const latestOfacCheck = Math.max(
    ...BOOfacLastCheckValues.filter((value) => value !== undefined),
    BPlastOfacCheck ?? 0,
    PAHlastOfacCheck ?? 0,
  );
 
  const BPOfacStatus = data?.businessProfile?.ofac?.lastCheckStatusName;
  const PAHOfacStatus = data?.primaryAccountHolder?.ofac?.lastCheckStatusName;
  const BOOfacStatus = data?.businessOwnersList?.map(
    (owner) => owner.ofac?.lastCheckStatusName,
  );
  //we are showing the chip for confirmed and possible match
  const isOfacConfirmedMatch = [
    ...BOOfacStatus,
    PAHOfacStatus,
    BPOfacStatus,
  ]?.includes("confirmed_match");
 
  const isOfacPossibleMatch = [
    ...BOOfacStatus,
    PAHOfacStatus,
    BPOfacStatus,
  ]?.includes("possible_match");
 
  const ofacStatus = isOfacConfirmedMatch
    ? "confirmed_match"
    : isOfacPossibleMatch
    ? "possible_match"
    : undefined;
 
  //MATCH logic
  const isViewReadMATCHAllowed = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.MATCH_REPORT,
    ),
    operation: OPERATIONS.READ,
  });
 
  const matchDescriptionOnDisable = isPending
    ? MATCH_DISABLED_PENDING_STATUS
    : isViewReadMATCHAllowed
    ? MATCH_DISABLED
    : MATCH_NO_PERMISSION;
 
  const handleOpenMATCH = () => {
    NiceModal.show(NEW_MATCH_REPORT_MODAL, {
      merchantID: data.merchantInfo.merchantID,
      defaultReportsData: matchData ?? [],
      initialContentView:
        matchData?.length > 0 || !isMastercardMatchEnabled
          ? ContentViewTypes.MATCH
          : ContentViewTypes.ADD_NEW_REPORT,
    });
  };
 
  const onClickMatchConv = (e: SyntheticEvent) => {
    e.stopPropagation();
    openConversationHandler({ id: 0, name: "MATCH Check", paths: [] });
  };
  const lastMatchCheck =
    matchData && matchData?.length > 0 ? matchData[0].createdAt : undefined;
  const matchDescription = lastMatchCheck ? "Latest Check" : "Add New Report";
  const matchStatus =
    matchData && matchData?.length > 0 ? matchData[0].statusName : "";
  const matchStatusToShow =
    matchStatus === "confirmed_match" ? "confirmed_match" : undefined;
 
  const getCardData = (variant: TVariant) => {
    const modalValues: Record<TVariant, TValuesObject> = {
      ofac: {
        title: "OFAC",
        description: "Latest Check",
        descriptionOnDisable: isPending
          ? "OFAC Check is not available for merchants with Pending status"
          : "Please create a Business Profile or add a Primary Account Holder with both first and last name provided",
        lastCheck: getFormattedDate(latestOfacCheck),
        onClick: () => handleOpenOFAC(),
        onClickConv: (e) => onClickOfacConv(e),
        permissionDenied: !isViewOfacAllowed,
        status: ofacStatus,
      },
      match: {
        title: `MATCH`,
        description: matchDescription,
        descriptionOnDisable: isMastercardMatchEnabled
          ? matchDescriptionOnDisable
          : `Please create a Business Profile first`,
        lastCheck: getFormattedDate(lastMatchCheck),
        onClick: () => handleOpenMATCH(),
        onClickConv: (e) => onClickMatchConv(e),
        permissionDenied: !isViewReadMATCHAllowed,
        status: matchStatusToShow,
        hideTooltip: isMastercardMatchEnabled,
      },
      //will come in a future itteration
      website_monitoring: {
        title: `Website Monitoring`,
        description: "Add New Report",
        descriptionOnDisable: ``,
        lastCheck: getFormattedDate(),
        onClick: () => console.log("open website monitoring"),
        onClickConv: (e) => onClickOfacConv(e),
        status: undefined,
      },
    };
    return modalValues[variant];
  };
 
  return getCardData(variant);
};
 
export default useGetSimpleCardSectionData;