@vgroup/dialbox 0.0.51 → 0.0.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,11 +2,11 @@ import * as i0 from '@angular/core';
2
2
  import { Injectable, EventEmitter, Component, Input, Output, ViewChild, Inject, NgModule } from '@angular/core';
3
3
  import swal from 'sweetalert2';
4
4
  import { AsYouType } from 'libphonenumber-js';
5
- import { throwError, BehaviorSubject, timer, interval, Subscription } from 'rxjs';
5
+ import { throwError, BehaviorSubject, interval, Subscription } from 'rxjs';
6
6
  import * as i1 from '@angular/common/http';
7
7
  import { HttpHeaders, HttpParams, HttpClientModule } from '@angular/common/http';
8
8
  import { Device } from '@twilio/voice-sdk';
9
- import { catchError, map, switchMap } from 'rxjs/operators';
9
+ import { catchError, switchMap, map } from 'rxjs/operators';
10
10
  import * as i3$1 from '@angular/material/dialog';
11
11
  import { MAT_DIALOG_DATA } from '@angular/material/dialog';
12
12
  import * as i5 from '@angular/router';
@@ -154,22 +154,6 @@ class ExtensionService {
154
154
  this.isProfileUpdated = new BehaviorSubject(false);
155
155
  this.callerIdSubject = new BehaviorSubject(null);
156
156
  this.callerId$ = this.callerIdSubject.asObservable();
157
- // initiateCall(payload: any): Observable<any> {
158
- // return this.ipAddressService.getIpAddressInfo().pipe(
159
- // switchMap(ipAddressInfo => {
160
- // const params = {
161
- // 'Content-Type': 'application/json',
162
- // 'Auth-Key': 'Bearer ' + localStorage.getItem('ext_token'),
163
- // 'ip-address': ipAddressInfo.ip,
164
- // 'ip-country': ipAddressInfo.address.country
165
- // };
166
- // const httpOptions = { headers: new HttpHeaders(params) };
167
- // return this.http.post<[]>(environment.apiUrl + '/utilities/ext/ur/initiate/call', payload, httpOptions);
168
- // })
169
- // );
170
- // }
171
- this.maxRetries = 3;
172
- this.retryDelay = 2000; // 2 seconds
173
157
  }
174
158
  changeMessage(message) {
175
159
  this.messageSource.next(message);
@@ -420,34 +404,13 @@ class ExtensionService {
420
404
  const httpOptions = { headers: new HttpHeaders(params) };
421
405
  return this.http.post(environment.apiUrl + '/utilities/softphone/purchase/number', dtModel, httpOptions);
422
406
  }
423
- fetchCallerId(token, retryCount = 0) {
424
- if (!token) {
425
- return throwError(() => new Error('No authentication token provided'));
426
- }
427
- const headers = new HttpHeaders({
407
+ fetchCallerId(token) {
408
+ const params = {
428
409
  'Content-Type': 'application/json',
429
- 'Auth-Key': `Bearer ${token}`,
430
- 'Cache-Control': 'no-cache',
431
- 'Pragma': 'no-cache'
432
- });
433
- const httpOptions = {
434
- headers: headers,
435
- withCredentials: true,
436
- observe: 'response'
410
+ 'Auth-Key': "Bearer " + token
437
411
  };
438
- return this.http.get(`${environment.apiUrl}/utilities/softphone/fetch/callerid`, httpOptions).pipe(map(response => {
439
- if (!response.body) {
440
- throw new Error('Empty response from server');
441
- }
442
- return response.body;
443
- }), catchError(error => {
444
- console.error('Error fetching caller ID:', error);
445
- if (retryCount < this.maxRetries) {
446
- console.log(`Retrying... (${retryCount + 1}/${this.maxRetries})`);
447
- return timer(this.retryDelay).pipe(switchMap(() => this.fetchCallerId(token, retryCount + 1)));
448
- }
449
- return throwError(() => new Error(`Failed to fetch caller ID after ${this.maxRetries} attempts`));
450
- }));
412
+ const httpOptions = { headers: new HttpHeaders(params) };
413
+ return this.http.get(environment.apiUrl + '/utilities/softphone/fetch/callerid', httpOptions);
451
414
  }
452
415
  updateNumberLabel(token, dtModel) {
453
416
  const params = {
@@ -602,66 +565,36 @@ class ExtensionService {
602
565
  }
603
566
  }));
604
567
  }
605
- getIncomingCallToken(retryCount = 0) {
606
- const token = localStorage.getItem('ext_token');
607
- if (!token) {
608
- return throwError(() => new Error('No authentication token found'));
609
- }
610
- const params = new HttpHeaders({
568
+ // initiateCall(payload: any): Observable<any> {
569
+ // return this.ipAddressService.getIpAddressInfo().pipe(
570
+ // switchMap(ipAddressInfo => {
571
+ // const params = {
572
+ // 'Content-Type': 'application/json',
573
+ // 'Auth-Key': 'Bearer ' + localStorage.getItem('ext_token'),
574
+ // 'ip-address': ipAddressInfo.ip,
575
+ // 'ip-country': ipAddressInfo.address.country
576
+ // };
577
+ // const httpOptions = { headers: new HttpHeaders(params) };
578
+ // return this.http.post<[]>(environment.apiUrl + '/utilities/ext/ur/initiate/call', payload, httpOptions);
579
+ // })
580
+ // );
581
+ // }
582
+ getIncomingCallToken() {
583
+ const params = {
611
584
  'Content-Type': 'application/json',
612
- 'Auth-Key': `Bearer ${token}`,
613
- 'Cache-Control': 'no-cache',
614
- 'Pragma': 'no-cache'
615
- });
616
- const httpOptions = {
617
- headers: params,
618
- withCredentials: true,
619
- observe: 'response'
585
+ 'Auth-Key': "Bearer " + localStorage.getItem('ext_token')
620
586
  };
621
- return this.http.get(`${environment.apiUrl}/utilities/twilio/incomingcall/token/web`, httpOptions).pipe(map(response => {
622
- if (!response.body) {
623
- throw new Error('Empty response from server');
624
- }
625
- return response.body;
626
- }), catchError(error => {
627
- console.error('Error fetching Twilio token:', error);
628
- if (retryCount < this.maxRetries) {
629
- console.log(`Retrying... (${retryCount + 1}/${this.maxRetries})`);
630
- return timer(this.retryDelay).pipe(switchMap(() => this.getIncomingCallToken(retryCount + 1)));
631
- }
632
- return throwError(() => new Error(`Failed to get Twilio token after ${this.maxRetries} attempts`));
633
- }));
587
+ const httpOptions = { headers: new HttpHeaders(params) };
588
+ return this.http.get(environment.apiUrl + '/utilities/twilio/incomingcall/token/web', httpOptions);
634
589
  }
635
- getOutgoingCallToken(payload, retryCount = 0) {
636
- const token = localStorage.getItem('ext_token');
637
- if (!token) {
638
- return throwError(() => new Error('No authentication token found'));
639
- }
640
- const headers = new HttpHeaders({
590
+ getOutgoingCallToken(payload) {
591
+ const params = {
641
592
  'Content-Type': 'application/json',
642
- 'Auth-Key': `Bearer ${token}`,
643
- 'c2c-request': window.location.hostname,
644
- 'Cache-Control': 'no-cache',
645
- 'Pragma': 'no-cache'
646
- });
647
- const httpOptions = {
648
- headers: headers,
649
- withCredentials: true,
650
- observe: 'response'
593
+ 'Auth-Key': "Bearer " + localStorage.getItem('ext_token'),
594
+ 'c2c-request': window.location.hostname
651
595
  };
652
- return this.http.post(`${environment.apiUrl}/utilities/ext/ur/generate/token`, payload, httpOptions).pipe(map(response => {
653
- if (!response.body) {
654
- throw new Error('Empty response from server');
655
- }
656
- return response.body;
657
- }), catchError(error => {
658
- console.error('Error fetching outgoing call token:', error);
659
- if (retryCount < this.maxRetries) {
660
- console.log(`Retrying... (${retryCount + 1}/${this.maxRetries})`);
661
- return timer(this.retryDelay).pipe(switchMap(() => this.getOutgoingCallToken(payload, retryCount + 1)));
662
- }
663
- return throwError(() => new Error(`Failed to get outgoing call token after ${this.maxRetries} attempts`));
664
- }));
596
+ const httpOptions = { headers: new HttpHeaders(params) };
597
+ return this.http.post(environment.apiUrl + '/utilities/ext/ur/generate/token', payload, httpOptions);
665
598
  }
666
599
  getCallRecording(callSid) {
667
600
  const headers = new HttpHeaders({
@@ -1382,89 +1315,29 @@ class TwilioService {
1382
1315
  this.isAvailableNumber = new BehaviorSubject(false);
1383
1316
  this.callerIdList = new BehaviorSubject([]);
1384
1317
  this.triggerSMSReload = new BehaviorSubject(false);
1385
- this.isInitialized = false;
1386
- // Initialize when token is available
1387
- if (this.token) {
1388
- this.initializeTwilioDevice();
1389
- }
1390
- else {
1391
- // If token is not available, listen for it
1392
- const tokenCheck = setInterval(() => {
1393
- const token = localStorage.getItem('ext_token');
1394
- if (token) {
1395
- this.token = token;
1396
- this.initializeTwilioDevice();
1397
- clearInterval(tokenCheck);
1398
- }
1399
- }, 1000);
1400
- }
1318
+ this.initializeTwilioDevice();
1401
1319
  }
1402
1320
  initializeTwilioDevice() {
1403
- if (this.isInitialized || !this.token) {
1404
- return;
1321
+ if (this.token) {
1322
+ this.extensionService.getIncomingCallToken().subscribe((data) => {
1323
+ this.incomingCallToken = data.token;
1324
+ localStorage.setItem('in-token', data.token);
1325
+ this.device = new Device(this.incomingCallToken, {
1326
+ allowIncomingWhileBusy: true,
1327
+ // Add any other necessary options
1328
+ });
1329
+ this.device.register();
1330
+ this.device.on('incoming', (call) => {
1331
+ this.currentCall.next(call);
1332
+ this.callType.next('INCOMING');
1333
+ this.currentCallState.next('incoming');
1334
+ });
1335
+ this.device.on('error', (error) => {
1336
+ console.error('Twilio Device Error:', error);
1337
+ // Add error handling and reconnection logic
1338
+ });
1339
+ });
1405
1340
  }
1406
- console.log('Initializing Twilio Device...');
1407
- this.extensionService.getIncomingCallToken().subscribe({
1408
- next: (data) => {
1409
- if (!data?.token) {
1410
- console.error('No token received from server');
1411
- return;
1412
- }
1413
- try {
1414
- console.log('Setting up Twilio Device with token');
1415
- this.incomingCallToken = data.token;
1416
- localStorage.setItem('in-token', data.token);
1417
- // Destroy existing device if any
1418
- if (this.device) {
1419
- this.device.destroy();
1420
- }
1421
- // Create new device instance
1422
- this.device = new Device(data.token, {
1423
- allowIncomingWhileBusy: true,
1424
- closeProtection: true
1425
- });
1426
- // Set up event listeners
1427
- this.setupDeviceListeners();
1428
- // Register the device
1429
- this.device.register();
1430
- this.isInitialized = true;
1431
- }
1432
- catch (error) {
1433
- console.error('Error initializing Twilio Device:', error);
1434
- this.isInitialized = false;
1435
- }
1436
- },
1437
- error: (error) => {
1438
- console.error('Error getting Twilio token:', error);
1439
- // Retry after delay
1440
- setTimeout(() => this.initializeTwilioDevice(), 5000);
1441
- }
1442
- });
1443
- }
1444
- setupDeviceListeners() {
1445
- if (!this.device)
1446
- return;
1447
- this.device.on('registered', () => {
1448
- console.log('Twilio Device registered successfully');
1449
- });
1450
- this.device.on('incoming', (call) => {
1451
- console.log('Incoming call received');
1452
- this.currentCall.next(call);
1453
- this.callType.next('INCOMING');
1454
- this.currentCallState.next('incoming');
1455
- });
1456
- this.device.on('error', (error) => {
1457
- console.error('Twilio Device Error:', error);
1458
- this.isInitialized = false;
1459
- // Attempt to reinitialize after delay
1460
- setTimeout(() => this.initializeTwilioDevice(), 5000);
1461
- });
1462
- this.device.on('unregistered', () => {
1463
- console.log('Twilio Device unregistered');
1464
- this.isInitialized = false;
1465
- // Attempt to re-register
1466
- setTimeout(() => this.initializeTwilioDevice(), 5000);
1467
- });
1468
1341
  }
1469
1342
  // onIncomingCall(){
1470
1343
  // this.device.on('incoming', (call:any) => {
@@ -1672,7 +1545,6 @@ class IncomingCallComponent {
1672
1545
  if (call) {
1673
1546
  this.twilioCallData = call;
1674
1547
  this.twilioAuthId = call.customParameters.get('twilioAuthId');
1675
- this.newIncomingCallsList.push(call);
1676
1548
  if (!call.parameters) {
1677
1549
  call.parameters = {};
1678
1550
  }
@@ -2264,7 +2136,7 @@ class DialboxComponent {
2264
2136
  this.ipService = ipService;
2265
2137
  this.extensionService = extensionService;
2266
2138
  this.router = router;
2267
- this.isDialpadHidden = false;
2139
+ this.isDialpadHidden = true;
2268
2140
  this.closeDialpadEvent = new EventEmitter();
2269
2141
  this.callInitiated = new EventEmitter();
2270
2142
  this.endCallEvent = new EventEmitter();
@@ -2310,14 +2182,22 @@ class DialboxComponent {
2310
2182
  this.subscriptions = new Subscription();
2311
2183
  this.shakeDedicatedBtn = false;
2312
2184
  this.isSmartDialCall = false;
2185
+ this.isTwilioInitialized = false;
2313
2186
  this.isMinimised = false;
2187
+ // Subscribe to incoming calls
2188
+ this.subscriptions.add(this.twilioService.currentCall.subscribe(call => {
2189
+ if (call) {
2190
+ this.handleIncomingCall(call);
2191
+ }
2192
+ }));
2314
2193
  }
2315
2194
  ngOnInit() {
2316
2195
  try {
2317
2196
  this.token = localStorage.getItem('ext_token') || '';
2318
- //this.isCallInProgress = true;
2197
+ this.initializeTwilio();
2319
2198
  this.getContactList();
2320
2199
  this.getUserCallSetting();
2200
+ // Subscribe to dial number events
2321
2201
  const sub1 = this.twilioService.dialNumberFromOtherModule.subscribe((contact) => {
2322
2202
  if (contact.number) {
2323
2203
  this.isSmartDialCall = false;
@@ -2436,12 +2316,47 @@ class DialboxComponent {
2436
2316
  this.registerDragElement();
2437
2317
  }
2438
2318
  ngOnChanges(changes) {
2439
- if (changes['isDialpadHidden'] && !this.isDialpadHidden) {
2440
- this.getContactList();
2441
- this.getUserCallSetting();
2442
- setTimeout(() => {
2443
- this.dialInputElement.nativeElement.focus();
2444
- }, 0);
2319
+ if (changes['isDialpadHidden']) {
2320
+ if (!changes['isDialpadHidden'].firstChange && !this.isDialpadHidden) {
2321
+ // Re-initialize Twilio when dialpad becomes visible
2322
+ this.initializeTwilio();
2323
+ }
2324
+ if (!this.isDialpadHidden) {
2325
+ this.getContactList();
2326
+ this.getUserCallSetting();
2327
+ setTimeout(() => {
2328
+ if (this.dialInputElement?.nativeElement) {
2329
+ this.dialInputElement.nativeElement.focus();
2330
+ }
2331
+ }, 0);
2332
+ }
2333
+ }
2334
+ }
2335
+ initializeTwilio() {
2336
+ if (this.isTwilioInitialized || !this.token)
2337
+ return;
2338
+ this.isTwilioInitialized = true;
2339
+ this.twilioService.initializeTwilioDevice();
2340
+ }
2341
+ handleIncomingCall(call) {
2342
+ try {
2343
+ if (!call)
2344
+ return;
2345
+ this.incomingCallInitiated.emit();
2346
+ this.newIncomingCallData = call;
2347
+ // Add to incoming calls list if not already present
2348
+ const existingCall = this.incomingCallsList.find((c) => c.parameters.CallSid === call.parameters.CallSid);
2349
+ if (!existingCall) {
2350
+ this.incomingCallsList.unshift(call);
2351
+ this.incomingCallsNewInfoEvent.emit(this.incomingCallsList);
2352
+ }
2353
+ // Show the dialpad if it's hidden
2354
+ if (this.isDialpadHidden) {
2355
+ this.isDialpadHidden = false;
2356
+ }
2357
+ }
2358
+ catch (error) {
2359
+ console.error('Error handling incoming call:', error);
2445
2360
  }
2446
2361
  }
2447
2362
  registerDragElement() {