All files / src/pages/Welcome useGetProfileStatus.ts

100% Statements 26/26
88.67% Branches 47/53
100% Functions 1/1
100% Lines 26/26

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            17x 80x 80x         80x 80x 80x     80x                 80x       80x     80x   80x 80x     80x                 80x   80x   80x         80x         80x   80x           80x           80x           80x         80x           80x         80x       80x                                                    
import useFindBusinessProfileById from "@hooks/enterprise-api/merchants/useFindBusinessProfileById";
import { shouldBeHidden } from "@constants/constants";
import { STATUS_ENUM } from "@components/ProfilePage/ProfileSetupHome/HomepageCard";
import { useGetBankAccounts } from "@services/api/products/manage-money";
import { checkPortals } from "@utils/routing";
 
export const useGetProfileStatus = (merchant: any) => {
  const { isMerchantPortal } = checkPortals();
  const { data: legalEntity, isLoading } = useFindBusinessProfileById({
    id: merchant?.legalEntityID,
    merchantId: merchant?.accID,
  });
 
  const isOnApprovalDelay = merchant?.isOnApprovalDelay;
  const isSponsorApproved = merchant?.sponsorStatusName === "approved";
  const isUnderwritingApproved = merchant?.statusName === "approved";
 
  const { data: bankAcountData, isLoading: bankAccountsLoading } =
    useGetBankAccounts(
      { sorting: "createdAt", merchantId: merchant?.accID, page: 1 },
      {
        refetchOnWindowFocus: false,
        enabled: Boolean(merchant?.accID),
      },
    );
 
  const hasPrincipal =
    !!legalEntity &&
    ((legalEntity?.principals && legalEntity?.canEdit) ||
      !legalEntity?.canEdit);
 
  const isLEApproved = legalEntity?.statusName === "approved";
 
  const isLEUnderVerification =
    !!legalEntity && legalEntity?.statusName === "ready_for_verification";
 
  const isBankAccountUnderVerification = (bankAcountData?.total || 0) > 0;
  const isApprovedPAH = merchant?.owner?.statusName === "approved";
 
  const isBusinessInfoCompleted =
    merchant?.highTicketAmount >= 0 &&
    merchant?.averageTicketAmount >= 0 &&
    merchant?.servicePhoneNumber &&
    merchant?.annualCreditCardSalesVolume >= 0 &&
    merchant?.websiteURL &&
    (!merchant?.serviceCountriesOutUSCanada ||
      (merchant?.serviceCountriesOutUSCanada &&
        merchant?.countriesServicedOutside.length > 0));
 
  const isBusinessApproved = isLEApproved && isBusinessInfoCompleted;
  const isBusinessUnderVerification =
    isBusinessInfoCompleted && isLEUnderVerification;
 
  const isBankAccountApproved = Boolean(
    merchant?.hasApprovedBankAccount || merchant?.hasApprovedLinkedBankAccount,
  );
 
  const isMSACompleted =
    (merchant?.msaHadAgreed &&
      merchant?.msaLastAcceptedVersion === merchant?.aquirerTcVersion) ||
    shouldBeHidden.merchantAgreement;
 
  const pahUnderVerification =
    merchant?.owner?.statusName === "ready_for_verification";
 
  const pahStatus = isApprovedPAH
    ? STATUS_ENUM.APPROVED
    : pahUnderVerification
    ? STATUS_ENUM.UNDER_VERIFICATION
    : STATUS_ENUM.INCOMPLETE;
 
  const businessDetailsStatus = isBusinessApproved
    ? STATUS_ENUM.APPROVED
    : isBusinessUnderVerification
    ? STATUS_ENUM.UNDER_VERIFICATION
    : STATUS_ENUM.INCOMPLETE;
 
  const bankAccountStatus = isBankAccountApproved
    ? STATUS_ENUM.APPROVED
    : isBankAccountUnderVerification
    ? STATUS_ENUM.UNDER_VERIFICATION
    : STATUS_ENUM.INCOMPLETE;
 
  const agreementStatus = isMSACompleted
    ? STATUS_ENUM.APPROVED
    : STATUS_ENUM.INCOMPLETE;
 
  const isCompletedProfile =
    pahStatus != STATUS_ENUM.INCOMPLETE &&
    businessDetailsStatus != STATUS_ENUM.INCOMPLETE &&
    bankAccountStatus != STATUS_ENUM.INCOMPLETE &&
    agreementStatus != STATUS_ENUM.INCOMPLETE;
 
  const isUnderReviewProfile =
    (pahStatus === STATUS_ENUM.UNDER_VERIFICATION ||
      businessDetailsStatus === STATUS_ENUM.UNDER_VERIFICATION ||
      bankAccountStatus === STATUS_ENUM.UNDER_VERIFICATION) &&
    isCompletedProfile;
 
  const isProfileReady = isMerchantPortal
    ? isSponsorApproved || isCompletedProfile
    : isCompletedProfile;
 
  return {
    allLoaded: !isLoading && !bankAccountsLoading,
    hasPrincipal,
    isBusinessDetailsCompleted: businessDetailsStatus != STATUS_ENUM.INCOMPLETE,
    isCompletedProfile,
    isUnderReviewProfile,
    isMSACompleted,
    bankAccounts: {
      data: bankAcountData?.total,
      rows: bankAcountData?.data,
      isLoading: bankAccountsLoading,
    },
    legalEntity: {
      data: legalEntity,
      isLoading,
    },
    pahStatus,
    agreementStatus,
    bankAccountStatus,
    businessDetailsStatus,
    isSponsorApproved,
    isUnderwritingApproved,
    isProfileReady,
    isOnApprovalDelay,
  };
};