Skip to content

Commit

Permalink
mark ringout inbound leg (#18)
Browse files Browse the repository at this point in the history
* add ringout inbound leg

* fix failed test

* new implement

* review feedback

* Update index.ts

fix wrong type

* review feedback

Co-authored-by: jackson lin <[email protected]>
  • Loading branch information
jarvislin94 and jackson lin authored Dec 3, 2020
1 parent d299f02 commit 4408a54
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,6 +69,7 @@ export interface SessionData {
creationTime: string;
voiceCallToken?: string;
sequence?: number;
origin: Origin;
}

export interface ForwardParams {
Expand Down
50 changes: 50 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -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 };
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -170,7 +179,7 @@ export class RingCentralCallControl extends EventEmitter {
}
}

get sessions() {
get sessions(): Session[] {
return Array.from(this._sessionsMap.values());
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4408a54

Please sign in to comment.