diff --git a/src/utils/index.ts b/src/utils/index.ts index 941eeda1a..f53f01019 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -164,6 +164,21 @@ export async function updateResourceInHearth(resource: fhir.ResourceBase) { return res.text() } +export const convertToMSISDN = (phone: string, countryAlpha3: string) => { + const countryCode = callingCountries[countryAlpha3.toUpperCase()].alpha2 + + const phoneUtil = PhoneNumberUtil.getInstance() + const number = phoneUtil.parse(phone, countryCode) + + return ( + phoneUtil + .format(number, PhoneNumberFormat.INTERNATIONAL) + // libphonenumber adds spaces and dashes to phone numbers, + // which we do not want to keep for now + .replace(/[\s-]/g, '') + ) +} + const csvStringify = promisify>, Options>(stringify) export async function writeJSONToCSV( filename: string, diff --git a/typings/country-data.d.ts b/typings/country-data.d.ts new file mode 100644 index 000000000..7f2dd2e33 --- /dev/null +++ b/typings/country-data.d.ts @@ -0,0 +1,11 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * OpenCRVS is also distributed under the terms of the Civil Registration + * & Healthcare Disclaimer located at http://opencrvs.org/license. + * + * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. + */ +declare module 'country-data'