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

92% Statements 23/25
48.14% Branches 13/27
100% Functions 6/6
92% Lines 23/25

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                                                              8x   7x                                                                   6x 6x           6x 6x 6x 6x 6x 6x                 6x 7x     6x 7x                           6x 6x           6x             6x       1x 1x     6x       6x       6x                        
import { MouseEvent } from "react";
import {
  IMerchantBankAccount,
  TMerchantBankAccountSettings,
} from "@components/Merchants/MerchantPreview/data.types";
import GiveBAStatusChip from "@features/Merchants/MerchantSidePanel/GiveMerchantFile/bankAccounts/GiveBAStatusChip";
import { findBankByRoutingNumber } from "@utils/bank_list";
import { capitalize } from "lodash";
import { useBankAccount } from "@features/Merchants/MerchantSidePanel/GiveMerchantFile/hooks/useBankAccount";
import AccordionSection from "./AccordionSection";
import { FieldConfig } from "../types";
import { ApprovalBanner, TaskEmptyState, TaskLoading } from "./Sections.atoms";
import { useIsFetching } from "react-query";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { useMerchantDateFormatter } from "@components/Merchants/MerchantPreview/helpers/parsers";
 
interface BankAccountsSectionProps {
  data: {
    bankAccounts?: IMerchantBankAccount[];
    bankAccountSettings?: TMerchantBankAccountSettings;
    merchantInfo?: {
      merchantName: string;
      enterprise: string;
      enterpriseID: number;
    };
    [key: string]: any;
  };
  merchantId?: number;
  isProvider?: boolean;
}
 
const getBankAccountFields = (
  bankAccount: IMerchantBankAccount,
): FieldConfig[] => [
  {
    label: "Created",
    value: bankAccount?.created?.replaceAll(" ", ""),
  },
  {
    label: "Account Type",
    value: capitalize(bankAccount?.type),
  },
  {
    label: "Business Name on Account",
    value: bankAccount?.name,
  },
  {
    label: "Account Number",
    value: bankAccount?.accountNumber,
  },
  {
    label: "Routing Number",
    value: bankAccount?.routingNumber,
  },
  {
    label: "Bank Name",
    value:
      bankAccount?.bankName ||
      findBankByRoutingNumber(bankAccount?.routingNumber) ||
      "-",
  },
];
function BankAccountsSection({
  data,
  merchantId,
  isProvider,
}: BankAccountsSectionProps) {
  const { merchantDateFormatter } = useMerchantDateFormatter();
  const fetchNumber = useIsFetching({
    queryKey: [
      MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_BANK_ACCOUNTS,
      merchantId,
    ],
  });
  const isFetching = fetchNumber > 0;
  const bankAccounts = data?.bankAccounts || [];
  const parentName = data?.merchantInfo?.enterprise;
  const bankAccountSettings = data?.bankAccountSettings || [];
  const enterpriseID = data?.merchantInfo?.enterpriseID;
  const { handleEdit } = useBankAccount(
    bankAccounts,
    merchantId,
    enterpriseID,
    bankAccountSettings as any,
    undefined,
    isProvider,
  );
 
  const renderStatusChip = (bankAccount: IMerchantBankAccount) => (
    <GiveBAStatusChip accountStatus={bankAccount.status} />
  );
 
  const renderExtraContent = (bankAccount: IMerchantBankAccount) =>
    bankAccount.approvedByName ? (
      <ApprovalBanner
        text={`Bank account was approved for ${
          bankAccount.enterprise
            ? parentName || ""
            : data?.merchantInfo?.merchantName || ""
        } on ${
          bankAccount.approvedAt
            ? merchantDateFormatter("MM/dd/yyyy", bankAccount.approvedAt) || ""
            : ""
        } by ${bankAccount.approvedByName}.`}
      />
    ) : null;
 
  const getBankAccountDisplayData = (bankAccount: IMerchantBankAccount) => {
    const headerDescription = bankAccount.plaid
      ? "Connected With Plaid"
      : bankAccount.enterprise
      ? "Linked to Provider"
      : "Manually Uploaded";
 
    return {
      ...bankAccount,
      title: bankAccount.name,
      subtitle: headerDescription,
    };
  };
 
  const handleBankAccountEdit = (
    event: MouseEvent<HTMLElement>,
    bankAccount: IMerchantBankAccount,
  ) => {
    event.stopPropagation();
    handleEdit(bankAccount);
  };
 
  Iif (isFetching) {
    <TaskLoading />;
  }
 
  Iif (!bankAccounts?.length) {
    return <TaskEmptyState section="bank-accounts" />;
  }
 
  return (
    <AccordionSection
      items={bankAccounts.map(getBankAccountDisplayData)}
      getFieldsList={getBankAccountFields}
      statusComponent={renderStatusChip}
      onEdit={handleBankAccountEdit}
      extraContent={renderExtraContent}
    />
  );
}
 
export default BankAccountsSection;