diff --git a/packages/client/src/forms/register/mappings/query/registration-mappings.ts b/packages/client/src/forms/register/mappings/query/registration-mappings.ts index c17758fbaa..d2a4e12de7 100644 --- a/packages/client/src/forms/register/mappings/query/registration-mappings.ts +++ b/packages/client/src/forms/register/mappings/query/registration-mappings.ts @@ -95,30 +95,32 @@ export const certificateDateTransformer = window.__localeId__ = prevLocale } -export const convertToLocal = ( - mobileWithCountryCode: string, - alpha3CountryCode: string -) => { +export const convertToLocal = (mobileWithCountryCode: string) => { /* * If country is the fictional demo country (Farajaland), use Zambian number format */ - - const countryCode = countryAlpha3toAlpha2(alpha3CountryCode) + const phoneUtil = PhoneNumberUtil.getInstance() + const number = phoneUtil.parse(mobileWithCountryCode) + const countryCode = phoneUtil.getRegionCodeForNumber(number) if (!countryCode) { return } - const phoneUtil = PhoneNumberUtil.getInstance() - if (!phoneUtil.isPossibleNumberString(mobileWithCountryCode, countryCode)) { return } - const number = phoneUtil.parse(mobileWithCountryCode, countryCode) + let nationalFormat = phoneUtil.format(number, PhoneNumberFormat.NATIONAL) + + // This is a special case for countries that have a national prefix of 0 + if ( + phoneUtil.getNddPrefixForRegion(countryCode, true) === '0' && + !nationalFormat.startsWith('0') + ) { + nationalFormat = '0' + nationalFormat + } - return phoneUtil - .format(number, PhoneNumberFormat.NATIONAL) - .replace(/[^A-Z0-9]+/gi, '') + return nationalFormat } export const localPhoneTransformer = @@ -132,7 +134,7 @@ export const localPhoneTransformer = const fieldName = transformedFieldName || field.name const msisdnPhone = get(queryData, fieldName as string) as unknown as string - const localPhone = convertToLocal(msisdnPhone, window.config.COUNTRY) + const localPhone = convertToLocal(msisdnPhone) transformedData[sectionId][field.name] = localPhone return transformedData