All files / src/components/ProfilePage/BusinessProfileSetup/helpers generatePayload.ts

54.54% Statements 18/33
46.15% Branches 12/26
38.46% Functions 5/13
60.71% Lines 17/28

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            19x         2x   2x 13x 13x 12x             19x                   19x             1x     1x                                                                                                                   19x 12x 12x   12x 12x 12x           12x        
import { FormSectionName } from "@components/ProfilePage/BusinessProfileSetupNew/types";
import { toISODateString, convertPhoneNumber } from "@utils/date.helpers";
import { parseAmountValue } from "@components/Merchants/CreateMerchant/utils/functions";
import { encodeString } from "@utils/index";
import { isCountryUSA } from "@components/Settings/BusinessDetails/components/MerchantInfoSection/utils/utils";
 
export const generatePayload = (
  section: FormSectionName,
  values: any,
  dirtyFields?: Record<string, boolean | undefined>,
) => {
  Iif (section === "businessOwners") return values;
 
  return attributes[section].reduce((acc, v) => {
    const [name] = v;
    if (dirtyFields && !dirtyFields[name]) return acc;
    return {
      ...acc,
      ...addAttribute([values, ...v]),
    };
  }, {});
};
 
const serviceOutsideParser = (data: boolean, extraData?: string) => {
  const isUS = isCountryUSA(extraData);
  if (!isUS && data) return true;
  if (isUS) return undefined;
  else return false;
};
 
const attributes: Record<
  FormSectionName,
  [name: string, options?: TOptions][]
> = {
  businessDetails: [
    ["businessType", { apiKey: "type" }],
    ["legalName", { apiKey: "name" }],
    ["DBA", { apiKey: "doingBusinessAs" }],
    ["ssn"],
    ["taxIDNumber"],
    ["phoneNumber", { parser: (v: string) => v?.slice(1) }],
    [
      "businessOpenedAt",
      { parser: (v: string) => toISODateString(v) },
    ],
    ["ownershipType"],
  ],
  businessAddress: [
    ["address", { apiKey: "line1" }],
    ["city"],
    ["state"],
    ["zip"],
    ["country"],
  ],
  businessOwners: [],
  merchantInfo: [
    ["websiteURL", { parser: (v: string) => (v ? "https://" + v : null) }],
    ["servicePhoneNumber", { parser: convertPhoneNumber }],
    [
      "annualCreditCardSalesVolume",
      { parser: (v: string) => parseAmountValue(v) * 100 },
    ],
    [
      "averageTicketAmount",
      { parser: (v: string) => parseAmountValue(v) * 100 },
    ],
    ["highTicketAmount", { parser: (v: string) => parseAmountValue(v) * 100 }],
    ["serviceCountriesOutUSCanada"],
    ["countriesServicedOutside"],
    ["name"],
    ["slug"],
    ["billingDescriptor", { parser: (v: string) => v.toUpperCase() }],
  ],
  enterpriseInfo: [
    ["name"],
    ["permalink", { apiKey: "slug" }],
    [
      "purpose",
      { parser: (v: string) => encodeString(v), apiKey: "description" },
    ],
    ["websiteURL", { parser: (v: string) => (v ? "https://" + v : null) }],
    ["servicePhoneNumber", { parser: convertPhoneNumber }],
    [
      "serviceCountriesOutUSCanada",
      {
        parser: serviceOutsideParser,
        extraFieldKey: "countriesServicedOutside",
      },
    ],
    ["countriesServicedOutside"],
  ],
};
 
type TOptions = {
  apiKey?: string;
  parser?: (value: any, extradata?: any) => any;
  extraFieldKey?: string;
};
 
type TArgs = [data: any, name: string, options?: TOptions];
 
const addAttribute = (args: TArgs) => {
  const [data, name, options] = args;
  Iif (!data?.[name] && typeof data?.[name] !== "boolean") return undefined;
 
  const attribute = options?.apiKey || name;
  const extraFieldKey = options?.extraFieldKey;
  const value = options?.parser
    ? options.parser(
        data[name],
        extraFieldKey ? data[extraFieldKey] : undefined,
      )
    : data[name];
  return {
    [attribute]: value,
  };
};