All files / src/components/ProfilePage/BusinessProfileSetupNew/helpers refineData.ts

91.48% Statements 43/47
61.9% Branches 39/63
92.85% Functions 13/14
91.48% Lines 43/47

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                      20x 34x 34x   34x 34x 34x 34x         34x 34x   34x                 34x         20x 34x                             20x 34x                                                 20x 34x   34x         34x         34x     20x 218x         33x 33x     33x     33x         33x       33x                                                                                         218x     20x   34x 34x                 20x 34x 34x                                     34x     20x                         20x 68x 68x     20x     102x 102x                                                                
import { refineBusinessOwnershipInOnboarding } from "@utils/validation.helpers";
import {
  businessOwnerParser,
  merchantClassificationParser,
} from "@components/Settings/BusinessDetails/components/MerchantInfoSection/utils/merchantInfoParser";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { getServicePhoneNumber } from "@utils/helpers";
import { formatInUTCMoment } from "@utils/date.helpers";
import { normalizeAddress } from "@utils/address";
import { getIDFile } from "@features/Merchants/MerchantSidePanel/GiveMerchantFile/components/BusinessOwnersSection";
 
export const useRefineData = () => {
  const { parsePrincipals } = useParsePrincipals();
  const { parseBusinessDetails } = useParseBusinessDetails();
 
  const refineData = (data: any) => {
    const businessDetails = parseBusinessDetails(data?.legalEntity);
    const businessAddress = parseBusinessAddress(data?.legalEntity?.address);
    const businessOwners = parsePrincipals(
      data?.legalEntity?.principals,
      data?.documents,
      data?.merchant?.address,
    );
    const merchantInfo = parseMerchantInfo(data?.merchant);
    const enterpriseInfo = parseEnterpriseInfo(data?.merchant);
 
    return {
      businessDetails,
      businessAddress,
      businessOwners,
      merchantInfo,
      enterpriseInfo,
    };
  };
 
  return { refineData };
};
 
export type DynamicReturnType = any;
 
const parseEnterpriseInfo = (data: any) => {
  return {
    name: data?.name,
    servicePhoneNumber: (data?.servicePhoneNumber
      ? getServicePhoneNumber(data.servicePhoneNumber)
      : "") as string,
    purpose: data?.description,
    websiteURL: stripHttps(data?.websiteURL),
    serviceCountriesOutUSCanada: data?.serviceCountriesOutUSCanada,
    countriesServicedOutside: data?.countriesServicedOutside,
    permalink: data?.slug,
    class: merchantClassificationParser(data),
    primaryAccountHolder: businessOwnerParser(data?.owner), //PAH data for default value on BO create when is sole proprietor
  };
};
 
const parseMerchantInfo = (data: any) => {
  return {
    averageTicketAmount: formatToStringValue(data?.averageTicketAmount),
    servicePhoneNumber: (data?.servicePhoneNumber
      ? getServicePhoneNumber(data.servicePhoneNumber)
      : "") as string,
    annualCreditCardSalesVolume: formatToStringValue(
      data?.annualCreditCardSalesVolume,
    ),
    highTicketAmount: formatToStringValue(data?.highTicketAmount),
    description: data?.description,
    websiteURL: stripHttps(data?.websiteURL),
    serviceCountriesOutUSCanada: data?.serviceCountriesOutUSCanada,
    countriesServicedOutside: data?.countriesServicedOutside,
    parentSlug: data?.parentSlug,
    slug: data?.slug,
    name: data?.name,
    class: merchantClassificationParser(data),
    imageURL: data?.imageURL,
    billingDescriptor: data?.billingDescriptor
      ? data?.billingDescriptor
      : data?.name,
    billingDescriptorPrefix: data?.billingDescriptorPrefix,
  };
};
 
export const useParsePrincipals = () => {
  const { parseBO } = useParseBO();
 
  const parsePrincipals = (
    data: any,
    documents: { data: TMerchantDocument[] },
    address?: any,
  ): OnboardingPrincipal[] => {
    return data?.map((principal: any) =>
      parseBO(principal, documents, address),
    );
  };
 
  return { parsePrincipals };
};
 
export const useParseBO = () => {
  const parseBO = (
    principal: any,
    documents?: { data: TMerchantDocument[] },
    address?: any,
  ) => {
    const useBusinessAddress = principal?.useEntityGeoAddress;
    const ssn: string = principal?.taxIDNumberLast4
      ? `XXXXX${principal?.taxIDNumberLast4}`
      : "";
    const docIDNumber: string = principal?.docIDNumberLast4
      ? `XXXXX${principal?.docIDNumberLast4}`
      : "";
    const file = documents?.data
      ? getIDFile(principal?.id, documents.data)
      : null;
 
    // Normalize address - use business address if specified, otherwise principal's address
    const normalizedAddress = normalizeAddress(
      useBusinessAddress ? address : principal,
    );
 
    return {
      documentNumber: docIDNumber,
      documentType: (principal?.docIDNumberType || "") as string,
      firstName: (principal?.firstName || "") as string,
      lastName: (principal?.lastName || "") as string,
      isUSResident: !principal?.citizenship,
      citizenship: principal?.citizenship || "",
      isNotResidentInCitizenshipCountry: !!principal?.countryOfResidence,
      countryOfResidence: principal?.countryOfResidence || "",
      email: (principal?.email || "") as string,
      ssn,
      dob: formatInUTCMoment(principal?.dateOfBirth),
      ownership: (principal?.ownershipPercentage ?? "") as string,
      // Flat address fields for backward compatibility
      country: normalizedAddress.country,
      city: normalizedAddress.city,
      street: normalizedAddress.line1,
      state: normalizedAddress.state,
      zip: normalizedAddress.zip,
      id: (principal?.id || "") as string,
      pepStatusName: (principal?.pepStatusName || "") as string,
 
      phone: (principal?.phoneNumber
        ? getServicePhoneNumber(principal?.phoneNumber)
        : "") as string,
      useBusinessAddress,
      files: {
        allFiles: file ? [file] : [],
      },
      businessAddress: {
        country: normalizedAddress.country,
        street: normalizedAddress.line1,
        city: normalizedAddress.city,
        province: normalizedAddress.state,
        zip: normalizedAddress.zip,
        state: normalizedAddress.state,
      },
      ofac: {
        lastCheckAt: principal.lastCheckAt,
        lastCheckStatusName: principal.lastCheckStatusName,
      },
      idImageUrl: principal.idImageURL,
    };
  };
 
  return { parseBO };
};
 
const parseBusinessAddress = (data: any) => {
  // Use normalizeAddress then serialize to TBusinessAddressInfo format
  const normalizedAddress = normalizeAddress(data);
  return {
    country: normalizedAddress.country,
    address: normalizedAddress.line1, // TBusinessAddressInfo uses "address" field for street
    city: normalizedAddress.city,
    state: normalizedAddress.state,
    zip: normalizedAddress.zip,
  };
};
 
const useParseBusinessDetails = () => {
  const parseBusinessDetails = (data: any): Record<string, string> => {
    return {
      legalName: data?.name || "",
      DBA: data?.doingBusinessAs || "",
      tinType: data?.tinType || "ein",
      ssn: data?.tinType === "ssn" ? data?.taxIDNumber || "" : "",
      taxIDNumber: data?.tinType === "ein" ? data?.taxIDNumber || "" : "",
      businessOpenedAt: formatInUTCMoment(data?.businessOpenedAt),
      businessType: data?.type?.name || "corporation",
      ownershipType: refineBusinessOwnershipInOnboarding(
        data?.type?.name || "corporation",
        data?.ownershipType?.toLowerCase(),
      ),
      phoneNumber: data?.phoneNumber
        ? getServicePhoneNumber(data?.phoneNumber)
        : "",
      id: data?.id || "",
    };
  };
 
  return { parseBusinessDetails };
};
 
export const businessDetailsDefaultValues = {
  legalName: "",
  DBA: "",
  tinType: "ein",
  ssn: "",
  taxIDNumber: "",
  businessOpenedAt: new Date().toISOString(),
  businessType: "corporation",
  ownershipType: refineBusinessOwnershipInOnboarding("corporation", ""),
  phoneNumber: "",
  id: "",
};
 
const stripHttps = (url = ""): string => {
  const httpsPrefix = "https://";
  return url.replace(httpsPrefix, "");
};
 
export const formatToStringValue = (
  value: number | string | null | undefined,
) => {
  if (typeof value === "number") {
    return value / 100 + ".00";
  } else Eif (typeof value === "string") {
    return `${value.slice(0, -2)}.${value.slice(-2)}`;
  } else {
    ("0.00");
  }
};
 
export interface OnboardingPrincipal {
  businessAddress: any;
 
  address: {
    country: string;
    city: string;
    street: string;
    state: string;
    zip: string;
  };
  firstName: string;
  lastName: string;
  email: string;
  ssn: string;
  dob: string;
  ownership: string;
  id: string;
  pepStatusName: string;
  useBusinessAddress: boolean;
  isUSResident: boolean;
  citizenship: string;
  isNotResidentInCitizenshipCountry: boolean;
  countryOfResidence: string;
}