-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: send number to server with e164 (#119)
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters