NTP-26125 | callStateManager Completed

This commit is contained in:
Mayank Singh
2025-01-13 00:58:16 +05:30
parent f9c12d4689
commit a81e6fb8e4
3 changed files with 73 additions and 26 deletions

1
.npmrc
View File

@@ -0,0 +1 @@
registry=http://localhost:4873/

View File

@@ -1,29 +1,7 @@
import {MessagingType, parseQuerystring} from "./assets/js/ajaxClient.ts";
type CallStatus = 'inactive' | 'initialized' | 'ringing' | 'connected' | 'hungup';
import {CallStatus, PushType, CallState} from "./types.ts";
enum PushType {
UserCallModelUpdated = 'UserCallModelUpdatedPush',
CustomerCallMemberUpdated = 'CustomerCallMemberUpdatedPush',
CustomerCallMemberCreated = 'CustomerCallMemberCreatedPush',
CRMCreateNotify = 'CRMCreateNotifyPush'
}
interface CallState {
isConnected: boolean;
lastCallStatus: CallStatus;
customerCallStatus: string | null;
crtObjectId: string | null;
connectionAttempts: number;
customerInfo: {
phoneNumber?: string;
lan?: string;
crtObjectId?: string;
userCRTObjectId?: string;
callId?: string;
} | null;
}
class CallStateManager {
private state: CallState = {
isConnected: false,
@@ -90,6 +68,18 @@ class CallStateManager {
case PushType.CRMCreateNotify:
this.handleCRMCreate(data);
break;
case PushType.UserCallMemberUpdated:
this.handleUserCallMemberUpdate(data);
break;
case PushType.UserCCRuntimeUpdated:
this.handleUserCCRuntimeUpdate(data);
break;
case PushType.UserLoggedOff:
this.handleUserLoggedOff();
break;
}
}
@@ -167,6 +157,36 @@ class CallStateManager {
};
}
private handleUserCallMemberUpdate(data: any) {
const validStatuses = ["ringing", "connected", "hungup"];
if (data?.associationType === 'transfer.association' &&
validStatuses.includes(data?.status)) {
window.postMessage({
type: MessagingType.ON_AMEYO_CALL_TRANSFER,
data
});
}
}
private handleUserCCRuntimeUpdate(data: any) {
if (data?.isOnBreak) {
window.postMessage({
type: MessagingType.ON_AMEYO_AGENT_ON_BREAK,
data: {
reason: data
}
});
}
}
private handleUserLoggedOff() {
window.postMessage({
type: MessagingType.ON_AMEYO_FORCED_LOGOUT,
data: {}
});
}
private handleCallDisconnect() {
window.postMessage({
type: MessagingType.ON_AMEYO_CALL_DISCONNECTED,
@@ -186,7 +206,6 @@ class CallStateManager {
}
}
// Usage
export const callStateManager = new CallStateManager();
export function extractAndProcessEvents(rawResponse) {
@@ -204,8 +223,8 @@ export function extractAndProcessEvents(rawResponse) {
// Process events in order, prioritizing CRMCreateNotifyPush
const sortedParts = [
...parts.filter(part => part?.pushType === 'CRMCreateNotifyPush'),
...parts.filter(part => part?.pushType !== 'CRMCreateNotifyPush')
...parts.filter(part => part?.pushType === PushType.CRMCreateNotify),
...parts.filter(part => part?.pushType !== PushType.CRMCreateNotify)
];
sortedParts.forEach(event => {

View File

@@ -76,3 +76,30 @@ export type CallTransferData = {
campaignId : string,
targetCRTObjectId: string
}
export type CallStatus = 'inactive' | 'initialized' | 'ringing' | 'connected' | 'hungup';
export enum PushType {
UserCallModelUpdated = 'UserCallModelUpdatedPush',
CustomerCallMemberUpdated = 'CustomerCallMemberUpdatedPush',
CustomerCallMemberCreated = 'CustomerCallMemberCreatedPush',
CRMCreateNotify = 'CRMCreateNotifyPush',
UserCallMemberUpdated = 'UserCallMemberUpdatedPush',
UserCCRuntimeUpdated = 'UserCCRuntimeUpdatedPush',
UserLoggedOff = 'UserLoggedOffPush'
}
export interface CallState {
isConnected: boolean;
lastCallStatus: CallStatus;
customerCallStatus: string | null;
crtObjectId: string | null;
connectionAttempts: number;
customerInfo: {
phoneNumber?: string;
lan?: string;
crtObjectId?: string;
userCRTObjectId?: string;
callId?: string;
} | null;
}