@vgroup/dialbox 0.1.66 → 0.1.67

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,7 +2,7 @@ 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, of, interval, Subscription } from 'rxjs';
5
+ import { throwError, BehaviorSubject, Observable, of, 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 { catchError, switchMap, map, tap, shareReplay } from 'rxjs/operators';
@@ -588,6 +588,38 @@ class ExtensionService {
588
588
  const params = new HttpParams().set('deviceId', deviceId);
589
589
  return this.http.get(environment.apiUrl + '/utilities/twilio/incomingcall/token/web', { headers, params });
590
590
  }
591
+ // Check if token needs refresh based on expires_at timestamp
592
+ isTokenExpired() {
593
+ const expiresAt = localStorage.getItem('token_expires_at');
594
+ if (!expiresAt) {
595
+ return true; // No expiration info, assume expired
596
+ }
597
+ const expirationTime = parseInt(expiresAt) * 1000; // Convert to milliseconds
598
+ const currentTime = Date.now();
599
+ const bufferTime = 5 * 60 * 1000; // 5 minutes buffer before actual expiration
600
+ return currentTime >= (expirationTime - bufferTime);
601
+ }
602
+ // Refresh token if needed
603
+ refreshTokenIfNeeded(deviceId) {
604
+ if (this.isTokenExpired()) {
605
+ console.log('Token expired or about to expire, refreshing...');
606
+ return this.getIncomingCallToken(deviceId);
607
+ }
608
+ // Return current token data
609
+ const currentToken = localStorage.getItem('in-token');
610
+ const expiresAt = localStorage.getItem('token_expires_at');
611
+ if (currentToken && expiresAt) {
612
+ return new Observable(observer => {
613
+ observer.next({
614
+ token: currentToken,
615
+ expires_at: parseInt(expiresAt)
616
+ });
617
+ observer.complete();
618
+ });
619
+ }
620
+ // If no current token, get a new one
621
+ return this.getIncomingCallToken(deviceId);
622
+ }
591
623
  getOutgoingCallToken(payload) {
592
624
  const params = {
593
625
  'Content-Type': 'application/json',
@@ -1397,11 +1429,17 @@ class TwilioService {
1397
1429
  if (this.tokenInitialized) {
1398
1430
  return this.tokenInitialization$ || of(null);
1399
1431
  }
1400
- this.tokenInitialization$ = this.extensionService.getIncomingCallToken(this.deviceId).pipe(tap((data) => {
1432
+ this.tokenInitialization$ = this.extensionService.refreshTokenIfNeeded(this.deviceId).pipe(tap((data) => {
1401
1433
  this.incomingCallToken = data.token;
1402
1434
  localStorage.setItem('in-token', data.token);
1435
+ // Store expiration timestamp for future refresh checks
1436
+ if (data.expires_at) {
1437
+ localStorage.setItem('token_expires_at', data.expires_at.toString());
1438
+ console.log('Token expires at:', new Date(data.expires_at * 1000).toLocaleString());
1439
+ }
1403
1440
  this.tokenInitialized = true;
1404
- console.log('dfdfdsfdsfds');
1441
+ console.log('Token initialized successfully');
1442
+ this.startTokenRefreshScheduler();
1405
1443
  this.initializeDevice();
1406
1444
  }), catchError(error => {
1407
1445
  console.error('Error initializing Twilio token:', error);
@@ -1412,12 +1450,24 @@ class TwilioService {
1412
1450
  }
1413
1451
  initializeTwilioDevice(deviceId) {
1414
1452
  this.deviceId = deviceId;
1415
- if (this.tokenInitialized && this.incomingCallToken) {
1416
- this.initializeDevice();
1417
- }
1418
- else {
1419
- this.initializeToken().subscribe();
1420
- }
1453
+ // Always check if token needs refresh when initializing device
1454
+ this.extensionService.refreshTokenIfNeeded(deviceId).subscribe({
1455
+ next: (data) => {
1456
+ this.incomingCallToken = data.token;
1457
+ localStorage.setItem('in-token', data.token);
1458
+ // Store expiration timestamp for future refresh checks
1459
+ if (data.expires_at) {
1460
+ localStorage.setItem('token_expires_at', data.expires_at.toString());
1461
+ console.log('Token refreshed, expires at:', new Date(data.expires_at * 1000).toLocaleString());
1462
+ }
1463
+ this.tokenInitialized = true;
1464
+ this.startTokenRefreshScheduler();
1465
+ this.initializeDevice();
1466
+ },
1467
+ error: (error) => {
1468
+ console.error('Error refreshing token during device initialization:', error);
1469
+ }
1470
+ });
1421
1471
  }
1422
1472
  initializeDevice() {
1423
1473
  if (this.device) {
@@ -1520,6 +1570,45 @@ class TwilioService {
1520
1570
  };
1521
1571
  return this.http.post(environment.apiUrl + '/utilities/phonebook/delete/photo/' + id, payload, httpOptions);
1522
1572
  }
1573
+ // Start periodic token refresh scheduler
1574
+ startTokenRefreshScheduler() {
1575
+ // Clear existing interval if any
1576
+ if (this.tokenRefreshInterval) {
1577
+ clearInterval(this.tokenRefreshInterval);
1578
+ }
1579
+ // Check token every 10 minutes
1580
+ this.tokenRefreshInterval = setInterval(() => {
1581
+ if (this.deviceId && this.extensionService.isTokenExpired()) {
1582
+ console.log('Scheduled token refresh triggered');
1583
+ this.extensionService.refreshTokenIfNeeded(this.deviceId).subscribe({
1584
+ next: (data) => {
1585
+ this.incomingCallToken = data.token;
1586
+ localStorage.setItem('in-token', data.token);
1587
+ if (data.expires_at) {
1588
+ localStorage.setItem('token_expires_at', data.expires_at.toString());
1589
+ console.log('Token automatically refreshed, expires at:', new Date(data.expires_at * 1000).toLocaleString());
1590
+ }
1591
+ // Reinitialize device with new token
1592
+ this.initializeDevice();
1593
+ },
1594
+ error: (error) => {
1595
+ console.error('Error during scheduled token refresh:', error);
1596
+ }
1597
+ });
1598
+ }
1599
+ }, 10 * 60 * 1000); // 10 minutes
1600
+ }
1601
+ // Stop token refresh scheduler
1602
+ stopTokenRefreshScheduler() {
1603
+ if (this.tokenRefreshInterval) {
1604
+ clearInterval(this.tokenRefreshInterval);
1605
+ this.tokenRefreshInterval = null;
1606
+ }
1607
+ }
1608
+ // Cleanup method to be called when service is destroyed
1609
+ ngOnDestroy() {
1610
+ this.stopTokenRefreshScheduler();
1611
+ }
1523
1612
  // toggleCallerIdAlertFn(val: any) {
1524
1613
  // let httpOptions = {
1525
1614
  // headers: new HttpHeaders({