All files / src/features/Merchants/MerchantSidePanel/GiveMerchantFile/hooks helpers.tsx

83.33% Statements 30/36
73.33% Branches 44/60
100% Functions 6/6
84.37% Lines 27/32

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                  77x 8x       77x 821x 649x       77x     5x 5x 5x         77x 37x   37x 37x 37x   37x       37x             37x     77x                 114x   114x             114x     114x         114x                                       77x           484x   484x   484x            
import {
  TBusinessOwner,
  TPrimaryAccountHolderAdress,
} from "@components/Merchants/MerchantPreview/data.types";
import { getCountryNameFromCode } from "@utils/country_dial_codes";
import { fromUnixTime } from "date-fns";
import { MERCHANT_BUSINESS_TYPES } from "../../constants";
import { normalizePhoneInputValue } from "@shared/HFInputs/HFGiveTelephone/phoneNumber.utils";
 
export const formatSSN = (value: string) => {
  Eif (!value) return "";
  return "•••• " + value.slice(-4);
};
 
export const parseTaxID = (id: string) => {
  if (!id) return id;
  Eif (id?.length > 2) return id.slice(0, 2) + "-" + id.slice(2);
  return id;
};
 
export const formatPrimaryAccountHolderAddress = (
  address: TPrimaryAccountHolderAdress,
) => {
  Iif (!address || !address.line1) return "";
  const { line1, city, state, country, zip } = address;
  return `${zip} ${line1}, ${city}, ${state}, ${getCountryNameFromCode(
    country,
  )}`;
};
 
export const betterPhoneNumber = (phoneNumber: string): string => {
  const startsWithPlus = phoneNumber.startsWith("+");
  // Check if the phoneNumber has a valid length
  const length = startsWithPlus ? 12 : 11;
  const isValidLength = phoneNumber.length === length;
  const normalizedNumber = normalizePhoneInputValue(phoneNumber) || "";
 
  Iif (!isValidLength) {
    return normalizedNumber;
  }
 
  const formatted = `${normalizedNumber.substring(
    0,
    1,
  )} (${normalizedNumber.substring(1, 4)}) ${normalizedNumber.substring(
    4,
    7,
  )}-${normalizedNumber.substring(7)}`;
  return formatted;
};
 
export const adjustAddressType = (owner: TBusinessOwner) => {
  const {
    id,
    dob,
    businessAddress,
    address,
    isBusinessAddress,
    ownership,
    phone,
  } = owner;
 
  const customAddress = {
    city: address?.city || businessAddress?.city,
    country: address?.country || businessAddress?.country,
    street: address?.line1 || businessAddress?.line1,
    zip: address?.zip || businessAddress?.zip,
    province: address?.state || businessAddress?.state,
  };
  const { city, country, street, zip, province } = customAddress;
 
  const dateOfBirth =
    typeof dob === "string"
      ? dob
      : typeof dob === "number"
      ? fromUnixTime(dob)
      : dob;
  return {
    ...owner,
    id: parseInt(id),
    dob: dob ? dateOfBirth : "",
    businessAddress: {
      city: city || "",
      country: country || "US",
      street: street || "",
      zip: zip || "",
      province: province || "",
    },
    useBusinessAddress: isBusinessAddress || false,
    ownership: ownership || "0",
    // The + is added during parsing the api response.
    // But that same parsed value is being used in old app.
    // That's why we can not remove from there, instead removing from here.
    phone: phone.includes("+") ? phone.slice(1) : phone,
  };
};
 
export const getOwnershipPercentageLabel = (
  businessType: string,
  ownership: string | number,
  prefixText = "",
  suffixText = "",
) => {
  Iif (businessType === MERCHANT_BUSINESS_TYPES.taxExemptOrganization)
    return "Nonprofit Principal";
  Iif (businessType === MERCHANT_BUSINESS_TYPES.governmentAgency)
    return "Representative";
  return ownership
    ? `${prefixText ? prefixText + " " : ""}${ownership}%${
        suffixText ? " " + suffixText : ""
      }`
    : "";
};