All files / src/validation/merchant merchantInfo.ts

84.44% Statements 38/45
78.37% Branches 29/37
82.35% Functions 14/17
90.24% Lines 37/41

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                                        70x     381x 381x 258x   236x       118x   118x 1x 1x 1x 1x     118x                 70x       381x                                                     70x             381x 292x                     70x               269x                                                     70x 112x   112x             112x         70x                 112x                 23x         112x 89x     184x   78x         89x 29x       112x           70x         269x                   70x         112x                  
// ======================================================
// Imports
// ======================================================
import * as Yup from "yup";
import { AsYouType } from "libphonenumber-js";
import { matchIsValidTel } from "mui-tel-input";
import {
  BILLING_DESCRIPTOR_MAX_LENGTH,
  DESCRIPTOR_PREFIX,
  MERCHANT_PROVIDER_MAX_CHARS,
} from "@constants/constants";
import { ENTER_COUNTRIES_OUTSIDE_USA } from "@constants/stringConstants";
import { createUrlValidator } from "@validation/fields";
import { VALIDATION_MESSAGES } from "@validation/messages";
import { DBA_LENGTH_REGEX } from "@validation/regex";
 
type PhoneValidationOptions = {
  allowPlusOneShortcut?: boolean; // if true, allows "+1" as valid
};
 
const phoneValidator = ({
  allowPlusOneShortcut = false,
}: PhoneValidationOptions) => {
  const asYouType = new AsYouType();
  return Yup.string().when({
    is: (exists: string) => !!exists,
    then: (schema) =>
      schema.test(
        "is-valid-number",
        VALIDATION_MESSAGES.INVALID_PHONE,
        function (value) {
          Iif (!value) return true;
 
          if (allowPlusOneShortcut) {
            Iif (value === "+1") return true;
            asYouType.input(value);
            const callingCode = `+${asYouType.getCallingCode()}`;
            Iif (callingCode === value) return true;
          }
 
          return matchIsValidTel(value);
        },
      ),
  });
};
 
// ======================================================
// Base Shared Schema (common for create + edit)
// ======================================================
const getBaseMerchantSchema = ({
  isCreate,
}: {
  isCreate: boolean;
}): Record<string, Yup.AnySchema> => ({
  merchantName: Yup.string()
    .required(VALIDATION_MESSAGES.NAME_REQUIRED)
    .matches(
      /^(?=(.*[a-zA-Z]){3}).*$/,
      VALIDATION_MESSAGES.INVALID_NAME_MIN_LENGTH,
    )
    .max(
      MERCHANT_PROVIDER_MAX_CHARS,
      `Merchant Name can not contain more than ${MERCHANT_PROVIDER_MAX_CHARS} characters`,
    )
    .trim(),
 
  merchantSlug: Yup.string(),
 
  servicePhoneNumber: phoneValidator({ allowPlusOneShortcut: isCreate }),
 
  websiteURL: createUrlValidator({ required: false }),
  socialMediaURL: createUrlValidator({ required: false }),
 
  description: isCreate ? Yup.string() : Yup.string().trim(),
  classification: isCreate ? Yup.string() : Yup.string().nullable(),
});
 
// ======================================================
// Shared conditional fields for acquirer/provider logic
// ======================================================
const getAcquirerFields = ({
  isProvider = false,
  isCreate = false,
}: {
  isProvider?: boolean;
  isCreate?: boolean;
}): Record<string, Yup.AnySchema> => {
  if (!isProvider && !isCreate) return {};
  return {
    averageTicketAmount: Yup.string(),
    highTicketAmount: Yup.string(),
    estimatedAnnualRevenue: Yup.string(),
    category: isCreate ? Yup.number() : Yup.string().nullable(),
  };
};
 
// ======================================================
// CREATE-SPECIFIC FIELDS
// ======================================================
const getCreateSpecificFields = ({
  isAcquirerPortal,
  isProvider,
  isMerchantProcessorEnabled,
}: {
  isAcquirerPortal?: boolean;
  isProvider?: boolean;
  isMerchantProcessorEnabled?: boolean;
}): Record<string, Yup.AnySchema> => ({
  businessPurpose: Yup.string(),
  serviceCountriesOutUSCanada: Yup.boolean(),
  countriesServicedOutside: Yup.string().when("serviceCountriesOutUSCanada", {
    is: true,
    then: Yup.string().required(VALIDATION_MESSAGES.SERVICED_COUNTRY_REQUIRED),
  }),
  billingDescriptor: Yup.string(),
  classificationDescription: Yup.string(),
  enterpriseID:
    isAcquirerPortal && !isProvider
      ? Yup.number()
          .typeError(VALIDATION_MESSAGES.SPECIFY_PROVIDER)
          .required(VALIDATION_MESSAGES.SPECIFY_PROVIDER)
      : Yup.number().nullable(),
  ...(isMerchantProcessorEnabled &&
    isAcquirerPortal &&
    !isProvider && {
      processor: Yup.string()
        .typeError(VALIDATION_MESSAGES.SELECT_PROCESSOR)
        .required(VALIDATION_MESSAGES.SELECT_PROCESSOR),
    }),
});
 
// ======================================================
// EDIT-SPECIFIC FIELDS and Billing descriptor logic
// ======================================================
const getBillingDescriptorSchema = (required?: boolean) => {
  const ERROR_TEXT = `Descriptor must match this format '${DESCRIPTOR_PREFIX}*BILLINGDESCRIPTOR' and must be between 3 to ${BILLING_DESCRIPTOR_MAX_LENGTH} characters length`;
 
  Iif (!required)
    return Yup.string().when({
      is: (value: string) => value && value.length > 0,
      then: (schema) => schema.matches(DBA_LENGTH_REGEX, ERROR_TEXT),
      otherwise: (schema) => schema,
    });
 
  return Yup.string()
    .required("Billing descriptor is required")
    .matches(DBA_LENGTH_REGEX, ERROR_TEXT);
};
 
const getEditSpecificFields = ({
  isAcquirerPortal,
  isProvider,
  isMerchantProcessorEnabled,
}: {
  isAcquirerPortal: boolean;
  isProvider?: boolean;
  isMerchantProcessorEnabled?: boolean;
}) => {
  const fields: Record<string, Yup.AnySchema> = {
    socialMediaURL: createUrlValidator({ required: false }),
    merchantRiskStatus: Yup.string(),
    categoryCodeID: Yup.number(),
    isOutsideUSA: Yup.boolean(),
    countriesOutside: Yup.string()
      .nullable()
      .when("isOutsideUSA", {
        is: true,
        then: () => Yup.string().trim().required(ENTER_COUNTRIES_OUTSIDE_USA),
      }),
    billingDescriptor: getBillingDescriptorSchema(true),
  };
 
  if (!isProvider) {
    fields.enterprise = Yup.number()
      .nullable()
      .when("category", {
        is: () => isAcquirerPortal,
        then: () =>
          Yup.number()
            .typeError(VALIDATION_MESSAGES.SPECIFY_PROVIDER)
            .required(VALIDATION_MESSAGES.SPECIFY_PROVIDER),
      });
 
    if (isMerchantProcessorEnabled) {
      fields.processorName = Yup.string().required("Please select a processor");
    }
  }
 
  return fields;
};
 
// ======================================================
// PUBLIC: CREATE / EDIT SCHEMAS
// ======================================================
export const getCreateMerchantInfoSchema = (
  isAcquirerPortal: boolean,
  isProvider?: boolean,
  isMerchantProcessorEnabled?: boolean,
) =>
  Yup.object().shape({
    ...getBaseMerchantSchema({ isCreate: true }),
    ...getAcquirerFields({ isProvider, isCreate: true }),
    ...getCreateSpecificFields({
      isAcquirerPortal,
      isProvider,
      isMerchantProcessorEnabled,
    }),
  });
 
export const getEditMerchantInfoSchema = (
  isAcquirerPortal: boolean,
  isProvider?: boolean,
  isMerchantProcessorEnabled?: boolean,
) =>
  Yup.object().shape({
    ...getBaseMerchantSchema({ isCreate: false }),
    ...getAcquirerFields({ isProvider, isCreate: false }),
    ...getEditSpecificFields({
      isAcquirerPortal,
      isProvider,
      isMerchantProcessorEnabled,
    }),
  });