@@ -12,6 +12,7 @@ import {
12
12
ContactTemplate ,
13
13
ContactUpdate ,
14
14
ServerError ,
15
+ UpdateCallEventBridgeRequest ,
15
16
} from "." ;
16
17
import { calendarEventsSchema , contactsSchema } from "../schemas" ;
17
18
import { anonymizeKey } from "../util/anonymize-key" ;
@@ -21,6 +22,7 @@ import { validate } from "../util/validate";
21
22
import { APIContact } from "./api-contact.model" ;
22
23
import { CacheItemStateType } from "./cache-item-state.model" ;
23
24
import { CalendarFilterOptions } from "./calendar-filter-options.model" ;
25
+ import { errorLogger , infoLogger } from "../util/logger.util" ;
24
26
25
27
const CONTACT_FETCH_TIMEOUT : number = 3000 ;
26
28
@@ -50,35 +52,35 @@ export class Controller {
50
52
res : Response ,
51
53
next : NextFunction
52
54
) : Promise < void > {
53
- const { providerConfig : { apiKey = "" , locale = "" } = { } } = req ;
54
- const anonKey = anonymizeKey ( apiKey ) ;
55
+ const { providerConfig } = req ;
56
+
55
57
try {
58
+ if ( ! providerConfig ) {
59
+ throw new ServerError ( 400 , "Missing parameters" ) ;
60
+ }
61
+
56
62
const fetchContacts = async ( ) : Promise < Contact [ ] > => {
57
63
if ( ! this . adapter . getContacts ) {
58
64
throw new ServerError ( 501 , "Fetching contacts is not implemented" ) ;
59
65
}
60
66
61
- if ( ! req . providerConfig ) {
62
- throw new ServerError ( 400 , "Missing parameters" ) ;
63
- }
64
-
65
- console . log ( `[${ anonKey } ] Fetching contacts` ) ;
67
+ infoLogger ( providerConfig , `Fetching contacts…` ) ;
66
68
67
69
const fetchedContacts : Contact [ ] = await this . adapter . getContacts (
68
- req . providerConfig
70
+ providerConfig
69
71
) ;
70
72
71
73
if ( ! validate ( this . ajv , contactsSchema , fetchedContacts ) ) {
72
74
throw new ServerError ( 500 , "Invalid contacts received" ) ;
73
75
}
74
76
75
77
return fetchedContacts . map ( ( contact ) =>
76
- sanitizeContact ( contact , locale )
78
+ sanitizeContact ( contact , providerConfig . locale )
77
79
) ;
78
80
} ;
79
81
80
82
const fetcherPromise = this . contactCache
81
- ? this . contactCache . get ( apiKey , fetchContacts )
83
+ ? this . contactCache . get ( providerConfig . apiKey , fetchContacts )
82
84
: fetchContacts ( ) ;
83
85
84
86
const timeoutPromise : Promise < "TIMEOUT" > = new Promise ( ( resolve ) =>
@@ -87,7 +89,7 @@ export class Controller {
87
89
88
90
const raceResult = await Promise . race ( [ fetcherPromise , timeoutPromise ] ) ;
89
91
if ( raceResult === "TIMEOUT" ) {
90
- console . log ( `[ ${ anonKey } ] fetching too slow, returning empty array`) ;
92
+ infoLogger ( providerConfig , `Fetching too slow, returning empty array. `) ;
91
93
}
92
94
93
95
const responseContacts : Contact [ ] = Array . isArray ( raceResult )
@@ -96,7 +98,7 @@ export class Controller {
96
98
97
99
const contactsCount = responseContacts . length ;
98
100
99
- console . log ( `[ ${ anonKey } ] Found ${ contactsCount } cached contacts`) ;
101
+ infoLogger ( providerConfig , ` Found ${ contactsCount } cached contacts. `) ;
100
102
101
103
if (
102
104
! Array . isArray ( raceResult ) &&
@@ -113,7 +115,11 @@ export class Controller {
113
115
114
116
res . status ( 200 ) . send ( responseContacts ) ;
115
117
} catch ( error ) {
116
- console . error ( "Could not get contacts:" , error || "Unknown" ) ;
118
+ errorLogger (
119
+ providerConfig ,
120
+ "Could not get contacts:" ,
121
+ error || "Unknown"
122
+ ) ;
117
123
next ( error ) ;
118
124
}
119
125
}
@@ -450,36 +456,40 @@ export class Controller {
450
456
res : Response ,
451
457
next : NextFunction
452
458
) : Promise < void > {
453
- const { providerConfig : { apiKey = "" } = { } } = req ;
459
+ const { providerConfig } = req ;
460
+
454
461
try {
455
- if ( ! this . adapter . handleCallEvent ) {
456
- throw new ServerError ( 501 , "Handling call event is not implemented " ) ;
462
+ if ( ! providerConfig ) {
463
+ throw new ServerError ( 400 , "Missing config parameters " ) ;
457
464
}
458
465
459
- if ( ! req . providerConfig ) {
460
- throw new ServerError ( 400 , "Missing config parameters " ) ;
466
+ if ( ! this . adapter . handleCallEvent ) {
467
+ throw new ServerError ( 501 , "Handling call event is not implemented " ) ;
461
468
}
462
469
463
470
if ( shouldSkipCallEvent ( req . body as CallEvent ) ) {
464
- console . log (
465
- `[${ anonymizeKey ( apiKey ) } ] skipping call event for call id ${
466
- req . body . id
467
- } `
471
+ infoLogger (
472
+ providerConfig ,
473
+ `Skipping call event for call id ${ req . body . id } `
468
474
) ;
469
475
res . status ( 200 ) . send ( "Skipping call event" ) ;
470
476
return ;
471
477
}
472
478
473
- console . log ( `[ ${ anonymizeKey ( apiKey ) } ] Handling call event for key `) ;
479
+ infoLogger ( providerConfig , ` Handling call event`) ;
474
480
475
481
const integrationCallEventRef = await this . adapter . handleCallEvent (
476
- req . providerConfig ,
482
+ providerConfig ,
477
483
req . body as CallEvent
478
484
) ;
479
485
480
486
res . status ( 200 ) . send ( integrationCallEventRef ) ;
481
487
} catch ( error ) {
482
- console . error ( "Could not handle call event:" , error || "Unknown" ) ;
488
+ errorLogger (
489
+ providerConfig ,
490
+ "Could not handle call event:" ,
491
+ error || "Unknown"
492
+ ) ;
483
493
next ( error ) ;
484
494
}
485
495
}
@@ -514,11 +524,11 @@ export class Controller {
514
524
}
515
525
516
526
public async updateCallEvent (
517
- req : BridgeRequest ,
527
+ req : UpdateCallEventBridgeRequest ,
518
528
res : Response ,
519
529
next : NextFunction
520
530
) : Promise < void > {
521
- const { providerConfig : { apiKey = "" } = { } } = req ;
531
+ const { providerConfig : { apiKey = "" } = { } , body , params } = req ;
522
532
523
533
try {
524
534
if ( ! this . adapter . updateCallEvent ) {
@@ -531,12 +541,7 @@ export class Controller {
531
541
532
542
console . log ( `[${ anonymizeKey ( apiKey ) } ] Updating call event` ) ;
533
543
534
- //maybe return updated state obj
535
- await this . adapter . updateCallEvent (
536
- req . providerConfig ,
537
- req . params . id ,
538
- req . body as CallEvent
539
- ) ;
544
+ await this . adapter . updateCallEvent ( req . providerConfig , params . id , body ) ;
540
545
res . status ( 200 ) . send ( ) ;
541
546
} catch ( error ) {
542
547
console . error ( "Could not update call event:" , error || "Unknown" ) ;
0 commit comments