Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with formatting national phone numbers for countries with a '0' national prefix #8318

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
- Fix the informant column on the Perfomance page showing "Other family member" when `Someone else` is selected for a registration [#6157](https://github.com/opencrvs/opencrvs-core/issues/6157)
- Fix the event name displayed in email templates for death correction requests [#7703](https://github.com/opencrvs/opencrvs-core/issues/7703)

## 1.6.3 Release candidate

### Improvements

- Refactored convertToLocal to handle optional country code, fallback region detection, and special handling for national prefixes (e.g., 0), with alphanumeric sanitization of phone numbers.

## 1.6.2 Release candidate

### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,40 @@ export const certificateDateTransformer =

export const convertToLocal = (
mobileWithCountryCode: string,
alpha3CountryCode: string
alpha3CountryCode?: string
) => {
/*
* If country is the fictional demo country (Farajaland), use Zambian number format
*/

const countryCode = countryAlpha3toAlpha2(alpha3CountryCode)
if (!mobileWithCountryCode) {
return
}
const phoneUtil = PhoneNumberUtil.getInstance()
const number = phoneUtil.parse(mobileWithCountryCode)
const countryCode = alpha3CountryCode
? countryAlpha3toAlpha2(alpha3CountryCode)
: phoneUtil.getRegionCodeForNumber(number)

if (!countryCode) {
return
}

const phoneUtil = PhoneNumberUtil.getInstance()

if (!phoneUtil.isPossibleNumberString(mobileWithCountryCode, countryCode)) {
return
}
const number = phoneUtil.parse(mobileWithCountryCode, countryCode)

return phoneUtil
let nationalFormat = phoneUtil
.format(number, PhoneNumberFormat.NATIONAL)
.replace(/[^A-Z0-9]+/gi, '')

// 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 nationalFormat
}

export const localPhoneTransformer =
Expand All @@ -131,7 +143,6 @@ 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)

transformedData[sectionId][field.name] = localPhone
Expand Down
Loading