Skip to content

Commit 03990e6

Browse files
committed
Revert "changed order: getToken first, then get/update/create/deleteContact"
This reverts commit 5c2905b.
1 parent 5896fc4 commit 03990e6

File tree

1 file changed

+58
-82
lines changed

1 file changed

+58
-82
lines changed

src/models/controller.model.ts

Lines changed: 58 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,6 @@ 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-
8173
infoLogger("getContacts", `Fetching contacts…`, providerConfig.apiKey);
8274

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

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+
133130
infoLogger("getContacts", "END", providerConfig.apiKey);
134131
res.status(200).send(responseContacts);
135132
} catch (error: any) {
@@ -157,31 +154,21 @@ export class Controller {
157154
res: Response,
158155
next: NextFunction
159156
): Promise<void> {
160-
const { providerConfig } = req;
161-
162-
if (!providerConfig) {
163-
throw new ServerError(400, "Missing config parameters");
164-
}
165-
157+
const { providerConfig: { apiKey = "", locale = "" } = {} } = req;
166158
try {
167-
infoLogger("createContact", "START", providerConfig.apiKey);
159+
infoLogger("createContact", "START", apiKey);
168160

169161
if (!this.adapter.createContact) {
170162
throw new ServerError(501, "Creating contacts is not implemented");
171163
}
172164

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);
165+
if (!req.providerConfig) {
166+
throw new ServerError(400, "Missing config parameters");
179167
}
180-
181-
infoLogger("createContact", "Creating contact", providerConfig.apiKey);
168+
infoLogger("createContact", "Creating contact", apiKey);
182169

183170
const contact: Contact = await this.adapter.createContact(
184-
providerConfig,
171+
req.providerConfig,
185172
req.body
186173
);
187174

@@ -191,7 +178,7 @@ export class Controller {
191178
errorLogger(
192179
"createContact",
193180
"Invalid contact provided by adapter",
194-
providerConfig.apiKey,
181+
apiKey,
195182
this.ajv.errorsText()
196183
);
197184
throw new ServerError(400, "Invalid contact provided by adapter");
@@ -200,26 +187,25 @@ export class Controller {
200187
infoLogger(
201188
"createContact",
202189
`Contact with id ${contact.id} created`,
203-
providerConfig.apiKey
190+
apiKey
204191
);
205192

206-
const sanitizedContact: Contact = sanitizeContact(
207-
contact,
208-
providerConfig.locale
209-
);
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+
}
210199
res.status(200).send(sanitizedContact);
211200

212201
if (this.contactCache) {
213-
const contacts = await this.contactCache.get(providerConfig.apiKey);
202+
const contacts = await this.contactCache.get(apiKey);
214203
if (Array.isArray(contacts)) {
215-
await this.contactCache.set(providerConfig.apiKey, [
216-
...contacts,
217-
sanitizedContact,
218-
]);
204+
await this.contactCache.set(apiKey, [...contacts, sanitizedContact]);
219205
}
220206
}
221207

222-
infoLogger("createContact", "END", providerConfig.apiKey);
208+
infoLogger("createContact", "END", apiKey);
223209
} catch (error) {
224210
// prevent logging of refresh errors
225211
if (
@@ -233,10 +219,10 @@ export class Controller {
233219
errorLogger(
234220
"createContact",
235221
"Could not create contact:",
236-
providerConfig.apiKey,
222+
apiKey,
237223
error || "Unknown"
238224
);
239-
errorLogger("createContact", "Entity", providerConfig.apiKey, req.body);
225+
errorLogger("createContact", "Entity", apiKey, req.body);
240226
next(error);
241227
}
242228
}
@@ -246,28 +232,20 @@ export class Controller {
246232
res: Response,
247233
next: NextFunction
248234
): Promise<void> {
249-
const { providerConfig } = req;
250-
if (!providerConfig) {
251-
throw new ServerError(400, "Missing config parameters");
252-
}
253-
235+
const { providerConfig: { apiKey = "", locale = "" } = {} } = req;
254236
try {
255237
if (!this.adapter.updateContact) {
256238
throw new ServerError(501, "Updating contacts is not implemented");
257239
}
258240

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);
241+
if (!req.providerConfig) {
242+
throw new ServerError(400, "Missing config parameters");
265243
}
266244

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

269247
const contact: Contact = await this.adapter.updateContact(
270-
providerConfig,
248+
req.providerConfig,
271249
req.params.id,
272250
req.body
273251
);
@@ -277,7 +255,7 @@ export class Controller {
277255
errorLogger(
278256
"updateContact",
279257
"Invalid contact provided by adapter",
280-
providerConfig.apiKey,
258+
apiKey,
281259
this.ajv.errorsText()
282260
);
283261
throw new ServerError(400, "Invalid contact provided by adapter");
@@ -286,26 +264,28 @@ export class Controller {
286264
infoLogger(
287265
"updateContact",
288266
`Contact with id ${contact.id} updated`,
289-
providerConfig.apiKey
267+
apiKey
290268
);
291269

292-
const sanitizedContact: Contact = sanitizeContact(
293-
contact,
294-
providerConfig.locale
295-
);
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+
}
296276
res.status(200).send(sanitizedContact);
297277

298278
if (this.contactCache) {
299-
const contacts = await this.contactCache.get(providerConfig.apiKey);
279+
const contacts = await this.contactCache.get(apiKey);
300280
if (Array.isArray(contacts)) {
301281
const updatedCache: Contact[] = contacts.map((entry) =>
302282
entry.id === sanitizedContact.id ? sanitizedContact : entry
303283
);
304-
await this.contactCache.set(providerConfig.apiKey, updatedCache);
284+
await this.contactCache.set(apiKey, updatedCache);
305285
}
306286
}
307287

308-
infoLogger("updateContact", "END", providerConfig.apiKey);
288+
infoLogger("updateContact", "END", apiKey);
309289
} catch (error) {
310290
// prevent logging of refresh errors
311291
if (
@@ -319,10 +299,10 @@ export class Controller {
319299
errorLogger(
320300
"updateContact",
321301
"Could not update contact:",
322-
providerConfig.apiKey,
302+
apiKey,
323303
error || "Unknown"
324304
);
325-
errorLogger("updateContact", "Entity", providerConfig.apiKey, req.body);
305+
errorLogger("updateContact", "Entity", apiKey, req.body);
326306
next(error);
327307
}
328308
}
@@ -332,50 +312,46 @@ export class Controller {
332312
res: Response,
333313
next: NextFunction
334314
): Promise<void> {
335-
const { providerConfig } = req;
336-
337-
if (!providerConfig) {
338-
throw new ServerError(400, "Missing config parameters");
339-
}
340-
315+
const { providerConfig: { apiKey = "" } = {} } = req;
341316
try {
342-
infoLogger("deleteContact", "START", providerConfig.apiKey);
317+
infoLogger("deleteContact", "START", apiKey);
343318

344319
if (!this.adapter.deleteContact) {
345320
throw new ServerError(501, "Deleting contacts is not implemented");
346321
}
347322

348-
if (this.adapter.getToken && req.providerConfig) {
349-
infoLogger("getToken", `Fetching token…`, providerConfig.apiKey);
350-
const { apiKey } = await this.adapter.getToken(req.providerConfig);
351-
providerConfig.apiKey = apiKey;
352-
infoLogger("getToken", `Fetched new token…`, providerConfig.apiKey);
353-
res.header("X-Provider-Key", apiKey);
323+
if (!req.providerConfig) {
324+
throw new ServerError(400, "Missing config parameters");
354325
}
355326

356-
infoLogger("deleteContact", "Deleting contact", providerConfig.apiKey);
327+
infoLogger("deleteContact", "Deleting contact", apiKey);
357328

358329
const contactId = req.params.id;
359-
await this.adapter.deleteContact(providerConfig, contactId);
330+
await this.adapter.deleteContact(req.providerConfig, contactId);
331+
332+
if (this.adapter.getToken && req.providerConfig) {
333+
const { apiKey } = await this.adapter.getToken(req.providerConfig);
334+
res.header("X-Provider-Key", apiKey);
335+
}
360336
res.status(200).send();
361337

362338
infoLogger(
363339
"deleteContact",
364340
`Contact with id ${contactId} deleted`,
365-
providerConfig.apiKey
341+
apiKey
366342
);
367343

368344
if (this.contactCache) {
369-
const contacts = await this.contactCache.get(providerConfig.apiKey);
345+
const contacts = await this.contactCache.get(apiKey);
370346
if (Array.isArray(contacts)) {
371347
const updatedCache: Contact[] = contacts.filter(
372348
(entry) => entry.id !== contactId
373349
);
374-
await this.contactCache.set(providerConfig.apiKey, updatedCache);
350+
await this.contactCache.set(apiKey, updatedCache);
375351
}
376352
}
377353

378-
infoLogger("deleteContact", "END", providerConfig.apiKey);
354+
infoLogger("deleteContact", "END", apiKey);
379355
} catch (error) {
380356
// prevent logging of refresh errors
381357
if (
@@ -389,7 +365,7 @@ export class Controller {
389365
errorLogger(
390366
"deleteContact",
391367
"Could not delete contact:",
392-
providerConfig.apiKey,
368+
apiKey,
393369
error || "Unknown"
394370
);
395371
next(error);

0 commit comments

Comments
 (0)