Import - Improve phone validation (#12901)
Context : - Phones import is a bit complex if not all subfields are provided. - Phones subfield validation are absent or different from BE validation. Solution : - Normalize callingCode and countryCode validation (BE/FE) - Ease phone import if only phoneNumber is provided
This commit is contained in:
@ -2,13 +2,12 @@ import { t } from '@lingui/core/macro';
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
import {
|
||||
CountryCallingCode,
|
||||
CountryCode,
|
||||
getCountries,
|
||||
getCountryCallingCode,
|
||||
parsePhoneNumberWithError,
|
||||
} from 'libphonenumber-js';
|
||||
import {
|
||||
getCountryCodesForCallingCode,
|
||||
isDefined,
|
||||
isValidCountryCode,
|
||||
parseJson,
|
||||
removeUndefinedFields,
|
||||
} from 'twenty-shared/utils';
|
||||
@ -22,8 +21,6 @@ import {
|
||||
PhonesMetadata,
|
||||
} from 'src/engine/metadata-modules/field-metadata/composite-types/phones.composite-type';
|
||||
|
||||
const ALL_COUNTRIES_CODE = getCountries();
|
||||
|
||||
export type PhonesFieldGraphQLInput =
|
||||
| Partial<
|
||||
Omit<PhonesMetadata, 'additionalPhones'> & {
|
||||
@ -38,22 +35,6 @@ type AdditionalPhoneMetadataWithNumber = Partial<AdditionalPhoneMetadata> &
|
||||
|
||||
const removePlusFromString = (str: string) => str.replace(/\+/g, '');
|
||||
|
||||
const isValidCountryCode = (input: string): input is CountryCode => {
|
||||
return ALL_COUNTRIES_CODE.includes(input as unknown as CountryCode);
|
||||
};
|
||||
|
||||
const getCountryCodesForCallingCode = (callingCode: string) => {
|
||||
const cleanCallingCode = callingCode.startsWith('+')
|
||||
? callingCode.slice(1)
|
||||
: callingCode;
|
||||
|
||||
return ALL_COUNTRIES_CODE.filter((country) => {
|
||||
const countryCallingCode = getCountryCallingCode(country);
|
||||
|
||||
return countryCallingCode === cleanCallingCode;
|
||||
});
|
||||
};
|
||||
|
||||
const validatePrimaryPhoneCountryCodeAndCallingCode = ({
|
||||
callingCode,
|
||||
countryCode,
|
||||
|
||||
Reference in New Issue
Block a user