Skip to content

Commit 67224f4

Browse files
committed
0.13.32 added optional parameters req, res for getContacts, createContct, updateContact and deleteContact
1 parent f263359 commit 67224f4

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sipgate/integration-bridge",
3-
"version": "0.13.31",
3+
"version": "0.13.32",
44
"description": "sipgate Integration Bridge Framework",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/models/adapter.model.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,30 @@ import {
1313

1414
export interface Adapter {
1515
getToken?: (config: Config) => Promise<{ apiKey: string }>;
16-
getContacts?: (config: Config) => Promise<Contact[]>;
16+
getContacts?: (
17+
config: Config,
18+
req?: Request,
19+
res?: Response
20+
) => Promise<Contact[]>;
1721
createContact?: (
1822
config: Config,
19-
contact: ContactTemplate
23+
contact: ContactTemplate,
24+
req?: Request,
25+
res?: Response
2026
) => Promise<Contact>;
2127
updateContact?: (
2228
config: Config,
2329
id: string,
24-
contact: ContactUpdate
30+
contact: ContactUpdate,
31+
req?: Request,
32+
res?: Response
2533
) => Promise<Contact>;
26-
deleteContact?: (config: Config, id: string) => Promise<void>;
34+
deleteContact?: (
35+
config: Config,
36+
id: string,
37+
req?: Request,
38+
res?: Response
39+
) => Promise<void>;
2740
getCalendarEvents?: (
2841
config: Config,
2942
options?: CalendarFilterOptions | null

src/models/controller.model.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,22 @@ export class Controller {
6767
infoLogger(`Fetching contacts…`, providerConfig);
6868

6969
const fetchedContacts: Contact[] = await this.adapter.getContacts(
70-
providerConfig
70+
providerConfig,
71+
req,
72+
res
7173
);
7274

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

7779
return fetchedContacts.map((contact) =>
78-
sanitizeContact(contact, providerConfig.locale)
80+
sanitizeContact(contact, locale)
7981
);
8082
};
8183

8284
const fetcherPromise = this.contactCache
83-
? this.contactCache.get(providerConfig.apiKey, fetchContacts)
85+
? this.contactCache.get(apiKey, fetchContacts)
8486
: fetchContacts();
8587

8688
const timeoutPromise: Promise<"TIMEOUT"> = new Promise((resolve) =>
@@ -89,7 +91,7 @@ export class Controller {
8991

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

9597
const responseContacts: Contact[] = Array.isArray(raceResult)
@@ -98,7 +100,7 @@ export class Controller {
98100

99101
const contactsCount = responseContacts.length;
100102

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

103105
if (
104106
!Array.isArray(raceResult) &&
@@ -115,11 +117,7 @@ export class Controller {
115117

116118
res.status(200).send(responseContacts);
117119
} catch (error) {
118-
errorLogger(
119-
"Could not get contacts:",
120-
providerConfig,
121-
error || "Unknown"
122-
);
120+
console.error("Could not get contacts:", error || "Unknown");
123121
next(error);
124122
}
125123
}
@@ -143,7 +141,9 @@ export class Controller {
143141

144142
const contact: Contact = await this.adapter.createContact(
145143
req.providerConfig,
146-
req.body as ContactTemplate
144+
req.body as ContactTemplate,
145+
req,
146+
res
147147
);
148148

149149
const valid = validate(this.ajv, contactsSchema, [contact]);
@@ -199,7 +199,9 @@ export class Controller {
199199
const contact: Contact = await this.adapter.updateContact(
200200
req.providerConfig,
201201
req.params.id,
202-
req.body as ContactUpdate
202+
req.body as ContactUpdate,
203+
req,
204+
res
203205
);
204206

205207
const valid = validate(this.ajv, contactsSchema, [contact]);
@@ -252,7 +254,7 @@ export class Controller {
252254
console.log(`Deleting contact for key "${anonymizeKey(apiKey)}"`);
253255

254256
const contactId: string = req.params.id;
255-
await this.adapter.deleteContact(req.providerConfig, contactId);
257+
await this.adapter.deleteContact(req.providerConfig, contactId, req, res);
256258

257259
if (this.adapter.getToken && req.providerConfig) {
258260
const { apiKey } = await this.adapter.getToken(req.providerConfig);
@@ -456,40 +458,36 @@ export class Controller {
456458
res: Response,
457459
next: NextFunction
458460
): Promise<void> {
459-
const { providerConfig } = req;
460-
461+
const { providerConfig: { apiKey = "" } = {} } = req;
461462
try {
462-
if (!providerConfig) {
463-
throw new ServerError(400, "Missing config parameters");
464-
}
465-
466463
if (!this.adapter.handleCallEvent) {
467464
throw new ServerError(501, "Handling call event is not implemented");
468465
}
469466

467+
if (!req.providerConfig) {
468+
throw new ServerError(400, "Missing config parameters");
469+
}
470+
470471
if (shouldSkipCallEvent(req.body as CallEvent)) {
471-
infoLogger(
472-
`Skipping call event for call id ${req.body.id}`,
473-
providerConfig
472+
console.log(
473+
`[${anonymizeKey(apiKey)}] skipping call event for call id ${
474+
req.body.id
475+
}`
474476
);
475477
res.status(200).send("Skipping call event");
476478
return;
477479
}
478480

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

481483
const integrationCallEventRef = await this.adapter.handleCallEvent(
482-
providerConfig,
484+
req.providerConfig,
483485
req.body as CallEvent
484486
);
485487

486488
res.status(200).send(integrationCallEventRef);
487489
} catch (error) {
488-
errorLogger(
489-
"Could not handle call event:",
490-
providerConfig,
491-
error || "Unknown"
492-
);
490+
console.error("Could not handle call event:", error || "Unknown");
493491
next(error);
494492
}
495493
}
@@ -524,11 +522,11 @@ export class Controller {
524522
}
525523

526524
public async updateCallEvent(
527-
req: UpdateCallEventBridgeRequest,
525+
req: BridgeRequest,
528526
res: Response,
529527
next: NextFunction
530528
): Promise<void> {
531-
const { providerConfig: { apiKey = "" } = {}, body, params } = req;
529+
const { providerConfig: { apiKey = "" } = {} } = req;
532530

533531
try {
534532
if (!this.adapter.updateCallEvent) {
@@ -539,9 +537,24 @@ export class Controller {
539537
throw new ServerError(400, "Missing config parameters");
540538
}
541539

540+
if (shouldSkipCallEvent(req.body as CallEvent)) {
541+
console.log(
542+
`[${anonymizeKey(apiKey)}] skipping call event for call id ${
543+
req.body.id
544+
}`
545+
);
546+
res.status(200).send("Skipping call event");
547+
return;
548+
}
549+
542550
console.log(`[${anonymizeKey(apiKey)}] Updating call event`);
543551

544-
await this.adapter.updateCallEvent(req.providerConfig, params.id, body);
552+
//maybe return updated state obj
553+
await this.adapter.updateCallEvent(
554+
req.providerConfig,
555+
req.params.id,
556+
req.body as CallEvent
557+
);
545558
res.status(200).send();
546559
} catch (error) {
547560
console.error("Could not update call event:", error || "Unknown");

0 commit comments

Comments
 (0)