All files / src/features/Merchants/MerchantSidePanel/GiveMerchantFile/components BusinessOwnersSection.tsx

92.59% Statements 25/27
66.66% Branches 20/30
87.5% Functions 7/8
96% Lines 24/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 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                                                          54x 175x                 54x                 37x 37x 37x 37x 37x   37x 18x   18x 18x         37x   37x   37x                                   37x   37x                 37x 37x     37x                                       37x                               74x 74x                                                                       37x                                            
import GiveText from "@shared/Text/GiveText";
import GiveBusinessOwnerItem from "../businessOwners/GiveBusinessOwnerItem";
import { adjustAddressType } from "../hooks/helpers";
import {
  TBusinessOwner,
  TMerchantDocument,
} from "@components/Merchants/MerchantPreview/data.types";
import { Stack } from "@mui/material";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import { checkPortals } from "@utils/routing";
import { useMemo } from "react";
import useBusinessOwner from "@components/Settings/ManageTeam/BusinessOwners/useBusinessOwner";
import { ChatsCircleIcon, PlusIcon } from "@phosphor-icons/react";
import { HeaderAction } from "./BankAccountSection";
import { useConversationsModal } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  CREATE_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import GiveLink from "@shared/Link/GiveLink";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
 
export const getIDFile = (ownerID: any, documents?: TMerchantDocument[]) => {
  return documents && documents.find((doc) => ownerID === doc.legalPrincipalID);
};
 
type Props = {
  businessOwners: TBusinessOwner[];
  merchantId: number;
  isBusinessAddressAvailable: boolean;
};
 
const BusinessOwnersSection = ({
  businessOwners,
  merchantId,
  isBusinessAddressAvailable,
}: Props) => {
  const {
    id,
    data: { documents, businessProfile, businessOwnersList, header },
    isEnterprise,
  } = useMerchantSidePanelContext();
  const { isEnterprisePortal, isAcquirerPortal } = checkPortals();
  const { merchant_underwriting } = useEnterprisePermissions();
  const { isNewConversationsEnabled } = useGetFeatureFlagValues();
  const businessOwnersLength = businessOwners.length;
 
  const totalOwnerships = useMemo(() => {
    return (
      businessOwnersList?.reduce((acc, owner) => {
        const _ownership = owner.ownership ? +owner.ownership : 0;
        return acc + _ownership;
      }, 0) || 0
    );
  }, [businessOwnersList]);
  const isApprovedLE =
    header?.businessProfileStatus?.status?.toLowerCase() === "approved";
  const isIndividualSoleProprietorship =
    businessProfile?.businessType === "individual_sole_proprietorship";
 
  const { addOwnerHandler, editOwnerHandler } = useBusinessOwner({
    stakeSum: totalOwnerships,
    isApprovedLE,
    hasMaxOwners:
      (isIndividualSoleProprietorship && businessOwnersLength >= 1) || false,
    legalEntityID: businessProfile?.id,
    showTag: true,
    merchantId,
    isSoleProprietorship: isIndividualSoleProprietorship,
    isBPLinked: businessProfile?.isBPLinked,
    businessType: businessProfile?.businessType,
    documents,
    isMerchant: true,
    isBusinessAddressAvailable,
    businessOwnersLength,
  });
 
  const limitCreation =
    isIndividualSoleProprietorship && (businessOwners?.length || 0) >= 1;
 
  const isAddAllowed = useAccessControl({
    resource: composePermission(
      isEnterprise ? RESOURCE_BASE.ENTERPRISE : RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.LEGAL_ENTITY,
      RESOURCE_BASE.PRINCIPAL,
    ),
    operation: OPERATIONS.CREATE,
  });
 
  const { openConversationHandler } = useConversationsModal();
  const handleBOConv = () =>
    openConversationHandler({ id: 0, name: "Business Owners", paths: [] });
 
  const headerActions: HeaderAction[] = [
    {
      Icon: PlusIcon,
      onClick: addOwnerHandler,
      hidden: false,
      iconText: "Add",
      size: "extraSmall",
      disabled: limitCreation || !isAddAllowed,
      tooltipTitle: limitCreation
        ? "The sole proprietorship legal entities cannot have more than one business owner"
        : CREATE_DENY_MESSAGE,
    },
    {
      Icon: ChatsCircleIcon,
      onClick: handleBOConv,
      hidden:
        (!merchant_underwriting && !isAcquirerPortal) ||
        isNewConversationsEnabled,
    },
  ];
  return (
    <Stack>
      <Stack
        justifyContent="space-between"
        alignItems="center"
        flexDirection="row"
        width="100%"
        marginBottom="20px"
      >
        <GiveText>Business Owners</GiveText>
        <Stack gap="10px" flexDirection="row" alignItems="center">
          {headerActions?.map(
            (
              { Icon, onClick, hidden, iconText, tooltipTitle, disabled },
              idx,
            ) => {
              Iif (hidden) return null;
              return (
                <GiveTooltip
                  width="compact"
                  alignment="left"
                  title={tooltipTitle || CREATE_DENY_MESSAGE}
                  color="default"
                  disableHoverListener={!disabled}
                  key={idx}
                >
                  {iconText ? (
                    <GiveLink
                      component="button"
                      color="primary"
                      onClick={onClick}
                      disabled={disabled}
                      Icon={Icon}
                    >
                      {iconText}
                    </GiveLink>
                  ) : (
                    <GiveIconButton
                      sx={{
                        cursor: "pointer",
                      }}
                      variant="ghost"
                      onClick={onClick}
                      Icon={Icon}
                    />
                  )}
                </GiveTooltip>
              );
            },
          )}
        </Stack>
      </Stack>
      {businessOwners?.map((owner, idx) => (
        <GiveBusinessOwnerItem
          key={owner.id}
          owner={adjustAddressType(owner)}
          owners={businessOwners}
          disabled={false}
          onEdit={editOwnerHandler}
          legalEntityID={businessProfile.id}
          isEnterprise={isEnterprisePortal}
          withPepStatus
          showViewPep={true}
          idFile={owner.files?.allFiles[0] || getIDFile(owner.id, documents)}
          merchantId={id}
          isFirst={idx === 0}
          isLast={idx === businessOwnersLength - 1}
          businessType={businessProfile?.businessType}
        />
      ))}
    </Stack>
  );
};
 
export default BusinessOwnersSection;