Skip to content

Commit

Permalink
fix: send number to server with e164 (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux authored Nov 1, 2024
1 parent b531468 commit 98ba034
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib/checkCountryCode.ts
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,
});
}
}
}
};
30 changes: 30 additions & 0 deletions src/lib/parseNumber.ts
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;
};
33 changes: 33 additions & 0 deletions src/modules/EvCall/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 };

0 comments on commit 98ba034

Please sign in to comment.