@vgroup/dialbox 0.0.79 → 0.0.80

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.
@@ -1366,10 +1366,10 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
1366
1366
  }], ctorParameters: function () { return [{ type: TwilioService }]; } });
1367
1367
 
1368
1368
  class TwilioService {
1369
- constructor(http, extensionService, notificationSerivce) {
1369
+ constructor(http, extensionService, notificationService) {
1370
1370
  this.http = http;
1371
1371
  this.extensionService = extensionService;
1372
- this.notificationSerivce = notificationSerivce;
1372
+ this.notificationService = notificationService;
1373
1373
  this.openInProgressDialpad = new BehaviorSubject(false);
1374
1374
  this.currentCall = new BehaviorSubject(null);
1375
1375
  this.currentCallState = new BehaviorSubject('none'); // 'incoming', 'in-progress', 'out-progress', 'none'
@@ -1388,54 +1388,92 @@ class TwilioService {
1388
1388
  this.isAvailableNumber = new BehaviorSubject(false);
1389
1389
  this.callerIdList = new BehaviorSubject([]);
1390
1390
  this.triggerSMSReload = new BehaviorSubject(false);
1391
- this.initializeTwilioDevice();
1391
+ this.isInitialized = false;
1392
+ this.autoAnswer = false; // Default to manual answer
1393
+ // Don't initialize here - let the component handle it
1392
1394
  }
1393
1395
  initializeTwilioDevice() {
1394
- if (this.token) {
1396
+ return new Promise((resolve, reject) => {
1397
+ if (this.device) {
1398
+ console.log('Twilio device already initialized');
1399
+ resolve();
1400
+ return;
1401
+ }
1402
+ const token = localStorage.getItem('ext_token');
1403
+ if (!token) {
1404
+ const error = new Error('No authentication token found in localStorage');
1405
+ console.error(error);
1406
+ reject(error);
1407
+ return;
1408
+ }
1409
+ // Store token in a const to ensure TypeScript knows it's not null
1410
+ const authToken = token;
1395
1411
  this.extensionService.getIncomingCallToken().subscribe((data) => {
1412
+ if (!data || !data.token) {
1413
+ const error = 'No token received from getIncomingCallToken';
1414
+ console.error(error);
1415
+ reject(new Error(error));
1416
+ return;
1417
+ }
1396
1418
  this.incomingCallToken = data.token;
1397
1419
  localStorage.setItem('in-token', data.token);
1398
- this.device = new Device(this.incomingCallToken, {
1399
- allowIncomingWhileBusy: true,
1400
- // Add any other necessary options
1401
- });
1402
- this.device.register();
1403
- this.device.on('incoming', (call) => {
1404
- this._currentCall = call;
1405
- this.currentCall.next(call);
1406
- this.callType.next('INCOMING');
1407
- this.currentCallState.next('incoming');
1408
- this.notificationSerivce.showNotification(call);
1409
- // Set up call event handlers
1410
- call.on('accept', () => {
1411
- this.currentCallState.next('in-progress');
1412
- });
1413
- call.on('disconnect', () => {
1414
- this._currentCall = null;
1415
- this.currentCall.next(null);
1416
- this.currentCallState.next('none');
1417
- this.callType.next('NIL');
1420
+ try {
1421
+ this.device = new Device(authToken, {
1422
+ allowIncomingWhileBusy: true,
1423
+ // Add any other necessary options
1418
1424
  });
1419
- call.on('cancel', () => {
1420
- this._currentCall = null;
1421
- this.currentCall.next(null);
1422
- this.currentCallState.next('none');
1423
- this.callType.next('NIL');
1424
- });
1425
- });
1426
- this.device.on('error', (error) => {
1427
- console.error('Twilio Device Error:', error);
1428
- // Add error handling and reconnection logic
1429
- });
1425
+ // Setup device with token
1426
+ this.setupDevice(this.device);
1427
+ this.device.register();
1428
+ console.log('Twilio device initialized successfully');
1429
+ resolve();
1430
+ }
1431
+ catch (error) {
1432
+ console.error('Error initializing Twilio device:', error);
1433
+ reject(error);
1434
+ }
1430
1435
  });
1431
- }
1436
+ });
1437
+ }
1438
+ setupDevice(device) {
1439
+ device.on('incoming', (call) => {
1440
+ console.log('Incoming call from:', call.parameters['From']);
1441
+ this._currentCall = call;
1442
+ this.currentCall.next(call);
1443
+ this.currentCallState.next('incoming');
1444
+ // Auto-answer if configured
1445
+ if (this.autoAnswer) {
1446
+ console.log('Auto-answering incoming call');
1447
+ // Call accept() without promise chaining since it returns void
1448
+ call.accept();
1449
+ this.currentCallState.next('in-progress');
1450
+ }
1451
+ // Handle call end
1452
+ call.on('disconnect', () => {
1453
+ if (this._currentCall && this._currentCall.parameters['CallSid'] === call.parameters['CallSid']) {
1454
+ this._currentCall = null;
1455
+ this.currentCall.next(null);
1456
+ this.currentCallState.next('none');
1457
+ }
1458
+ });
1459
+ // Handle call cancel
1460
+ call.on('cancel', () => {
1461
+ if (this._currentCall && this._currentCall.parameters['CallSid'] === call.parameters['CallSid']) {
1462
+ this._currentCall = null;
1463
+ this.currentCall.next(null);
1464
+ this.currentCallState.next('none');
1465
+ }
1466
+ });
1467
+ });
1468
+ device.on('error', (error) => {
1469
+ console.error('Twilio Device Error:', error);
1470
+ // Handle specific error codes if needed
1471
+ if (error.code === 31201) { // Token expired
1472
+ console.log('Token expired, attempting to refresh...');
1473
+ this.initializeTwilioDevice();
1474
+ }
1475
+ });
1432
1476
  }
1433
- // onIncomingCall(){
1434
- // this.device.on('incoming', (call:any) => {
1435
- // console.log(call);
1436
- // //call.accept();
1437
- // });
1438
- // }
1439
1477
  saveContact(payload) {
1440
1478
  const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Auth-Key': "Bearer " + localStorage.getItem('ext_token') }) };
1441
1479
  return this.http.post(environment.apiUrl + '/utilities/phonebook/add/contacts/manually', payload, httpOptions);
@@ -1464,9 +1502,6 @@ class TwilioService {
1464
1502
  return this.http.get(environment.apiUrl + '/utilities/phonebook/favourite/contacts', httpOptions);
1465
1503
  }
1466
1504
  getRecentCalls(pageIndex, pageSize) {
1467
- // let params = new HttpParams();
1468
- // params = params.set('size', pageSize);
1469
- // params = params.set('page', pageIndex);
1470
1505
  const headers = { 'Content-Type': 'application/json', 'Auth-Key': "Bearer " + localStorage.getItem('ext_token') };
1471
1506
  const httpOptions = { headers };
1472
1507
  return this.http.get(environment.apiUrl + '/utilities/phonebook/recent/calls', httpOptions);
@@ -1497,14 +1532,6 @@ class TwilioService {
1497
1532
  };
1498
1533
  return this.http.post(environment.apiUrl + '/utilities/phonebook/delete/photo/' + id, payload, httpOptions);
1499
1534
  }
1500
- // toggleCallerIdAlertFn(val: any) {
1501
- // let httpOptions = {
1502
- // headers: new HttpHeaders({
1503
- // 'Auth-Key': "Bearer " + localStorage.getItem('ext_token')
1504
- // })
1505
- // };
1506
- // return this.http.put<[]>(environment.apiUrl + '/utilities/softphone/callerid/alert/' + val, {}, httpOptions);
1507
- // }
1508
1535
  toggleCoutryCodeToast(val) {
1509
1536
  let httpOptions = {
1510
1537
  headers: new HttpHeaders({
@@ -1518,7 +1545,6 @@ class TwilioService {
1518
1545
  headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Auth-Key': "Bearer " + localStorage.getItem('ext_token') }),
1519
1546
  params: new HttpParams().set('isoCode', isoCode)
1520
1547
  };
1521
- const params = new HttpParams().set('isoCode', isoCode);
1522
1548
  return this.http.get(environment.apiUrl + '/utilities/softphone/check/countryCode/' + dialledNo, httpOptions);
1523
1549
  }
1524
1550
  /**
@@ -1533,6 +1559,21 @@ class TwilioService {
1533
1559
  hasActiveCall() {
1534
1560
  return this._currentCall !== null;
1535
1561
  }
1562
+ /**
1563
+ * Set whether to automatically answer incoming calls
1564
+ * @param autoAnswer - Whether to automatically answer calls
1565
+ */
1566
+ setAutoAnswer(autoAnswer) {
1567
+ this.autoAnswer = autoAnswer;
1568
+ }
1569
+ /**
1570
+ * Toggle auto-answer for incoming calls
1571
+ * @returns The new auto-answer state
1572
+ */
1573
+ toggleAutoAnswer() {
1574
+ this.autoAnswer = !this.autoAnswer;
1575
+ return this.autoAnswer;
1576
+ }
1536
1577
  }
1537
1578
  TwilioService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: TwilioService, deps: [{ token: i1.HttpClient }, { token: ExtensionService }, { token: NotificationService }], target: i0.ɵɵFactoryTarget.Injectable });
1538
1579
  TwilioService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.10", ngImport: i0, type: TwilioService, providedIn: 'root' });
@@ -2163,8 +2204,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.10", ngImpo
2163
2204
  class DialboxComponent {
2164
2205
  set isDialpadHidden(value) {
2165
2206
  this._isDialpadHidden = value;
2166
- if (!value) {
2167
- // When dialpad becomes visible, ensure Twilio is initialized
2207
+ if (!value && !this.isInitialized) {
2208
+ // When dialpad becomes visible, ensure Twilio is initialized if not already
2168
2209
  this.initializeTwilio();
2169
2210
  }
2170
2211
  }
@@ -2226,21 +2267,22 @@ class DialboxComponent {
2226
2267
  this.isSmartDialCall = false;
2227
2268
  this.isInitialized = false;
2228
2269
  this.isMinimised = false;
2229
- // Always initialize Twilio when component loads to receive calls immediately
2230
- this.initializeTwilio();
2270
+ // Initialize Twilio when component loads
2271
+ this.token = localStorage.getItem('ext_token') || '';
2272
+ if (this.token) {
2273
+ this.initializeTwilio();
2274
+ }
2275
+ else {
2276
+ console.warn('No auth token found, Twilio not initialized');
2277
+ }
2231
2278
  }
2232
2279
  initializeTwilio() {
2233
2280
  if (this.isInitialized)
2234
2281
  return;
2235
- this.token = localStorage.getItem('ext_token') || '';
2236
- if (!this.token) {
2237
- console.error('No authentication token found');
2238
- return;
2239
- }
2240
- this.isInitialized = true;
2241
2282
  console.log('Initializing Twilio service...');
2242
2283
  // Initialize Twilio service
2243
2284
  this.twilioService.initializeTwilioDevice();
2285
+ this.isInitialized = true;
2244
2286
  // Check for any existing call first
2245
2287
  const currentCall = this.twilioService.getCurrentCall();
2246
2288
  if (currentCall) {