All files / src/components/Merchants/MerchantPreview/modals/hooks useAgreementStatus.tsx

100% Statements 22/22
90.62% Branches 29/32
100% Functions 4/4
100% Lines 21/21

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                                        41x                   94x       94x 94x   27x         94x 28x 1x 27x 24x       94x       94x 94x   94x   94x 94x 7x               87x 2x                 85x                   94x    
import { useMemo, useCallback } from "react";
import { TMerchantAgreementPayload } from "@components/Merchants/CreateMerchantPanel/hooks/TMerchantAgreementPayload";
import { IParsedData } from "@components/Merchants/MerchantPreview/data.types";
import { AgreementState } from "@features/Merchants/MerchantSidePanel/AgreementAndSnapshot/agreements.types";
import {
  formatAgreementDate,
  getDisplayTimestamp,
  renderSpan,
} from "../../MerchantAgreement/helpers";
 
interface UseAgreementStatusProps {
  data?: TMerchantAgreementPayload & { isEnterPrise?: boolean };
  APIData?: any;
  signatureFile?: File;
  globalName?: { firstName: string; lastName: string };
  merchantData?: Partial<IParsedData>;
  isMerchant?: boolean;
  canBePending?: boolean;
}
 
export const useAgreementStatus = ({
  data,
  APIData,
  signatureFile,
  globalName,
  merchantData,
  isMerchant,
  canBePending = true, // On creation flow the signature can't be pending because the agreement is being signed for the first time
}: UseAgreementStatusProps) => {
  //data from /msa-version
  const APIMsaVersion = isMerchant
    ? APIData?.submerchantMSAVersion
    : APIData?.enterpriseMSAVersion;
 
  const latestVersion = data?.tcVersion || APIMsaVersion || "";
  const isPending = useMemo(
    () =>
      !!data?.msaLastAcceptedVersion &&
      data?.msaLastAcceptedVersion !== latestVersion,
    [data?.msaLastAcceptedVersion, latestVersion],
  );
 
  const status: AgreementState = useMemo(() => {
    if (isPending && (data?.signatureURL || signatureFile) && canBePending)
      return "newVersion";
    if (signatureFile || data?.signatureURL) return "signed";
    return "not_signed";
  }, [isPending, signatureFile, data?.signatureURL, canBePending]);
 
  const signedBy =
    data?.signedBy ||
    merchantData?.merchantAgreement?.signedBy ||
    `${globalName?.firstName ?? ""} ${globalName?.lastName ?? ""}`.trim();
 
  const timestamp = getDisplayTimestamp(data, merchantData, signatureFile);
  const { signedDate, signedDateTime } = formatAgreementDate(timestamp);
  const currentVersion =
    data?.msaLastAcceptedVersion || APIData?.msaLastAcceptedVersion || "";
 
  const renderSubtitle = useCallback(() => {
    if (status === "signed") {
      return (
        <>
          {renderSpan(signedBy)} agreed on {renderSpan(signedDate, true)} to
          Version {renderSpan(currentVersion)}
        </>
      );
    }
 
    if (status === "newVersion") {
      return (
        <>
          {renderSpan(signedBy)} agreed on {renderSpan(signedDateTime, true)} to
          version {renderSpan(currentVersion)} but a new TOS version (
          {renderSpan(latestVersion)}) is available
        </>
      );
    }
 
    return "The agreement has not been signed yet. The agreement can be signed by authorized users or Primary Account Holder.";
  }, [
    status,
    signedBy,
    signedDate,
    signedDateTime,
    currentVersion,
    latestVersion,
  ]);
 
  return { status, renderSubtitle };
};