@@ -70,6 +70,14 @@ export class Controller {
70
70
throw new ServerError ( 501 , "Fetching contacts is not implemented" ) ;
71
71
}
72
72
73
+ if ( this . adapter . getToken && req . providerConfig ) {
74
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
75
+ const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
76
+ providerConfig . apiKey = apiKey ;
77
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
78
+ res . header ( "X-Provider-Key" , apiKey ) ;
79
+ }
80
+
73
81
infoLogger ( "getContacts" , `Fetching contacts…` , providerConfig . apiKey ) ;
74
82
75
83
const fetchedContacts : Contact [ ] = await this . adapter . getContacts (
@@ -122,11 +130,6 @@ export class Controller {
122
130
res . header ( "X-Fetching-State" , "pending" ) ;
123
131
}
124
132
125
- if ( this . adapter . getToken && req . providerConfig ) {
126
- const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
127
- res . header ( "X-Provider-Key" , apiKey ) ;
128
- }
129
-
130
133
infoLogger ( "getContacts" , "END" , providerConfig . apiKey ) ;
131
134
res . status ( 200 ) . send ( responseContacts ) ;
132
135
} catch ( error : any ) {
@@ -154,21 +157,31 @@ export class Controller {
154
157
res : Response ,
155
158
next : NextFunction
156
159
) : Promise < void > {
157
- const { providerConfig : { apiKey = "" , locale = "" } = { } } = req ;
160
+ const { providerConfig } = req ;
161
+
162
+ if ( ! providerConfig ) {
163
+ throw new ServerError ( 400 , "Missing config parameters" ) ;
164
+ }
165
+
158
166
try {
159
- infoLogger ( "createContact" , "START" , apiKey ) ;
167
+ infoLogger ( "createContact" , "START" , providerConfig . apiKey ) ;
160
168
161
169
if ( ! this . adapter . createContact ) {
162
170
throw new ServerError ( 501 , "Creating contacts is not implemented" ) ;
163
171
}
164
172
165
- if ( ! req . providerConfig ) {
166
- throw new ServerError ( 400 , "Missing config parameters" ) ;
173
+ if ( this . adapter . getToken && req . providerConfig ) {
174
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
175
+ const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
176
+ providerConfig . apiKey = apiKey ;
177
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
178
+ res . header ( "X-Provider-Key" , apiKey ) ;
167
179
}
168
- infoLogger ( "createContact" , "Creating contact" , apiKey ) ;
180
+
181
+ infoLogger ( "createContact" , "Creating contact" , providerConfig . apiKey ) ;
169
182
170
183
const contact : Contact = await this . adapter . createContact (
171
- req . providerConfig ,
184
+ providerConfig ,
172
185
req . body
173
186
) ;
174
187
@@ -178,7 +191,7 @@ export class Controller {
178
191
errorLogger (
179
192
"createContact" ,
180
193
"Invalid contact provided by adapter" ,
181
- apiKey ,
194
+ providerConfig . apiKey ,
182
195
this . ajv . errorsText ( )
183
196
) ;
184
197
throw new ServerError ( 400 , "Invalid contact provided by adapter" ) ;
@@ -187,25 +200,26 @@ export class Controller {
187
200
infoLogger (
188
201
"createContact" ,
189
202
`Contact with id ${ contact . id } created` ,
190
- apiKey
203
+ providerConfig . apiKey
191
204
) ;
192
205
193
- const sanitizedContact : Contact = sanitizeContact ( contact , locale ) ;
194
-
195
- if ( this . adapter . getToken && req . providerConfig ) {
196
- const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
197
- res . header ( "X-Provider-Key" , apiKey ) ;
198
- }
206
+ const sanitizedContact : Contact = sanitizeContact (
207
+ contact ,
208
+ providerConfig . locale
209
+ ) ;
199
210
res . status ( 200 ) . send ( sanitizedContact ) ;
200
211
201
212
if ( this . contactCache ) {
202
- const contacts = await this . contactCache . get ( apiKey ) ;
213
+ const contacts = await this . contactCache . get ( providerConfig . apiKey ) ;
203
214
if ( Array . isArray ( contacts ) ) {
204
- await this . contactCache . set ( apiKey , [ ...contacts , sanitizedContact ] ) ;
215
+ await this . contactCache . set ( providerConfig . apiKey , [
216
+ ...contacts ,
217
+ sanitizedContact ,
218
+ ] ) ;
205
219
}
206
220
}
207
221
208
- infoLogger ( "createContact" , "END" , apiKey ) ;
222
+ infoLogger ( "createContact" , "END" , providerConfig . apiKey ) ;
209
223
} catch ( error ) {
210
224
// prevent logging of refresh errors
211
225
if (
@@ -219,10 +233,10 @@ export class Controller {
219
233
errorLogger (
220
234
"createContact" ,
221
235
"Could not create contact:" ,
222
- apiKey ,
236
+ providerConfig . apiKey ,
223
237
error || "Unknown"
224
238
) ;
225
- errorLogger ( "createContact" , "Entity" , apiKey , req . body ) ;
239
+ errorLogger ( "createContact" , "Entity" , providerConfig . apiKey , req . body ) ;
226
240
next ( error ) ;
227
241
}
228
242
}
@@ -232,20 +246,28 @@ export class Controller {
232
246
res : Response ,
233
247
next : NextFunction
234
248
) : Promise < void > {
235
- const { providerConfig : { apiKey = "" , locale = "" } = { } } = req ;
249
+ const { providerConfig } = req ;
250
+ if ( ! providerConfig ) {
251
+ throw new ServerError ( 400 , "Missing config parameters" ) ;
252
+ }
253
+
236
254
try {
237
255
if ( ! this . adapter . updateContact ) {
238
256
throw new ServerError ( 501 , "Updating contacts is not implemented" ) ;
239
257
}
240
258
241
- if ( ! req . providerConfig ) {
242
- throw new ServerError ( 400 , "Missing config parameters" ) ;
259
+ if ( this . adapter . getToken && req . providerConfig ) {
260
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
261
+ const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
262
+ providerConfig . apiKey = apiKey ;
263
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
264
+ res . header ( "X-Provider-Key" , apiKey ) ;
243
265
}
244
266
245
- infoLogger ( "updateContact" , "Updating contact" , apiKey ) ;
267
+ infoLogger ( "updateContact" , "Updating contact" , providerConfig . apiKey ) ;
246
268
247
269
const contact : Contact = await this . adapter . updateContact (
248
- req . providerConfig ,
270
+ providerConfig ,
249
271
req . params . id ,
250
272
req . body
251
273
) ;
@@ -255,7 +277,7 @@ export class Controller {
255
277
errorLogger (
256
278
"updateContact" ,
257
279
"Invalid contact provided by adapter" ,
258
- apiKey ,
280
+ providerConfig . apiKey ,
259
281
this . ajv . errorsText ( )
260
282
) ;
261
283
throw new ServerError ( 400 , "Invalid contact provided by adapter" ) ;
@@ -264,28 +286,26 @@ export class Controller {
264
286
infoLogger (
265
287
"updateContact" ,
266
288
`Contact with id ${ contact . id } updated` ,
267
- apiKey
289
+ providerConfig . apiKey
268
290
) ;
269
291
270
- const sanitizedContact : Contact = sanitizeContact ( contact , locale ) ;
271
-
272
- if ( this . adapter . getToken && req . providerConfig ) {
273
- const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
274
- res . header ( "X-Provider-Key" , apiKey ) ;
275
- }
292
+ const sanitizedContact : Contact = sanitizeContact (
293
+ contact ,
294
+ providerConfig . locale
295
+ ) ;
276
296
res . status ( 200 ) . send ( sanitizedContact ) ;
277
297
278
298
if ( this . contactCache ) {
279
- const contacts = await this . contactCache . get ( apiKey ) ;
299
+ const contacts = await this . contactCache . get ( providerConfig . apiKey ) ;
280
300
if ( Array . isArray ( contacts ) ) {
281
301
const updatedCache : Contact [ ] = contacts . map ( ( entry ) =>
282
302
entry . id === sanitizedContact . id ? sanitizedContact : entry
283
303
) ;
284
- await this . contactCache . set ( apiKey , updatedCache ) ;
304
+ await this . contactCache . set ( providerConfig . apiKey , updatedCache ) ;
285
305
}
286
306
}
287
307
288
- infoLogger ( "updateContact" , "END" , apiKey ) ;
308
+ infoLogger ( "updateContact" , "END" , providerConfig . apiKey ) ;
289
309
} catch ( error ) {
290
310
// prevent logging of refresh errors
291
311
if (
@@ -299,10 +319,10 @@ export class Controller {
299
319
errorLogger (
300
320
"updateContact" ,
301
321
"Could not update contact:" ,
302
- apiKey ,
322
+ providerConfig . apiKey ,
303
323
error || "Unknown"
304
324
) ;
305
- errorLogger ( "updateContact" , "Entity" , apiKey , req . body ) ;
325
+ errorLogger ( "updateContact" , "Entity" , providerConfig . apiKey , req . body ) ;
306
326
next ( error ) ;
307
327
}
308
328
}
@@ -312,46 +332,50 @@ export class Controller {
312
332
res : Response ,
313
333
next : NextFunction
314
334
) : Promise < void > {
315
- const { providerConfig : { apiKey = "" } = { } } = req ;
335
+ const { providerConfig } = req ;
336
+
337
+ if ( ! providerConfig ) {
338
+ throw new ServerError ( 400 , "Missing config parameters" ) ;
339
+ }
340
+
316
341
try {
317
- infoLogger ( "deleteContact" , "START" , apiKey ) ;
342
+ infoLogger ( "deleteContact" , "START" , providerConfig . apiKey ) ;
318
343
319
344
if ( ! this . adapter . deleteContact ) {
320
345
throw new ServerError ( 501 , "Deleting contacts is not implemented" ) ;
321
346
}
322
347
323
- if ( ! req . providerConfig ) {
324
- throw new ServerError ( 400 , "Missing config parameters" ) ;
325
- }
326
-
327
- infoLogger ( "deleteContact" , "Deleting contact" , apiKey ) ;
328
-
329
- const contactId = req . params . id ;
330
- await this . adapter . deleteContact ( req . providerConfig , contactId ) ;
331
-
332
348
if ( this . adapter . getToken && req . providerConfig ) {
349
+ infoLogger ( "getToken" , `Fetching token…` , providerConfig . apiKey ) ;
333
350
const { apiKey } = await this . adapter . getToken ( req . providerConfig ) ;
351
+ providerConfig . apiKey = apiKey ;
352
+ infoLogger ( "getToken" , `Fetched new token…` , providerConfig . apiKey ) ;
334
353
res . header ( "X-Provider-Key" , apiKey ) ;
335
354
}
355
+
356
+ infoLogger ( "deleteContact" , "Deleting contact" , providerConfig . apiKey ) ;
357
+
358
+ const contactId = req . params . id ;
359
+ await this . adapter . deleteContact ( providerConfig , contactId ) ;
336
360
res . status ( 200 ) . send ( ) ;
337
361
338
362
infoLogger (
339
363
"deleteContact" ,
340
364
`Contact with id ${ contactId } deleted` ,
341
- apiKey
365
+ providerConfig . apiKey
342
366
) ;
343
367
344
368
if ( this . contactCache ) {
345
- const contacts = await this . contactCache . get ( apiKey ) ;
369
+ const contacts = await this . contactCache . get ( providerConfig . apiKey ) ;
346
370
if ( Array . isArray ( contacts ) ) {
347
371
const updatedCache : Contact [ ] = contacts . filter (
348
372
( entry ) => entry . id !== contactId
349
373
) ;
350
- await this . contactCache . set ( apiKey , updatedCache ) ;
374
+ await this . contactCache . set ( providerConfig . apiKey , updatedCache ) ;
351
375
}
352
376
}
353
377
354
- infoLogger ( "deleteContact" , "END" , apiKey ) ;
378
+ infoLogger ( "deleteContact" , "END" , providerConfig . apiKey ) ;
355
379
} catch ( error ) {
356
380
// prevent logging of refresh errors
357
381
if (
@@ -365,7 +389,7 @@ export class Controller {
365
389
errorLogger (
366
390
"deleteContact" ,
367
391
"Could not delete contact:" ,
368
- apiKey ,
392
+ providerConfig . apiKey ,
369
393
error || "Unknown"
370
394
) ;
371
395
next ( error ) ;
0 commit comments