forked from webex/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
191 lines (178 loc) · 6.44 KB
/
Copy pathstore.ts
File metadata and controls
191 lines (178 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {makeAutoObservable, observable} from 'mobx';
import Webex, {ITask} from '@webex/contact-center';
import {
IContactCenter,
Profile,
Team,
IdleCode,
InitParams,
IStore,
ILogger,
IWrapupCode,
ICustomState,
AgentLoginProfile,
LoginOptions,
WithWebex,
RealTimeTranscriptionData,
} from './store.types';
import {getFeatureFlags} from './util';
class Store implements IStore {
private static instance: Store;
teams: Team[] = [];
loginOptions: string[] = [];
cc: IContactCenter;
logger: ILogger;
idleCodes: IdleCode[] = [];
agentId: string = '';
currentTheme: string = 'LIGHT';
wrapupCodes: IWrapupCode[] = [];
currentTask: ITask = null;
isAgentLoggedIn = false;
deviceType: string = '';
teamId: string = '';
taskList: Record<string, ITask> = {};
dialNumber: string = '';
currentState: string = '';
customState: ICustomState = null;
isQueueConsultInProgress = false;
isDeclineButtonEnabled = false;
currentConsultQueueId: string = '';
consultStartTimeStamp = undefined;
lastStateChangeTimestamp?: number;
lastIdleCodeChangeTimestamp?: number;
showMultipleLoginAlert: boolean = false;
callControlAudio: MediaStream | null = null;
featureFlags: {[key: string]: boolean} = {};
isEndConsultEnabled: boolean = false;
isAddressBookEnabled: boolean = false;
allowConsultToQueue: boolean = false;
agentProfile: AgentLoginProfile = {};
isMuted: boolean = false;
isDigitalChannelsInitialized: boolean = false;
dataCenter: string = '';
realtimeTranscriptionData: Partial<RealTimeTranscriptionData>[] = [];
acceptedCampaignIds: Set<string> = new Set();
constructor() {
makeAutoObservable(this, {
cc: observable.ref,
});
}
public static getInstance(): Store {
if (!Store.instance) {
console.log('Creating new store instance');
Store.instance = new Store();
}
console.log('Returning store instance');
return Store.instance;
}
registerCC(webex?: WithWebex['webex']): Promise<void> {
if (webex) {
this.cc = webex.cc;
}
if (typeof webex === 'undefined' && typeof this.cc === 'undefined') {
throw new Error('Webex SDK not initialized');
}
this.logger = this.cc.LoggerProxy;
this.logger.info('CC-Widgets: Contact-center registerCC(): starting registration', {
module: 'cc-store#store.ts',
method: 'registerCC',
});
return this.cc
.register()
.then((response: Profile) => {
this.logger.log('CC-Widgets: Contact-center registerCC(): registration successful', {
module: 'cc-store#store.ts',
method: 'registerCC',
});
// wire up logger into feature‐flag extraction
this.featureFlags = getFeatureFlags(response);
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
this.teams = response.teams;
this.loginOptions = response.webRtcEnabled
? response.loginVoiceOptions
: response.loginVoiceOptions.filter((option) => option !== 'BROWSER');
this.loginOptions.sort((a, b) => Object.keys(LoginOptions).indexOf(a) - Object.keys(LoginOptions).indexOf(b));
this.idleCodes = response.idleCodes;
this.agentId = response.agentId;
this.wrapupCodes = response.wrapupCodes;
this.isAgentLoggedIn = response.isAgentLoggedIn;
this.deviceType = response.deviceType ?? this.loginOptions[0];
this.dialNumber = response.dn;
this.teamId = response.currentTeamId ?? '';
this.currentState = response.lastStateAuxCodeId;
this.lastStateChangeTimestamp = response.lastStateChangeTimestamp;
this.lastIdleCodeChangeTimestamp = response.lastIdleCodeChangeTimestamp;
this.isEndConsultEnabled = response.isEndConsultEnabled;
// TODO: Remove this once SDK performs the validation
this.isAddressBookEnabled = Boolean(response.addressBookId);
this.allowConsultToQueue = response.allowConsultToQueue;
this.agentProfile.agentName = response.agentName;
this.agentProfile.isTimeoutDesktopInactivityEnabled = response.isTimeoutDesktopInactivityEnabled;
this.agentProfile.timeoutDesktopInactivityMins = response.timeoutDesktopInactivityMins;
this.dataCenter = (response as {environment?: string}).environment || '';
})
.catch((error) => {
this.logger.error(`CC-Widgets: Contact-center registerCC(): failed - ${error}`, {
module: 'cc-store#store.ts',
method: 'registerCC',
});
return Promise.reject(error);
});
}
init(options: InitParams, setupEventListeners): Promise<void> {
if ('webex' in options) {
// If devs decide to go with webex, they will have to listen to the ready event before calling init
// This has to be documented
setupEventListeners(options.webex.cc);
return this.registerCC(options.webex);
}
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error('Webex SDK failed to initialize'));
}, 6000);
try {
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
const webex = Webex.init({
config: options.webexConfig,
credentials: {
access_token: options.access_token,
},
});
webex.once('ready', () => {
try {
setupEventListeners(webex.cc);
clearTimeout(timer);
this.registerCC(webex)
.then(() => {
this.logger.log('CC-Widgets: Store init(): store initialization complete', {
module: 'cc-store#store.ts',
method: 'init',
});
resolve();
})
.catch((error) => {
this.logger.error(`CC-Widgets: Store init(): registration failed - ${error}`, {
module: 'cc-store#store.ts',
method: 'init',
});
reject(error);
});
} catch (error) {
clearTimeout(timer);
if (this.logger) {
this.logger.error(`CC-Widgets: Store init(): setupEventListeners failed - ${error}`, {
module: 'cc-store#store.ts',
method: 'init',
});
}
reject(error);
}
});
} catch (error) {
clearTimeout(timer);
reject(error);
}
});
}
}
export default Store;