Skip to content

Commit 80bbeae

Browse files
committed
🔊 improve logging
1 parent cbb66d9 commit 80bbeae

File tree

6 files changed

+88
-34
lines changed

6 files changed

+88
-34
lines changed

src/models/adapter.model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Contact,
99
ContactTemplate,
1010
ContactUpdate,
11+
UpdateCallEventBody,
1112
} from ".";
1213

1314
export interface Adapter {
@@ -39,7 +40,7 @@ export interface Adapter {
3940
updateCallEvent?: (
4041
config: Config,
4142
id: string,
42-
event: CallEvent
43+
event: UpdateCallEventBody
4344
) => Promise<void>;
4445
deleteCalendarEvent?: (config: Config, id: string) => Promise<void>;
4546
handleCallEvent?: (config: Config, event: CallEvent) => Promise<string>;

src/models/bridge-request.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Request } from "express";
22
import { Config } from "./config.model";
3+
import { UpdateCallEventBody } from "./call-event.model";
34

45
export interface BridgeRequest extends Request {
56
providerConfig?: Config;
67
}
8+
9+
export interface UpdateCallEventBridgeRequest extends BridgeRequest {
10+
body: UpdateCallEventBody;
11+
}

src/models/call-event.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ export interface CallEvent {
2626
note: string;
2727
state: CallState;
2828
}
29+
30+
export interface UpdateCallEventBody {
31+
note: string;
32+
}

src/models/controller.model.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ContactTemplate,
1313
ContactUpdate,
1414
ServerError,
15+
UpdateCallEventBridgeRequest,
1516
} from ".";
1617
import { calendarEventsSchema, contactsSchema } from "../schemas";
1718
import { anonymizeKey } from "../util/anonymize-key";
@@ -21,6 +22,7 @@ import { validate } from "../util/validate";
2122
import { APIContact } from "./api-contact.model";
2223
import { CacheItemStateType } from "./cache-item-state.model";
2324
import { CalendarFilterOptions } from "./calendar-filter-options.model";
25+
import { errorLogger, infoLogger } from "../util/logger.util";
2426

2527
const CONTACT_FETCH_TIMEOUT: number = 3000;
2628

@@ -50,35 +52,35 @@ export class Controller {
5052
res: Response,
5153
next: NextFunction
5254
): Promise<void> {
53-
const { providerConfig: { apiKey = "", locale = "" } = {} } = req;
54-
const anonKey = anonymizeKey(apiKey);
55+
const { providerConfig } = req;
56+
5557
try {
58+
if (!providerConfig) {
59+
throw new ServerError(400, "Missing parameters");
60+
}
61+
5662
const fetchContacts = async (): Promise<Contact[]> => {
5763
if (!this.adapter.getContacts) {
5864
throw new ServerError(501, "Fetching contacts is not implemented");
5965
}
6066

61-
if (!req.providerConfig) {
62-
throw new ServerError(400, "Missing parameters");
63-
}
64-
65-
console.log(`[${anonKey}] Fetching contacts`);
67+
infoLogger(providerConfig, `Fetching contacts…`);
6668

6769
const fetchedContacts: Contact[] = await this.adapter.getContacts(
68-
req.providerConfig
70+
providerConfig
6971
);
7072

7173
if (!validate(this.ajv, contactsSchema, fetchedContacts)) {
7274
throw new ServerError(500, "Invalid contacts received");
7375
}
7476

7577
return fetchedContacts.map((contact) =>
76-
sanitizeContact(contact, locale)
78+
sanitizeContact(contact, providerConfig.locale)
7779
);
7880
};
7981

8082
const fetcherPromise = this.contactCache
81-
? this.contactCache.get(apiKey, fetchContacts)
83+
? this.contactCache.get(providerConfig.apiKey, fetchContacts)
8284
: fetchContacts();
8385

8486
const timeoutPromise: Promise<"TIMEOUT"> = new Promise((resolve) =>
@@ -87,7 +89,7 @@ export class Controller {
8789

8890
const raceResult = await Promise.race([fetcherPromise, timeoutPromise]);
8991
if (raceResult === "TIMEOUT") {
90-
console.log(`[${anonKey}] fetching too slow, returning empty array`);
92+
infoLogger(providerConfig, `Fetching too slow, returning empty array.`);
9193
}
9294

9395
const responseContacts: Contact[] = Array.isArray(raceResult)
@@ -96,7 +98,7 @@ export class Controller {
9698

9799
const contactsCount = responseContacts.length;
98100

99-
console.log(`[${anonKey}] Found ${contactsCount} cached contacts`);
101+
infoLogger(providerConfig, `Found ${contactsCount} cached contacts.`);
100102

101103
if (
102104
!Array.isArray(raceResult) &&
@@ -113,7 +115,11 @@ export class Controller {
113115

114116
res.status(200).send(responseContacts);
115117
} catch (error) {
116-
console.error("Could not get contacts:", error || "Unknown");
118+
errorLogger(
119+
providerConfig,
120+
"Could not get contacts:",
121+
error || "Unknown"
122+
);
117123
next(error);
118124
}
119125
}
@@ -450,36 +456,40 @@ export class Controller {
450456
res: Response,
451457
next: NextFunction
452458
): Promise<void> {
453-
const { providerConfig: { apiKey = "" } = {} } = req;
459+
const { providerConfig } = req;
460+
454461
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");
457464
}
458465

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");
461468
}
462469

463470
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}`
468474
);
469475
res.status(200).send("Skipping call event");
470476
return;
471477
}
472478

473-
console.log(`[${anonymizeKey(apiKey)}] Handling call event for key`);
479+
infoLogger(providerConfig, `Handling call event`);
474480

475481
const integrationCallEventRef = await this.adapter.handleCallEvent(
476-
req.providerConfig,
482+
providerConfig,
477483
req.body as CallEvent
478484
);
479485

480486
res.status(200).send(integrationCallEventRef);
481487
} 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+
);
483493
next(error);
484494
}
485495
}
@@ -514,11 +524,11 @@ export class Controller {
514524
}
515525

516526
public async updateCallEvent(
517-
req: BridgeRequest,
527+
req: UpdateCallEventBridgeRequest,
518528
res: Response,
519529
next: NextFunction
520530
): Promise<void> {
521-
const { providerConfig: { apiKey = "" } = {} } = req;
531+
const { providerConfig: { apiKey = "" } = {}, body, params } = req;
522532

523533
try {
524534
if (!this.adapter.updateCallEvent) {
@@ -531,12 +541,7 @@ export class Controller {
531541

532542
console.log(`[${anonymizeKey(apiKey)}] Updating call event`);
533543

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);
540545
res.status(200).send();
541546
} catch (error) {
542547
console.error("Could not update call event:", error || "Unknown");

src/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./phone-number-utils";
22
export * from "./anonymize-key";
3+
export * from "./logger.util";

src/util/logger.util.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { anonymizeKey } from "./anonymize-key";
2+
import { Config } from "../models";
3+
4+
export const infoLogger = (
5+
{ apiKey }: Config,
6+
message: string,
7+
...args: unknown[]
8+
): void => {
9+
// eslint-disable-next-line no-console
10+
console.log(
11+
`${anonymizeKey(apiKey)}: ${message}`,
12+
args && args.length ? args : ""
13+
);
14+
};
15+
16+
export const errorLogger = (
17+
config: Config | undefined,
18+
message: string,
19+
...args: unknown[]
20+
): void => {
21+
// eslint-disable-next-line no-console
22+
console.error(
23+
`${config?.apiKey ? anonymizeKey(config.apiKey) : ""}: ${message}`,
24+
args && args.length ? args : ""
25+
);
26+
};
27+
28+
export const warnLogger = (
29+
{ apiKey }: Config,
30+
message: string,
31+
...args: unknown[]
32+
): void => {
33+
// eslint-disable-next-line no-console
34+
console.warn(
35+
`${anonymizeKey(apiKey)}: ${message}`,
36+
args && args.length ? args : ""
37+
);
38+
};

0 commit comments

Comments
 (0)