All files / src/components/Merchants/MerchantPreview/modals GiveEditBusinessProfileModal.tsx

92.3% Statements 36/39
88.17% Branches 82/93
75% Functions 6/8
94.73% Lines 36/38

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498                                                                                                                      211x               211x   211x   211x               211x   211x 211x 211x   211x   211x   211x     211x     211x       211x 211x 211x 57x 25x         25x       211x 19x     211x   211x 211x       211x       211x       211x   211x   211x                                                                         19x 19x 19x       19x                                                                                                                                                                                                                                                                                                                                                                                                                                                                             211x         211x                                                                                                       3798x                                                              
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { CreateBusinessProfileModalProps } from "../../CreateMerchantPanel/types";
import useBusinessProfileModal from "../../CreateMerchantPanel/modals/useBusinessProfileModal";
import { Stack, Box, Grid } from "@mui/material";
import { FileTextIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { ControlledDatePicker } from "@sections/PayBuilder/Forms/DateAndLocation/components/ControlledDatePicker";
import { FormProvider } from "react-hook-form";
import { GiveNameInput } from "@common/BusinessProfileInputs";
import GiveTaxIdInput from "@shared/GiveInputs/GiveTaxIdInput";
import GiveBusinessTypeSelect from "@common/BusinessProfileInputs/GiveBusinessTypeSelect";
import GiveOwnershipTypeSelect from "@common/BusinessProfileInputs/GiveOwnershipTypeSelect";
import { HFGiveTelephone } from "@shared/HFInputs/HFGiveTelephone/HFGiveTelephone";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { OFAC_STATUS } from "@constants/constants";
import { useEffect } from "react";
import { GiveProvinceInput, GiveZipCodeInput } from "@common/AddressInputs";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import { GiveHFStreetAddress } from "@common/RHFStreetAddress/GiveHFStreetAddress";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import useCountryCodes from "@hooks/common/useCountryCodes";
import { EDIT_DENY_MESSAGE, UPDAT_DENY_BY_OFAC } from "@constants/permissions";
import GiveAlert from "@shared/GiveAlert/GiveAlert";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import GiveButton from "@shared/Button/GiveButton";
import { checkPortals } from "@utils/routing";
import HFGiveSwitch from "@shared/HFInputs/HFGiveSwitch/HFGiveSwitch";
import { CustomToolTip as GiveCustomTooltip } from "componentsV2/Table/CustomTooltip";
import BusinessProfileSelect from "../components/BusinessProfile/BusinessProfileSelect";
import GiveBusinessProfileItem from "../components/BusinessProfile/GiveBusinessProfileItem";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import CheckStatusRow from "../components/BusinessOwners/CheckStatusRow";
import { FormType } from "../../CreateMerchantPanel/modals/CreateBusinessProfileModal";
 
function GiveEditBusinessProfileModal({
  data,
  formType = FormType.CREATE_MERCHANT,
  isLegalEntityApproved = false,
  isEdit = false,
  section = "business_profile",
  merchantName,
  merchantId,
  ...rest
}: CreateBusinessProfileModalProps) {
  const {
    open,
    isLoading,
    canEdit,
    canEditWithLegalEntity,
    showAccessDeniedTooltip,
    handleClose,
    onSubmit,
    methods,
    parsedSelectedProfile,
    setSelectedProfile,
    isErrors,
    checkedTaxID,
  } = useBusinessProfileModal({
    data,
    formType,
    isLegalEntityApproved,
    isEdit,
    merchantId,
    ...rest,
  });
  const { isMobileView } = useCustomThemeV2();
 
  const theme = useAppTheme();
  const { modify_merchant, b_profile_linking, merchant_underwriting } =
    useEnterprisePermissions();
 
  const {
    watch,
    formState: { isValid, errors },
    setValue,
    resetField,
    trigger,
  } = methods;
  const { isEnterprisePortal, isEnterpriseTable, isAcquirerPortal } =
    checkPortals();
  const { selectCountriesList } = useCountryCodes();
  const { isMerchantOnboardingModalEnabled } = useGetFeatureFlagValues();
 
  const isEntreprise = isEnterprisePortal || isEnterpriseTable;
 
  const values = watch();
  const isEditableByStatus =
    data?.statusName === "suspended" || data?.statusName === "pending";
 
  const isOFACMatch =
    data?.ofac?.lastCheckStatusName === OFAC_STATUS.CONFIRMED_MATCH ||
    data?.ofac?.lastCheckStatusName === OFAC_STATUS.POSSIBLE_MATCH;
 
  const isInputDisabled = (!canEdit && !isEditableByStatus) || isOFACMatch;
 
  //we need this useEffect to recheck checkTaxIDAvailability when the link BP is checked from on to off once again
  //when the link BP is checked from on to off once again and the id is deleted and reentered the same value
  const isLinkBusinessProfile = values.isLinkBusinessProfile;
  const taxID = values.taxID;
  useEffect(() => {
    if (!isLinkBusinessProfile && taxID.length === 10) {
      checkedTaxID.current = {
        taxId: "",
        isDeclinedOrNotApproved: false,
        isLinked: false,
      };
      trigger("taxID");
    }
  }, [values.taxID, isLinkBusinessProfile]);
 
  useEffect(() => {
    !data?.legalName && merchantName && setValue("legalName", merchantName);
  }, [data]);
 
  const bpOwner = isEntreprise ? "provider" : "merchant";
 
  const addressWarningText = `Do not enter a P.O. Box as the ${bpOwner}’s business address. If the ${bpOwner} operates from a residence, provide a brief explanation below`;
  const linkProfileText = `If the ${bpOwner} belongs to an existing business use this to
                  link it to that profile.`;
 
  const listOfacAllowed =
    ((isEnterprisePortal && merchant_underwriting) || isAcquirerPortal) &&
    isEdit;
 
  const listOfac =
    listOfacAllowed &&
    data?.ofac &&
    !(values.isLinkBusinessProfile && b_profile_linking);
 
  const residenceCountry = watch("address.country");
 
  const isResidentInUs = residenceCountry === "US";
 
  const inputList = [
    {
      input: (
        <Stack
          borderRadius="8px"
          p="16px"
          mb={listOfac ? "20px" : "0"}
          sx={{
            background: theme.palette.surface?.secondary,
          }}
        >
          <HFGiveSwitch
            name="isLinkBusinessProfile"
            size="small"
            data-testid="link-business-profile-switch"
            label={
              <Stack gap="4px">
                <GiveText color="primary" fontWeight={400} fontSize={14}>
                  Link to an existing profile
                </GiveText>
                <GiveText color="secondary" fontWeight={400} fontSize={12}>
                  {linkProfileText}
                </GiveText>
              </Stack>
            }
            disabled={isInputDisabled}
          />
        </Stack>
      ),
      hidden: (isEdit && isLegalEntityApproved) || !b_profile_linking,
      hasTooltip: true,
    },
    {
      input: (
        <BusinessProfileSelect
          selectedProfile={parsedSelectedProfile}
          onNewItemSelected={(value: any) => {
            const errorFields: any = Object.keys(errors);
            Iif (errorFields.length) methods.trigger(errorFields);
            setSelectedProfile(value);
          }}
          merchantId={merchantId}
          resetLinkedBusinessProfileValue={() =>
            resetField("linkedBusinessProfile")
          }
          useGlobalEndpoint={true}
        />
      ),
      hidden: !values.isLinkBusinessProfile || !b_profile_linking,
    },
    {
      input: (
        <CheckStatusRow
          label="Office of Foreign Assets Control Check:"
          status={data?.ofac?.lastCheckStatusName}
          timestamp={data?.ofac?.lastCheckAt}
        />
      ),
      hidden: !listOfac,
      inputSx: { paddingTop: "0 !important" },
    },
 
    {
      input: (
        <HFGiveInput
          name="legalName"
          label="Legal Name"
          disabled={
            (!canEditWithLegalEntity && !isEditableByStatus) || isOFACMatch
          }
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: (
        <HFGiveInput
          name="DBA"
          label="Doing Business As (Optional)"
          disabled={isInputDisabled}
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: (
        <ControlledDatePicker
          name="businessOpenedAt"
          label="Business Creation Date"
          maxDate={new Date()}
          disabled={!canEdit && !isEditableByStatus}
          data-testid="business-creation-date"
          hidePlaceholder={false}
          placeholder="MM/DD/YYYY"
          useUTCMoment
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: (
        <Box>
          <GiveText
            variant="bodyS"
            mb="8px"
            color={errors?.taxID ? "error1" : "primary"}
          >
            Federal Tax ID
          </GiveText>
          <GiveTaxIdInput
            ssnName="ssn"
            taxIdName="taxID"
            businessTypeName="businessType"
            tinType="tinType"
            disabled={false}
            data-testid="business-tax-id"
          />
        </Box>
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: (
        <GiveBusinessTypeSelect
          name="businessType"
          label="Business Type"
          disabled={
            (!canEditWithLegalEntity && !isEditableByStatus) || isOFACMatch
          }
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: (
        <GiveOwnershipTypeSelect
          name="ownershipType"
          label="Business Ownership Type"
          businessTypeName="type"
          //  disabled={type === "sole_proprietorship"}
          data-testid="business-ownership-type"
          disabled={isInputDisabled}
        />
      ),
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: (
        <HFGiveTelephone
          name="contactPhone"
          label="Business Phone Number"
          fullWidth
          data-testid="business-phone"
          disabled={isInputDisabled}
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile || !modify_merchant,
      hasTooltip: true,
    },
    {
      input: <GiveText variant="bodyL">Business Address</GiveText>,
      inputSx: { paddingTop: "32px !important" },
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: <GiveAlert type="warning2" description={addressWarningText} />,
      inputSx: { paddingTop: "16px !important" },
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: (
        <HFGiveSelect
          name="address.country"
          label="Country"
          options={selectCountriesList}
          disabled
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: (
        <GiveHFStreetAddress
          name="address.address"
          label="Street Address"
          fullWidth
          disabled={isInputDisabled}
        />
      ),
      isInput: true,
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: (
        <HFGiveInput
          name="address.city"
          label="City"
          fullWidth
          disabled={isInputDisabled}
        />
      ),
      sm: 12,
      md: 4,
      lg: 4,
      isInput: true,
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: (
        <GiveProvinceInput
          name="address.state"
          isUS={values.address?.country === "US"}
          disabled={isInputDisabled}
        />
      ),
      sm: 12,
      md: 4,
      lg: 4,
      isInput: true,
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: (
        <GiveZipCodeInput
          name="address.zip"
          label="Zip Code"
          letterSpacing="4px"
          disabled={isInputDisabled}
          isUSResident={isResidentInUs}
        />
      ),
      sm: 12,
      md: 4,
      lg: 4,
      hidden: values.isLinkBusinessProfile,
    },
    {
      input: (
        <HFGiveInput
          name="address.notes"
          fullWidth
          label={"Notes (Optional)"}
          placeholder={
            "Briefly explain if business operates from a residential address..."
          }
          type="text"
          multiline
          rows={4}
          inputProps={{
            maxLength: "200",
          }}
        />
      ),
      hidden:
        values.isLinkBusinessProfile ||
        !modify_merchant ||
        !isMerchantOnboardingModalEnabled,
      hasTooltip: true,
    },
  ];
 
  const tooltipProps = {
    message: isOFACMatch ? UPDAT_DENY_BY_OFAC : EDIT_DENY_MESSAGE,
    show: showAccessDeniedTooltip || isOFACMatch,
  };
 
  return (
    <GiveBaseModal
      open={open}
      onClose={() => handleClose(data)}
      title={
        (
          <Stack direction="row" alignItems="center" gap={1}>
            <FileTextIcon size={22} />
            <GiveText fontWeight="400" fontSize="16px">
              {`${isEdit ? "Edit" : "Add"} Business Profile (Legal Entity)`}
            </GiveText>
          </Stack>
        ) as any
      }
      height={isMobileView ? "auto" : undefined}
      buttons={
        <>
          <GiveButton
            label="Cancel"
            size="large"
            variant="ghost"
            disabled={isLoading}
            onClick={() => handleClose(data)}
            sx={{
              width: "auto !important",
            }}
            data-testid="business-profile-cancel-btn"
          />
          <GiveButton
            label={isEdit ? "Save" : "Add"}
            size="large"
            variant="filled"
            type="submit"
            form="edit-profile-form"
            sx={{
              width: "auto !important",
            }}
            data-testid="business-profile-submit-btn"
            disabled={isLoading}
          />
        </>
      }
    >
      <FormProvider {...methods}>
        <Box
          component="form"
          id="edit-profile-form"
          onSubmit={methods.handleSubmit(onSubmit)}
        >
          <Grid container spacing={3}>
            {inputList.map(
              ({ input, sm, md, lg, hidden, isInput, inputSx = {} }, index) => (
                <Grid
                  key={index}
                  item
                  xs={12}
                  sm={sm || 12}
                  md={md || sm || 12}
                  lg={lg || md || sm || 12}
                  sx={{ ...(hidden && { display: "none" }), ...inputSx }}
                >
                  <GiveCustomTooltip
                    showToolTip={!!isInput && tooltipProps?.show}
                    {...tooltipProps}
                  >
                    <GiveMotionWrapper delay={70 * (index + 1)}>
                      {input}
                    </GiveMotionWrapper>
                  </GiveCustomTooltip>
                </Grid>
              ),
            )}
          </Grid>
          {values.isLinkBusinessProfile && (
            <GiveBusinessProfileItem selectedProfile={parsedSelectedProfile} />
          )}
        </Box>
      </FormProvider>
    </GiveBaseModal>
  );
}
 
export default GiveEditBusinessProfileModal;