Skip to content

Commit ab30553

Browse files
authored
Merge pull request #97 from sipgate/refreshtokenHandling
changed order: getToken first, then get/update/create/deleteContact
2 parents 086987a + 5c2905b commit ab30553

File tree

1 file changed

+82
-58
lines changed

1 file changed

+82
-58
lines changed

src/models/controller.model.ts

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ export class Controller {
7070
throw new ServerError(501, "Fetching contacts is not implemented");
7171
}
7272

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+
7381
infoLogger("getContacts", `Fetching contacts…`, providerConfig.apiKey);
7482

7583
const fetchedContacts: Contact[] = await this.adapter.getContacts(
@@ -122,11 +130,6 @@ export class Controller {
122130
res.header("X-Fetching-State", "pending");
123131
}
124132

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-
130133
infoLogger("getContacts", "END", providerConfig.apiKey);
131134
res.status(200).send(responseContacts);
132135
} catch (error: any) {
@@ -154,21 +157,31 @@ export class Controller {
154157
res: Response,
155158
next: NextFunction
156159
): 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+
158166
try {
159-
infoLogger("createContact", "START", apiKey);
167+
infoLogger("createContact", "START", providerConfig.apiKey);
160168

161169
if (!this.adapter.createContact) {
162170
throw new ServerError(501, "Creating contacts is not implemented");
163171
}
164172

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);
167179
}
168-
infoLogger("createContact", "Creating contact", apiKey);
180+
181+
infoLogger("createContact", "Creating contact", providerConfig.apiKey);
169182

170183
const contact: Contact = await this.adapter.createContact(
171-
req.providerConfig,
184+
providerConfig,
172185
req.body
173186
);
174187

@@ -178,7 +191,7 @@ export class Controller {
178191
errorLogger(
179192
"createContact",
180193
"Invalid contact provided by adapter",
181-
apiKey,
194+
providerConfig.apiKey,
182195
this.ajv.errorsText()
183196
);
184197
throw new ServerError(400, "Invalid contact provided by adapter");
@@ -187,25 +200,26 @@ export class Controller {
187200
infoLogger(
188201
"createContact",
189202
`Contact with id ${contact.id} created`,
190-
apiKey
203+
providerConfig.apiKey
191204
);
192205

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+
);
199210
res.status(200).send(sanitizedContact);
200211

201212
if (this.contactCache) {
202-
const contacts = await this.contactCache.get(apiKey);
213+
const contacts = await this.contactCache.get(providerConfig.apiKey);
203214
if (Array.isArray(contacts)) {
204-
await this.contactCache.set(apiKey, [...contacts, sanitizedContact]);
215+
await this.contactCache.set(providerConfig.apiKey, [
216+
...contacts,
217+
sanitizedContact,
218+
]);
205219
}
206220
}
207221

208-
infoLogger("createContact", "END", apiKey);
222+
infoLogger("createContact", "END", providerConfig.apiKey);
209223
} catch (error) {
210224
// prevent logging of refresh errors
211225
if (
@@ -219,10 +233,10 @@ export class Controller {
219233
errorLogger(
220234
"createContact",
221235
"Could not create contact:",
222-
apiKey,
236+
providerConfig.apiKey,
223237
error || "Unknown"
224238
);
225-
errorLogger("createContact", "Entity", apiKey, req.body);
239+
errorLogger("createContact", "Entity", providerConfig.apiKey, req.body);
226240
next(error);
227241
}
228242
}
@@ -232,20 +246,28 @@ export class Controller {
232246
res: Response,
233247
next: NextFunction
234248
): 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+
236254
try {
237255
if (!this.adapter.updateContact) {
238256
throw new ServerError(501, "Updating contacts is not implemented");
239257
}
240258

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);
243265
}
244266

245-
infoLogger("updateContact", "Updating contact", apiKey);
267+
infoLogger("updateContact", "Updating contact", providerConfig.apiKey);
246268

247269
const contact: Contact = await this.adapter.updateContact(
248-
req.providerConfig,
270+
providerConfig,
249271
req.params.id,
250272
req.body
251273
);
@@ -255,7 +277,7 @@ export class Controller {
255277
errorLogger(
256278
"updateContact",
257279
"Invalid contact provided by adapter",
258-
apiKey,
280+
providerConfig.apiKey,
259281
this.ajv.errorsText()
260282
);
261283
throw new ServerError(400, "Invalid contact provided by adapter");
@@ -264,28 +286,26 @@ export class Controller {
264286
infoLogger(
265287
"updateContact",
266288
`Contact with id ${contact.id} updated`,
267-
apiKey
289+
providerConfig.apiKey
268290
);
269291

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+
);
276296
res.status(200).send(sanitizedContact);
277297

278298
if (this.contactCache) {
279-
const contacts = await this.contactCache.get(apiKey);
299+
const contacts = await this.contactCache.get(providerConfig.apiKey);
280300
if (Array.isArray(contacts)) {
281301
const updatedCache: Contact[] = contacts.map((entry) =>
282302
entry.id === sanitizedContact.id ? sanitizedContact : entry
283303
);
284-
await this.contactCache.set(apiKey, updatedCache);
304+
await this.contactCache.set(providerConfig.apiKey, updatedCache);
285305
}
286306
}
287307

288-
infoLogger("updateContact", "END", apiKey);
308+
infoLogger("updateContact", "END", providerConfig.apiKey);
289309
} catch (error) {
290310
// prevent logging of refresh errors
291311
if (
@@ -299,10 +319,10 @@ export class Controller {
299319
errorLogger(
300320
"updateContact",
301321
"Could not update contact:",
302-
apiKey,
322+
providerConfig.apiKey,
303323
error || "Unknown"
304324
);
305-
errorLogger("updateContact", "Entity", apiKey, req.body);
325+
errorLogger("updateContact", "Entity", providerConfig.apiKey, req.body);
306326
next(error);
307327
}
308328
}
@@ -312,46 +332,50 @@ export class Controller {
312332
res: Response,
313333
next: NextFunction
314334
): 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+
316341
try {
317-
infoLogger("deleteContact", "START", apiKey);
342+
infoLogger("deleteContact", "START", providerConfig.apiKey);
318343

319344
if (!this.adapter.deleteContact) {
320345
throw new ServerError(501, "Deleting contacts is not implemented");
321346
}
322347

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-
332348
if (this.adapter.getToken && req.providerConfig) {
349+
infoLogger("getToken", `Fetching token…`, providerConfig.apiKey);
333350
const { apiKey } = await this.adapter.getToken(req.providerConfig);
351+
providerConfig.apiKey = apiKey;
352+
infoLogger("getToken", `Fetched new token…`, providerConfig.apiKey);
334353
res.header("X-Provider-Key", apiKey);
335354
}
355+
356+
infoLogger("deleteContact", "Deleting contact", providerConfig.apiKey);
357+
358+
const contactId = req.params.id;
359+
await this.adapter.deleteContact(providerConfig, contactId);
336360
res.status(200).send();
337361

338362
infoLogger(
339363
"deleteContact",
340364
`Contact with id ${contactId} deleted`,
341-
apiKey
365+
providerConfig.apiKey
342366
);
343367

344368
if (this.contactCache) {
345-
const contacts = await this.contactCache.get(apiKey);
369+
const contacts = await this.contactCache.get(providerConfig.apiKey);
346370
if (Array.isArray(contacts)) {
347371
const updatedCache: Contact[] = contacts.filter(
348372
(entry) => entry.id !== contactId
349373
);
350-
await this.contactCache.set(apiKey, updatedCache);
374+
await this.contactCache.set(providerConfig.apiKey, updatedCache);
351375
}
352376
}
353377

354-
infoLogger("deleteContact", "END", apiKey);
378+
infoLogger("deleteContact", "END", providerConfig.apiKey);
355379
} catch (error) {
356380
// prevent logging of refresh errors
357381
if (
@@ -365,7 +389,7 @@ export class Controller {
365389
errorLogger(
366390
"deleteContact",
367391
"Could not delete contact:",
368-
apiKey,
392+
providerConfig.apiKey,
369393
error || "Unknown"
370394
);
371395
next(error);

0 commit comments

Comments
 (0)