Skip to content

Commit 5c2905b

Browse files
committed
changed order: getToken first, then get/update/create/deleteContact
1 parent f8dadde commit 5c2905b

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(
@@ -121,11 +129,6 @@ export class Controller {
121129
res.header("X-Fetching-State", "pending");
122130
}
123131

124-
if (this.adapter.getToken && req.providerConfig) {
125-
const { apiKey } = await this.adapter.getToken(req.providerConfig);
126-
res.header("X-Provider-Key", apiKey);
127-
}
128-
129132
infoLogger("getContacts", "END", providerConfig.apiKey);
130133
res.status(200).send(responseContacts);
131134
} catch (error: any) {
@@ -153,21 +156,31 @@ export class Controller {
153156
res: Response,
154157
next: NextFunction
155158
): Promise<void> {
156-
const { providerConfig: { apiKey = "", locale = "" } = {} } = req;
159+
const { providerConfig } = req;
160+
161+
if (!providerConfig) {
162+
throw new ServerError(400, "Missing config parameters");
163+
}
164+
157165
try {
158-
infoLogger("createContact", "START", apiKey);
166+
infoLogger("createContact", "START", providerConfig.apiKey);
159167

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

164-
if (!req.providerConfig) {
165-
throw new ServerError(400, "Missing config parameters");
172+
if (this.adapter.getToken && req.providerConfig) {
173+
infoLogger("getToken", `Fetching token…`, providerConfig.apiKey);
174+
const { apiKey } = await this.adapter.getToken(req.providerConfig);
175+
providerConfig.apiKey = apiKey;
176+
infoLogger("getToken", `Fetched new token…`, providerConfig.apiKey);
177+
res.header("X-Provider-Key", apiKey);
166178
}
167-
infoLogger("createContact", "Creating contact", apiKey);
179+
180+
infoLogger("createContact", "Creating contact", providerConfig.apiKey);
168181

169182
const contact: Contact = await this.adapter.createContact(
170-
req.providerConfig,
183+
providerConfig,
171184
req.body
172185
);
173186

@@ -177,7 +190,7 @@ export class Controller {
177190
errorLogger(
178191
"createContact",
179192
"Invalid contact provided by adapter",
180-
apiKey,
193+
providerConfig.apiKey,
181194
this.ajv.errorsText()
182195
);
183196
throw new ServerError(400, "Invalid contact provided by adapter");
@@ -186,25 +199,26 @@ export class Controller {
186199
infoLogger(
187200
"createContact",
188201
`Contact with id ${contact.id} created`,
189-
apiKey
202+
providerConfig.apiKey
190203
);
191204

192-
const sanitizedContact: Contact = sanitizeContact(contact, locale);
193-
194-
if (this.adapter.getToken && req.providerConfig) {
195-
const { apiKey } = await this.adapter.getToken(req.providerConfig);
196-
res.header("X-Provider-Key", apiKey);
197-
}
205+
const sanitizedContact: Contact = sanitizeContact(
206+
contact,
207+
providerConfig.locale
208+
);
198209
res.status(200).send(sanitizedContact);
199210

200211
if (this.contactCache) {
201-
const contacts = await this.contactCache.get(apiKey);
212+
const contacts = await this.contactCache.get(providerConfig.apiKey);
202213
if (Array.isArray(contacts)) {
203-
await this.contactCache.set(apiKey, [...contacts, sanitizedContact]);
214+
await this.contactCache.set(providerConfig.apiKey, [
215+
...contacts,
216+
sanitizedContact,
217+
]);
204218
}
205219
}
206220

207-
infoLogger("createContact", "END", apiKey);
221+
infoLogger("createContact", "END", providerConfig.apiKey);
208222
} catch (error) {
209223
// prevent logging of refresh errors
210224
if (
@@ -218,10 +232,10 @@ export class Controller {
218232
errorLogger(
219233
"createContact",
220234
"Could not create contact:",
221-
apiKey,
235+
providerConfig.apiKey,
222236
error || "Unknown"
223237
);
224-
errorLogger("createContact", "Entity", apiKey, req.body);
238+
errorLogger("createContact", "Entity", providerConfig.apiKey, req.body);
225239
next(error);
226240
}
227241
}
@@ -231,20 +245,28 @@ export class Controller {
231245
res: Response,
232246
next: NextFunction
233247
): Promise<void> {
234-
const { providerConfig: { apiKey = "", locale = "" } = {} } = req;
248+
const { providerConfig } = req;
249+
if (!providerConfig) {
250+
throw new ServerError(400, "Missing config parameters");
251+
}
252+
235253
try {
236254
if (!this.adapter.updateContact) {
237255
throw new ServerError(501, "Updating contacts is not implemented");
238256
}
239257

240-
if (!req.providerConfig) {
241-
throw new ServerError(400, "Missing config parameters");
258+
if (this.adapter.getToken && req.providerConfig) {
259+
infoLogger("getToken", `Fetching token…`, providerConfig.apiKey);
260+
const { apiKey } = await this.adapter.getToken(req.providerConfig);
261+
providerConfig.apiKey = apiKey;
262+
infoLogger("getToken", `Fetched new token…`, providerConfig.apiKey);
263+
res.header("X-Provider-Key", apiKey);
242264
}
243265

244-
infoLogger("updateContact", "Updating contact", apiKey);
266+
infoLogger("updateContact", "Updating contact", providerConfig.apiKey);
245267

246268
const contact: Contact = await this.adapter.updateContact(
247-
req.providerConfig,
269+
providerConfig,
248270
req.params.id,
249271
req.body
250272
);
@@ -254,7 +276,7 @@ export class Controller {
254276
errorLogger(
255277
"updateContact",
256278
"Invalid contact provided by adapter",
257-
apiKey,
279+
providerConfig.apiKey,
258280
this.ajv.errorsText()
259281
);
260282
throw new ServerError(400, "Invalid contact provided by adapter");
@@ -263,28 +285,26 @@ export class Controller {
263285
infoLogger(
264286
"updateContact",
265287
`Contact with id ${contact.id} updated`,
266-
apiKey
288+
providerConfig.apiKey
267289
);
268290

269-
const sanitizedContact: Contact = sanitizeContact(contact, locale);
270-
271-
if (this.adapter.getToken && req.providerConfig) {
272-
const { apiKey } = await this.adapter.getToken(req.providerConfig);
273-
res.header("X-Provider-Key", apiKey);
274-
}
291+
const sanitizedContact: Contact = sanitizeContact(
292+
contact,
293+
providerConfig.locale
294+
);
275295
res.status(200).send(sanitizedContact);
276296

277297
if (this.contactCache) {
278-
const contacts = await this.contactCache.get(apiKey);
298+
const contacts = await this.contactCache.get(providerConfig.apiKey);
279299
if (Array.isArray(contacts)) {
280300
const updatedCache: Contact[] = contacts.map((entry) =>
281301
entry.id === sanitizedContact.id ? sanitizedContact : entry
282302
);
283-
await this.contactCache.set(apiKey, updatedCache);
303+
await this.contactCache.set(providerConfig.apiKey, updatedCache);
284304
}
285305
}
286306

287-
infoLogger("updateContact", "END", apiKey);
307+
infoLogger("updateContact", "END", providerConfig.apiKey);
288308
} catch (error) {
289309
// prevent logging of refresh errors
290310
if (
@@ -298,10 +318,10 @@ export class Controller {
298318
errorLogger(
299319
"updateContact",
300320
"Could not update contact:",
301-
apiKey,
321+
providerConfig.apiKey,
302322
error || "Unknown"
303323
);
304-
errorLogger("updateContact", "Entity", apiKey, req.body);
324+
errorLogger("updateContact", "Entity", providerConfig.apiKey, req.body);
305325
next(error);
306326
}
307327
}
@@ -311,46 +331,50 @@ export class Controller {
311331
res: Response,
312332
next: NextFunction
313333
): Promise<void> {
314-
const { providerConfig: { apiKey = "" } = {} } = req;
334+
const { providerConfig } = req;
335+
336+
if (!providerConfig) {
337+
throw new ServerError(400, "Missing config parameters");
338+
}
339+
315340
try {
316-
infoLogger("deleteContact", "START", apiKey);
341+
infoLogger("deleteContact", "START", providerConfig.apiKey);
317342

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

322-
if (!req.providerConfig) {
323-
throw new ServerError(400, "Missing config parameters");
324-
}
325-
326-
infoLogger("deleteContact", "Deleting contact", apiKey);
327-
328-
const contactId = req.params.id;
329-
await this.adapter.deleteContact(req.providerConfig, contactId);
330-
331347
if (this.adapter.getToken && req.providerConfig) {
348+
infoLogger("getToken", `Fetching token…`, providerConfig.apiKey);
332349
const { apiKey } = await this.adapter.getToken(req.providerConfig);
350+
providerConfig.apiKey = apiKey;
351+
infoLogger("getToken", `Fetched new token…`, providerConfig.apiKey);
333352
res.header("X-Provider-Key", apiKey);
334353
}
354+
355+
infoLogger("deleteContact", "Deleting contact", providerConfig.apiKey);
356+
357+
const contactId = req.params.id;
358+
await this.adapter.deleteContact(providerConfig, contactId);
335359
res.status(200).send();
336360

337361
infoLogger(
338362
"deleteContact",
339363
`Contact with id ${contactId} deleted`,
340-
apiKey
364+
providerConfig.apiKey
341365
);
342366

343367
if (this.contactCache) {
344-
const contacts = await this.contactCache.get(apiKey);
368+
const contacts = await this.contactCache.get(providerConfig.apiKey);
345369
if (Array.isArray(contacts)) {
346370
const updatedCache: Contact[] = contacts.filter(
347371
(entry) => entry.id !== contactId
348372
);
349-
await this.contactCache.set(apiKey, updatedCache);
373+
await this.contactCache.set(providerConfig.apiKey, updatedCache);
350374
}
351375
}
352376

353-
infoLogger("deleteContact", "END", apiKey);
377+
infoLogger("deleteContact", "END", providerConfig.apiKey);
354378
} catch (error) {
355379
// prevent logging of refresh errors
356380
if (
@@ -364,7 +388,7 @@ export class Controller {
364388
errorLogger(
365389
"deleteContact",
366390
"Could not delete contact:",
367-
apiKey,
391+
providerConfig.apiKey,
368392
error || "Unknown"
369393
);
370394
next(error);

0 commit comments

Comments
 (0)