diff --git a/src/Session.ts b/src/Session.ts index c085726..6338522 100644 --- a/src/Session.ts +++ b/src/Session.ts @@ -55,6 +55,11 @@ export interface Party { recordings?: Recording[]; } +export interface Origin { + type: 'Call' | 'RingOut' | 'RingMe' | 'Conference' | 'GreetingsRecording' | + 'VerificationCall' | 'TestCall'; +} + export interface SessionData { id: string; extensionId: string; @@ -64,6 +69,7 @@ export interface SessionData { creationTime: string; voiceCallToken?: string; sequence?: number; + origin: Origin; } export interface ForwardParams { diff --git a/src/helper.ts b/src/helper.ts new file mode 100644 index 0000000..2112f0f --- /dev/null +++ b/src/helper.ts @@ -0,0 +1,50 @@ + +import { Session, SessionData, Direction as callDirections } from './Session'; + +function ringOutInboundLegCheck(newData: SessionData, allSessions: Session[]) { + const { parties = [], origin = { type: 'Call' } } = newData || {}; + const party = parties[0]; + const checkResult = { + isRingOutInboundLeg: false, + legSessionId: null, + }; + if (!party || origin.type === 'Call' && party.direction === callDirections.outbound) { + return checkResult; + } + if (allSessions.length) { + for (const session of allSessions) { + const sessionIdGap = parseInt(newData.sessionId, 10) - parseInt(session.sessionId, 10); + const { party: existedSessionParty } = session; + switch (sessionIdGap) { + case 1000: + case 2000: + case 3000: + case 4000: { + if (party.direction === callDirections.inbound && party.from && party.to && + existedSessionParty.from && existedSessionParty.to && (party.from.phoneNumber === existedSessionParty.to.phoneNumber) && + (party.to.phoneNumber === existedSessionParty.from.phoneNumber)) { + checkResult.isRingOutInboundLeg = true; + } + break; + } + case -1000: + case -2000: + case -3000: + case -4000: { + if (party.direction === callDirections.outbound && party.from && party.to && + existedSessionParty.from && existedSessionParty.to && (party.from.phoneNumber === existedSessionParty.to.phoneNumber) && + (party.to.phoneNumber === existedSessionParty.from.phoneNumber)) { + checkResult.isRingOutInboundLeg = false; + checkResult.legSessionId = session.id; + } + break; + } + default: + break; + } + } + } + return checkResult; +} + +export { ringOutInboundLegCheck }; diff --git a/src/index.ts b/src/index.ts index 869e39f..8b2c070 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { SDK as RingCentralSDK } from '@ringcentral/sdk'; import { Session, SessionData, PartyStatusCode } from './Session'; import { formatParty } from './formatParty'; import { USER_AGENT } from './userAgent'; - +import { ringOutInboundLegCheck } from './helper'; export interface SessionsMap { [key: string]: any; } @@ -153,6 +153,15 @@ export class RingCentralCallControl extends EventEmitter { } // use first event's eventTime as session creationTime newData.creationTime = eventTime; + // if new session is the inbound leg of ringout call then abandon it + const checkResult = ringOutInboundLegCheck(newData, this.sessions); + if (checkResult.isRingOutInboundLeg) { + return; + } + if(!checkResult.isRingOutInboundLeg && checkResult.legSessionId) { + // if find an inbound leg then remove it from sessions + this._sessionsMap.delete(checkResult.legSessionId) + } const newSession = new Session(newData, this._sdk, this._accountLevel); newSession.on('status', () => { this.onSessionStatusUpdated(newSession); @@ -170,7 +179,7 @@ export class RingCentralCallControl extends EventEmitter { } } - get sessions() { + get sessions(): Session[] { return Array.from(this._sessionsMap.values()); } diff --git a/tsconfig.json b/tsconfig.json index 025d9fd..b5d71af 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,6 @@ "outDir": "lib", "resolveJsonModule": true }, - "files": ["src/index.ts", "src/Session.ts", "src/formatParty.ts", "src/userAgent.ts"], + "files": ["src/index.ts", "src/Session.ts", "src/formatParty.ts", "src/userAgent.ts", "src/helper.ts"], "compileOnSave": true }