|
| 1 | +import {Str} from 'expensify-common'; |
| 2 | +import { |
| 3 | + isValidAddress, |
| 4 | + isValidCompanyName, |
| 5 | + isValidEmail, |
| 6 | + isValidPhoneInternational, |
| 7 | + isValidRegistrationNumber, |
| 8 | + isValidTaxIDEINNumber, |
| 9 | + isValidWebsite, |
| 10 | + isValidZipCodeInternational, |
| 11 | +} from '@libs/ValidationUtils'; |
1 | 12 | import CONST from '@src/CONST'; |
2 | 13 | import INPUT_IDS from '@src/types/form/ReimbursementAccountForm'; |
3 | 14 |
|
4 | 15 | const BUSINESS_INFO_STEP_KEYS = INPUT_IDS.ADDITIONAL_DATA.CORPAY; |
5 | 16 |
|
| 17 | +function isMissingValidCompanyName(companyName: string) { |
| 18 | + return companyName === '' || !isValidCompanyName(companyName); |
| 19 | +} |
| 20 | + |
| 21 | +function isMissingValidWebsite(website: string) { |
| 22 | + return website === '' || !isValidWebsite(Str.sanitizeURL(website, CONST.COMPANY_WEBSITE_DEFAULT_SCHEME)); |
| 23 | +} |
| 24 | + |
| 25 | +function isMissingValidAddress(street: string, city: string, postalCode: string, state: string, country: string) { |
| 26 | + return ( |
| 27 | + street === '' || |
| 28 | + city === '' || |
| 29 | + postalCode === '' || |
| 30 | + country === '' || |
| 31 | + ((country === CONST.COUNTRY.US || country === CONST.COUNTRY.CA) && state === '') || |
| 32 | + (country === '' && state === '') || |
| 33 | + !isValidAddress(street) || |
| 34 | + (country === CONST.COUNTRY.US && !isValidZipCodeInternational(postalCode)) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function isMissingValidBusinessContactInformation(contactNumber: string, contactEmail: string) { |
| 39 | + return contactNumber === '' || contactEmail === '' || !isValidPhoneInternational(contactNumber) || !isValidEmail(contactEmail); |
| 40 | +} |
| 41 | + |
| 42 | +function isMissingValidRegistrationNumber(registrationNumber: string, country: string) { |
| 43 | + return registrationNumber === '' || !isValidRegistrationNumber(registrationNumber, country as keyof typeof CONST.COUNTRY); |
| 44 | +} |
| 45 | + |
| 46 | +function isMissingValidTaxIDEINNumber(taxIDEINNumber: string, country: string) { |
| 47 | + return taxIDEINNumber === '' || !isValidTaxIDEINNumber(taxIDEINNumber, country as keyof typeof CONST.COUNTRY); |
| 48 | +} |
| 49 | + |
6 | 50 | /** |
7 | 51 | * Returns the initial subStep for the Business info step based on already existing data |
8 | 52 | */ |
9 | 53 | function getInitialSubStepForBusinessInfoStep(data: Record<string, string>): number { |
10 | | - if (data[BUSINESS_INFO_STEP_KEYS.COMPANY_NAME] === '') { |
| 54 | + if (isMissingValidCompanyName(data[BUSINESS_INFO_STEP_KEYS.COMPANY_NAME])) { |
11 | 55 | return 0; |
12 | 56 | } |
13 | 57 |
|
14 | | - if (data[BUSINESS_INFO_STEP_KEYS.COMPANY_WEBSITE] === '') { |
| 58 | + if (isMissingValidWebsite(data[BUSINESS_INFO_STEP_KEYS.COMPANY_WEBSITE])) { |
15 | 59 | return 1; |
16 | 60 | } |
17 | 61 |
|
18 | 62 | if ( |
19 | | - data[BUSINESS_INFO_STEP_KEYS.COMPANY_STREET] === '' || |
20 | | - data[BUSINESS_INFO_STEP_KEYS.COMPANY_CITY] === '' || |
21 | | - data[BUSINESS_INFO_STEP_KEYS.COMPANY_POSTAL_CODE] === '' || |
22 | | - data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE] === '' || |
23 | | - ((data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE] === CONST.COUNTRY.US || data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE] === CONST.COUNTRY.CA) && |
24 | | - data[BUSINESS_INFO_STEP_KEYS.COMPANY_STATE] === '') || |
25 | | - (data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE] === '' && data[BUSINESS_INFO_STEP_KEYS.COMPANY_STATE] === '') |
| 63 | + isMissingValidAddress( |
| 64 | + data[BUSINESS_INFO_STEP_KEYS.COMPANY_STREET], |
| 65 | + data[BUSINESS_INFO_STEP_KEYS.COMPANY_CITY], |
| 66 | + data[BUSINESS_INFO_STEP_KEYS.COMPANY_POSTAL_CODE], |
| 67 | + data[BUSINESS_INFO_STEP_KEYS.COMPANY_STATE], |
| 68 | + data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE], |
| 69 | + ) |
26 | 70 | ) { |
27 | 71 | return 2; |
28 | 72 | } |
29 | 73 |
|
30 | | - if (data[BUSINESS_INFO_STEP_KEYS.BUSINESS_CONTACT_NUMBER] === '' || data[BUSINESS_INFO_STEP_KEYS.BUSINESS_CONFIRMATION_EMAIL] === '') { |
| 74 | + if (isMissingValidBusinessContactInformation(data[BUSINESS_INFO_STEP_KEYS.BUSINESS_CONTACT_NUMBER], data[BUSINESS_INFO_STEP_KEYS.BUSINESS_CONFIRMATION_EMAIL])) { |
31 | 75 | return 3; |
32 | 76 | } |
33 | 77 |
|
34 | | - if (data[BUSINESS_INFO_STEP_KEYS.BUSINESS_REGISTRATION_INCORPORATION_NUMBER] === '') { |
| 78 | + if (isMissingValidRegistrationNumber(data[BUSINESS_INFO_STEP_KEYS.BUSINESS_REGISTRATION_INCORPORATION_NUMBER], data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE])) { |
35 | 79 | return 4; |
36 | 80 | } |
37 | 81 |
|
38 | | - if (data[BUSINESS_INFO_STEP_KEYS.TAX_ID_EIN_NUMBER] === '') { |
| 82 | + if (isMissingValidTaxIDEINNumber(data[BUSINESS_INFO_STEP_KEYS.TAX_ID_EIN_NUMBER], data[BUSINESS_INFO_STEP_KEYS.COMPANY_COUNTRY_CODE])) { |
39 | 83 | return 5; |
40 | 84 | } |
41 | 85 |
|
|
0 commit comments