Skip to content

Commit

Permalink
Fix issue with formatting national phone numbers for countries with a…
Browse files Browse the repository at this point in the history
… '0' national prefix
  • Loading branch information
tareq89 committed Jan 10, 2025
1 parent 0c24f14 commit dc2cf70
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down

0 comments on commit dc2cf70

Please sign in to comment.