From 98ba0340aad5c85e0942f9c071e3720b2e3b20de Mon Sep 17 00:00:00 2001 From: Embbnux Ji Date: Fri, 1 Nov 2024 16:21:15 +0800 Subject: [PATCH] fix: send number to server with e164 (#119) --- src/lib/checkCountryCode.ts | 28 ++++++++++++++++++++++++++++ src/lib/parseNumber.ts | 30 ++++++++++++++++++++++++++++++ src/modules/EvCall/index.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/lib/checkCountryCode.ts create mode 100644 src/lib/parseNumber.ts diff --git a/src/lib/checkCountryCode.ts b/src/lib/checkCountryCode.ts new file mode 100644 index 0000000..2d1f546 --- /dev/null +++ b/src/lib/checkCountryCode.ts @@ -0,0 +1,28 @@ +import countries from 'i18n-iso-countries'; + +import { + isE164, + parse, + parseIncompletePhoneNumber, +} from '@ringcentral-integration/phone-number'; + +import { messageTypes } from '@ringcentral-integration/engage-voice-widgets/enums'; +import { EvTypeError } from '@ringcentral-integration/engage-voice-widgets/lib/EvTypeError'; + +export const checkCountryCode = (input: string, availableCountries) => { + const cleanedNumber: string = parseIncompletePhoneNumber(input.toString()); + const isE164Number = isE164(cleanedNumber); + if (isE164Number) { + const { parsedNumber, isValid, hasInvalidChars, parsedCountry } = parse({ + input, + }); + if (isValid && !hasInvalidChars && parsedNumber) { + const dialoutCountryCode = countries.alpha2ToAlpha3(parsedCountry); + if (dialoutCountryCode !== 'USA' && !availableCountries.find(c => c.countryId === dialoutCountryCode)) { + throw new EvTypeError({ + type: messageTypes.NO_SUPPORT_COUNTRY, + }); + } + } + } +}; diff --git a/src/lib/parseNumber.ts b/src/lib/parseNumber.ts new file mode 100644 index 0000000..ab02be1 --- /dev/null +++ b/src/lib/parseNumber.ts @@ -0,0 +1,30 @@ +import { callErrors } from '@ringcentral-integration/commons/modules/Call'; +import { parse, format, formatTypes } from '@ringcentral-integration/phone-number'; +import cleanNumber from '@ringcentral-integration/phone-number/lib/cleanNumber'; +import { messageTypes } from '@ringcentral-integration/engage-voice-widgets/enums'; +import { EvTypeError } from '@ringcentral-integration/engage-voice-widgets/lib/EvTypeError'; + +export const parseNumber = (input: string) => { + const { parsedNumber, isValid, hasInvalidChars } = parse({ + input, + }); + + if (input === '911' || input === '933' || input === '112') { + throw new EvTypeError({ + type: callErrors.emergencyNumber, + }); + } + + if (!isValid || hasInvalidChars || !parsedNumber) { + throw new EvTypeError({ + type: messageTypes.INVALID_NUMBER, + }); + } + + const formattedNumber = cleanNumber(format({ + phoneNumber: input, + type: formatTypes.e164, + })); + + return formattedNumber; +}; diff --git a/src/modules/EvCall/index.ts b/src/modules/EvCall/index.ts index d3552b3..01a53d8 100644 --- a/src/modules/EvCall/index.ts +++ b/src/modules/EvCall/index.ts @@ -3,6 +3,10 @@ import { action, } from '@ringcentral-integration/core'; import { EvCall as BaseEvCall } from '@ringcentral-integration/engage-voice-widgets/modules/EvCall'; +import { callErrors } from '@ringcentral-integration/commons/modules/Call'; +import { messageTypes } from '@ringcentral-integration/engage-voice-widgets/enums'; +import { parseNumber } from '../../lib/parseNumber'; +import { checkCountryCode } from '../../lib/checkCountryCode'; const DEFAULT_OUTBOUND_SETTING = { dialoutCallerId: '-1', @@ -43,6 +47,35 @@ class EvCall extends BaseEvCall { this.dialoutRingTime = defaultRingTime; } } + + private _checkAndParseNumber(phoneNumber: string) { + try { + checkCountryCode(phoneNumber, this._deps.evAuth.availableCountries); + + return parseNumber(phoneNumber); + } catch (error) { + switch (error.type) { + case messageTypes.NO_SUPPORT_COUNTRY: + this._deps.alert.danger({ + message: messageTypes.NO_SUPPORT_COUNTRY, + ttl: 0, + }); + break; + case callErrors.emergencyNumber: + this._deps.alert.danger({ + message: callErrors.emergencyNumber, + }); + break; + default: + this._deps.alert.danger({ + message: callErrors.noToNumber, + }); + break; + } + + throw error; + } + } } export { EvCall };