All files / src/componentsV2/SideMenu/components/hooks useAccStatus.tsx

72.22% Statements 13/18
31.25% Branches 5/16
40% Functions 2/5
76.47% Lines 13/17

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                  12x             14x 14x 14x   14x   14x 2x         14x     14x           14x                                     14x                   14x       14x                
import { useMemo } from "react";
import NiceModal from "@ebay/nice-modal-react";
import { merchantAgreementParser } from "@components/Merchants/MerchantPreview/helpers/parsers";
import { GIVE_MERCHANT_AGREEMENT_MODAL } from "modals/modal_names";
import useCheckPAH from "@hooks/common/useCheckPAH";
import { useNavigate } from "react-router-dom";
import { useGetProfileStatus } from "@pages/Welcome/useGetProfileStatus";
import { checkPortals } from "@utils/routing";
 
const useAccStatus = ({
  merchant,
  isEnterprise = false,
}: {
  merchant: any;
  isEnterprise?: boolean;
}) => {
  const { isLoggedInPAH } = useCheckPAH();
  const { isMerchantPortal } = checkPortals();
  const navigate = useNavigate();
  const { isUnderReviewProfile, isProfileReady } =
    useGetProfileStatus(merchant);
 
  const merchantAgreement = useMemo(
    () => merchantAgreementParser(merchant),
    [merchant],
  );
 
  const isPending =
    merchantAgreement.msaHadAgreed &&
    merchantAgreement.msaLastAcceptedVersion !== merchantAgreement.tcVersion;
 
  const navigateToManageMoney = () => {
    const isAccountCompleted =
      isProfileReady && (isMerchantPortal ? !isUnderReviewProfile : true);
    if (isAccountCompleted) navigate("/merchant/manage-money");
  };
  //give agreement modal use different type of data
  const parseMerchantToAgreementData = (merchant: any) => {
    return {
      merchantAgreement: {
        tcVersion: merchant?.msaVersion,
        msaLastAcceptedVersion: merchant?.msaLastAcceptedVersion,
        msaLastUpdatedAt: merchant?.msaLastUpdatedAt,
        msaHadAgreed: merchant?.msaHadAgreed,
        msaRefundPolicy: merchant?.msaRefundPolicy,
        msaPCICompliant: merchant?.msaPCICompliant,
        msaPreviousTermination: merchant?.msaPreviousTermination,
        msaPreviousTerminationProcessorName:
          merchant?.msaPreviousTerminationProcessorName,
        msaPreviousTerminationReason: merchant?.msaPreviousTerminationReason,
        msaPreviousTerminationAt: merchant?.msaPreviousTerminationAt ?? null,
        signatureURL: merchant?.msaLastAcceptedBySignatureURL,
      },
    };
  };
 
  const handleCheckUpdate = () => {
    NiceModal.show(GIVE_MERCHANT_AGREEMENT_MODAL, {
      data: parseMerchantToAgreementData(merchant),
      merchantId: merchant?.accID,
      onSigningCompleted: navigateToManageMoney,
    });
  };
 
  // For enterprise: only check if there's a pending update (similar to GiveProviderAgreementContent)
  // For merchant: keep existing PAH checks
  const noUpdate = isEnterprise
    ? !isPending
    : !isPending || !isLoggedInPAH || !merchantAgreement?.approvedPAH;
 
  return {
    noUpdate,
    handleCheckUpdate,
    isAgreementSigned: merchant?.msaHadAgreed,
  };
};
 
export default useAccStatus;