All files / src/features/Merchants/MerchantSidePanel/hooks useSnapShot.tsx

97.87% Statements 46/47
69.86% Branches 51/73
92.3% Functions 12/13
97.87% Lines 46/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 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                                                                                  58x 58x 58x 58x 58x   58x         58x 58x 58x 58x 58x 58x   58x                 37x 58x 58x                           37x 58x 58x 25x 25x 25x                   25x     58x   25x         25x                                                               37x                                                       58x   58x   58x                                                                                                                             1044x     37x 174x     37x                     58x 58x   58x                                                         37x                 58x 58x 58x   58x                                               37x 58x                                                                                                             37x 174x      
import { useMerchantSidePanelContext } from "../Provider/MerchantSidePanelProvider";
import {
  IParsedData,
  TBusinessOwner,
  TFeeObject,
  TMerchantBaseContextData,
} from "@components/Merchants/MerchantPreview/data.types";
import {
  betterPhoneNumber,
  formatSSN,
  parseTaxID,
} from "../GiveMerchantFile/hooks/helpers";
import { getServicePhoneNumber } from "@utils/helpers";
import { capitalizeFirstLetter, parseAmount } from "@utils/index";
import { normalizeAddress, formatAddressDisplay } from "@utils/address";
import { getCountryNameFromCode } from "@utils/country_dial_codes";
import { formatInUTCMoment, isEmptyPhone } from "@utils/date.helpers";
import {
  CHARGEBACK_FEE,
  CURRENCY,
  DESCRIPTOR_PREFIX,
  AUTOMATED_CHARGEBACK_DISPUTE_FEE,
  CHARGEBACK_REFUND_FEE,
} from "@constants/constants";
import { BUSINESS_PURPOSE_FIELD } from "@constants/constants";
import { renderBODeps } from "@components/ProfilePage/BusinessProfileSetupNew/utils/principal.utils";
import { findBankByRoutingNumber } from "@utils/bank_list";
import { computeAgeOfBusiness } from "@components/Merchants/MerchantPreview/helpers/parsers";
import { BUSINESS_CUNDUCTED_OUTSIDE_USA } from "@constants/stringConstants";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { checkPortals } from "@utils/routing";
import useCheckEnvironment from "@hooks/common/useCheckEnvironment";
import { processorMap } from "@features/Merchants/MerchantSidePanel/constants";
import { calculateFeePerTransaction } from "@features/MerchantPortal/ManageMoney/modals/SendMoneyModal/helpers";
 
export default function useSnapShot({
  defaultContext,
}: {
  defaultContext?: TMerchantBaseContextData;
} = {}) {
  const { isEnterprisePortal, isEnterpriseTable, isMerchantPortal } =
    checkPortals();
  const { domain } = useCheckEnvironment();
  const context = useMerchantSidePanelContext();
  const { data } = defaultContext ?? context;
  const { isMerchantProcessorEnabled } = useGetFeatureFlagValues();
  const hiddenProcessor =
    !isMerchantProcessorEnabled ||
    isEnterprisePortal ||
    isEnterpriseTable ||
    isMerchantPortal;
 
  const merchantInfo = getMerchantInfo(data, { hiddenProcessor, domain });
  const businessProfile = getBusinessProfile(data);
  const primaryAccountHolder = getPrimaryAccountHolder(data);
  const businessOwners = getBusinessOwners(data);
  const bankAccounts = getBankAccounts(data);
  const fees = getFees(data);
 
  return {
    ...fees,
    merchantInfo,
    businessProfile,
    primaryAccountHolder,
    businessOwners,
    bankAccounts,
  };
}
const getBankAccounts = (data: IParsedData) => {
  const bankAccountList = data?.bankAccountList || [];
  return bankAccountList?.map((bank) => {
    return [
      {
        label: "Bank Name",
        value: bank?.bankName || findBankByRoutingNumber(bank?.routingNumber),
      },
      { label: "Account Type", value: bank?.type },
      { label: "Business Name on Account", value: bank?.name },
      { label: "Account Number", value: bank?.accountNumber },
      { label: "Routing Number", value: bank?.routingNumber },
    ];
  });
};
 
const getBusinessOwners = (data: IParsedData) => {
  const businessOwnersList = data?.businessOwnersList || [];
  const getDocType = (owner: TBusinessOwner) => {
    const { shouldUseSSN } = renderBODeps(owner);
    const isSSN = owner?.ssn;
    const docType = shouldUseSSN
      ? {
          label: isSSN ? "SSN" : "EIN",
          value: formatSSN((isSSN ? owner?.ssn : owner?.ein) || ""),
        }
      : {
          label:
            owner.documentType === "passport_id" ? "Passport" : "National ID",
          value: owner.documentNumber as string,
        };
    return docType;
  };
 
  return businessOwnersList?.map((owner, idx) => {
    const ownership =
      owner.ownership && !isNaN(parseFloat(owner.ownership))
        ? parseFloat(owner?.ownership) % 1 === 0
          ? parseInt(owner?.ownership)
          : parseFloat(owner?.ownership).toFixed(2)
        : owner?.ownership;
    return [
      { label: "Full Name", value: `${owner?.firstName} ${owner?.lastName}` },
      { label: "Email", value: owner?.email || "" },
      { label: "Ownership Percentage", value: `${ownership ?? 0}%` },
      getDocType(owner),
      {
        label: "Date of Birth",
        value: formatInUTCMoment(owner?.dob),
      },
      {
        label: "Business Owner Phone",
        value: isEmptyPhone(owner?.phone)
          ? ""
          : betterPhoneNumber(owner?.phone),
        isPhoneNumber: true,
      },
      {
        label: "Country of Citizenship",
        value: owner?.citizenship
          ? `${getCountryNameFromCode(owner?.citizenship)}`
          : "United States ",
      },
      {
        label: "Country of Residence",
        value: owner?.countryOfResidence
          ? `${getCountryNameFromCode(owner?.countryOfResidence)}`
          : "United States",
        isLast: businessOwnersList?.length - 1 !== idx,
      },
    ];
  });
};
const getMerchantInfo = (
  data: IParsedData,
  { hiddenProcessor, domain }: { hiddenProcessor: boolean; domain: string },
) => {
  const {
    merchantName,
    category,
    categoryCodeName,
    billingDescriptor,
    billingDescriptorPrefix,
    websiteURL,
    estimatedAnnualRevenue,
    servicePhoneNumber,
    averageTicketAmount,
    highTicketAmount,
    serviceCountriesOutUSCanada,
    countriesServicedOutside,
    businessPurpose,
    description,
    merchantID,
    enterprise,
    classification,
    createdAt,
    signupTypeDisplayName,
    socialMediaURL,
    merchantSlug,
    parentSlug,
    processor,
  } = data?.merchantInfo || {};
 
  const prefix = billingDescriptorPrefix || DESCRIPTOR_PREFIX;
 
  return [
    { label: "Merchant Name", value: merchantName },
    { label: "Merchant ID", value: merchantID },
    { label: "Provider", value: enterprise },
    {
      label: "Classification",
      value:
        typeof classification === "string"
          ? classification
          : classification?.displayName,
    },
    { label: "Created", value: createdAt },
    { label: "Origin", value: signupTypeDisplayName },
    {
      label: "Processor",
      value: processor ? processorMap[processor] : "-",
      hidden: hiddenProcessor,
    },
    {
      label: "Merchant Category Code (MCC)",
      value: category ? `${category} - ${categoryCodeName}` : "-",
    },
    {
      label: `Billing Descriptor (${prefix})`,
      value: billingDescriptor,
    },
    {
      label: "Merchant Phone Number",
      value: getServicePhoneNumber(servicePhoneNumber),
      isPhoneNumber: true,
    },
    { label: "Business Website", value: websiteURL },
    { label: "Business Social Media", value: socialMediaURL },
    {
      label: "GivePayments URL",
      value: `https://${parentSlug}.${domain}givepayments.com/${merchantSlug}`,
    },
    {
      label: BUSINESS_CUNDUCTED_OUTSIDE_USA,
      value: serviceCountriesOutUSCanada ? countriesServicedOutside : "None",
      isDivider: true,
    },
    {
      label: "Estimated Annual Revenue",
      value: formatCurrencyUSD(+estimatedAnnualRevenue),
    },
    {
      label: "Average Ticket Amount",
      value: formatCurrencyUSD(averageTicketAmount ?? 0),
    },
    {
      label: "High Ticket Amount",
      value: formatCurrencyUSD(highTicketAmount),
      isDivider: true,
    },
    {
      label: BUSINESS_PURPOSE_FIELD,
      value: (
        <span style={{ wordBreak: "break-word" }}>
          {businessPurpose || description}
        </span>
      ),
    },
  ].filter((item) => !item.hidden);
};
 
const formatCurrencyUSD = (value: number | string) => {
  return `${parseAmount(value)} USD`;
};
 
const getBusinessProfile = (data: IParsedData) => {
  const {
    legalName,
    dba,
    businessType,
    ownershipType,
    taxID,
    businessOpenedAt,
    contactPhone,
    tinType,
    ageOfBusiness,
  } = data?.businessProfile || {};
  const { age } = computeAgeOfBusiness(businessOpenedAt || "");
 
  return [
    { label: "Legal Name", value: legalName },
    { label: "Business Type", value: capitalizeFirstLetter(businessType) },
    {
      label: "Federal Tax ID",
      value: tinType === "ssn" ? formatSSN(taxID) : parseTaxID(taxID),
    },
    {
      label: "Business Ownership Type",
      value: capitalizeFirstLetter(ownershipType),
    },
    { label: "Doing Business As (Optional)", value: dba },
    { label: "Business Creation Date", value: businessOpenedAt },
    {
      label: "Business Phone Number",
      value: getServicePhoneNumber(contactPhone),
      isPhoneNumber: true,
    },
    {
      label: "Address",
      value: formatAddressDisplay(normalizeAddress(data?.businessAddress)),
    },
    {
      label: "Age of Business",
      value: age || ageOfBusiness,
    },
  ];
};
 
const getPrimaryAccountHolder = (data: IParsedData) => {
  const {
    lastName,
    firstName,
    email,
    dateOfBirth,
    phoneNumber,
    citizenship,
    countryOfResidence,
  } = data?.primaryAccountHolder || {};
  const dob = formatInUTCMoment(dateOfBirth);
  const name = [firstName, lastName].filter(Boolean).join(" ") || "-";
 
  return [
    { label: "Full Name", value: name },
    { label: "Email", value: email },
    { label: "Date of Birth", value: dob },
    {
      label: "Mobile Phone",
      isPhoneNumber: true,
      value: getServicePhoneNumber(phoneNumber),
    },
    {
      label: "Country of Citizenship",
      value: citizenship
        ? `${getCountryNameFromCode(citizenship)}`
        : "United States ",
    },
    {
      label: "Country of Residence",
      value: countryOfResidence
        ? `${getCountryNameFromCode(countryOfResidence)}`
        : "United States",
    },
  ];
};
 
const getFees = (data: IParsedData) => {
  return {
    transactionFees: [
      {
        label: "Credit Card Transaction",
        value: formatFee(data?.fees?.creditCardFee),
      },
      {
        label: "Debit Card Transaction",
        value: formatFee(data?.fees?.debitCardFee),
      },
      {
        label: "AMEX Card Transaction",
        value: formatFee(data?.fees?.amexCreditCardFee),
      },
    ],
    transferFees: [
      {
        label: "Transfer",
        value: `From 0.99 ${CURRENCY} per transfer. Capped at $20 ${CURRENCY} per transfer.`,
      },
    ],
    monthlyAccountFees: [
      { label: "Monthly Account", value: `0.00 ${CURRENCY} per month` },
    ],
    gateAwayAccessFees: [
      { label: "Gateway Access Fees", value: `0.00 ${CURRENCY} per month` },
      {
        label: "Per Transaction Gateway",
        value: `0.00 ${CURRENCY} per transaction`,
      },
    ],
    chargeBackFees: [
      {
        label: "Chargeback Refund Fee",
        value: `${CHARGEBACK_REFUND_FEE.toFixed(2)} ${CURRENCY} per refund`,
      },
      {
        label: "Chargeback",
        value: `${CHARGEBACK_FEE.toFixed(2)} ${CURRENCY} per chargeback`,
      },
      {
        label: "Automated Chargeback Dispute",
        value: `${AUTOMATED_CHARGEBACK_DISPUTE_FEE.toFixed(
          2,
        )} ${CURRENCY} per dispute case`,
      },
    ],
    misleneousFees: [
      { label: "Setup", value: `0.00 ${CURRENCY}` },
      { label: "PCI Compliance", value: `0.00 ${CURRENCY} per year` },
      { label: "Early Termination", value: `0.00 ${CURRENCY}` },
    ],
  };
};
 
const formatFee = (fee: TFeeObject) =>
  fee ? `${calculateFeePerTransaction(fee)} / fee per transaction` : "N/A";
 
//calculateFeePerTransaction