Skip to content

Commit

Permalink
0.13.32 added optional parameters req, res for getContacts, createCon…
Browse files Browse the repository at this point in the history
…tct, updateContact and deleteContact
  • Loading branch information
siavashcsr committed Mar 27, 2023
1 parent f263359 commit 67224f4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sipgate/integration-bridge",
"version": "0.13.31",
"version": "0.13.32",
"description": "sipgate Integration Bridge Framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
21 changes: 17 additions & 4 deletions src/models/adapter.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,30 @@ import {

export interface Adapter {
getToken?: (config: Config) => Promise<{ apiKey: string }>;
getContacts?: (config: Config) => Promise<Contact[]>;
getContacts?: (
config: Config,
req?: Request,
res?: Response
) => Promise<Contact[]>;
createContact?: (
config: Config,
contact: ContactTemplate
contact: ContactTemplate,
req?: Request,
res?: Response
) => Promise<Contact>;
updateContact?: (
config: Config,
id: string,
contact: ContactUpdate
contact: ContactUpdate,
req?: Request,
res?: Response
) => Promise<Contact>;
deleteContact?: (config: Config, id: string) => Promise<void>;
deleteContact?: (
config: Config,
id: string,
req?: Request,
res?: Response
) => Promise<void>;
getCalendarEvents?: (
config: Config,
options?: CalendarFilterOptions | null
Expand Down
77 changes: 45 additions & 32 deletions src/models/controller.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,22 @@ export class Controller {
infoLogger(`Fetching contacts…`, providerConfig);

const fetchedContacts: Contact[] = await this.adapter.getContacts(
providerConfig
providerConfig,
req,
res
);

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

return fetchedContacts.map((contact) =>
sanitizeContact(contact, providerConfig.locale)
sanitizeContact(contact, locale)
);
};

const fetcherPromise = this.contactCache
? this.contactCache.get(providerConfig.apiKey, fetchContacts)
? this.contactCache.get(apiKey, fetchContacts)
: fetchContacts();

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

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

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

const contactsCount = responseContacts.length;

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

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

res.status(200).send(responseContacts);
} catch (error) {
errorLogger(
"Could not get contacts:",
providerConfig,
error || "Unknown"
);
console.error("Could not get contacts:", error || "Unknown");
next(error);
}
}
Expand All @@ -143,7 +141,9 @@ export class Controller {

const contact: Contact = await this.adapter.createContact(
req.providerConfig,
req.body as ContactTemplate
req.body as ContactTemplate,
req,
res
);

const valid = validate(this.ajv, contactsSchema, [contact]);
Expand Down Expand Up @@ -199,7 +199,9 @@ export class Controller {
const contact: Contact = await this.adapter.updateContact(
req.providerConfig,
req.params.id,
req.body as ContactUpdate
req.body as ContactUpdate,
req,
res
);

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

const contactId: string = req.params.id;
await this.adapter.deleteContact(req.providerConfig, contactId);
await this.adapter.deleteContact(req.providerConfig, contactId, req, res);

if (this.adapter.getToken && req.providerConfig) {
const { apiKey } = await this.adapter.getToken(req.providerConfig);
Expand Down Expand Up @@ -456,40 +458,36 @@ export class Controller {
res: Response,
next: NextFunction
): Promise<void> {
const { providerConfig } = req;

const { providerConfig: { apiKey = "" } = {} } = req;
try {
if (!providerConfig) {
throw new ServerError(400, "Missing config parameters");
}

if (!this.adapter.handleCallEvent) {
throw new ServerError(501, "Handling call event is not implemented");
}

if (!req.providerConfig) {
throw new ServerError(400, "Missing config parameters");
}

if (shouldSkipCallEvent(req.body as CallEvent)) {
infoLogger(
`Skipping call event for call id ${req.body.id}`,
providerConfig
console.log(
`[${anonymizeKey(apiKey)}] skipping call event for call id ${
req.body.id
}`
);
res.status(200).send("Skipping call event");
return;
}

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

const integrationCallEventRef = await this.adapter.handleCallEvent(
providerConfig,
req.providerConfig,
req.body as CallEvent
);

res.status(200).send(integrationCallEventRef);
} catch (error) {
errorLogger(
"Could not handle call event:",
providerConfig,
error || "Unknown"
);
console.error("Could not handle call event:", error || "Unknown");
next(error);
}
}
Expand Down Expand Up @@ -524,11 +522,11 @@ export class Controller {
}

public async updateCallEvent(
req: UpdateCallEventBridgeRequest,
req: BridgeRequest,
res: Response,
next: NextFunction
): Promise<void> {
const { providerConfig: { apiKey = "" } = {}, body, params } = req;
const { providerConfig: { apiKey = "" } = {} } = req;

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

if (shouldSkipCallEvent(req.body as CallEvent)) {
console.log(
`[${anonymizeKey(apiKey)}] skipping call event for call id ${
req.body.id
}`
);
res.status(200).send("Skipping call event");
return;
}

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

await this.adapter.updateCallEvent(req.providerConfig, params.id, body);
//maybe return updated state obj
await this.adapter.updateCallEvent(
req.providerConfig,
req.params.id,
req.body as CallEvent
);
res.status(200).send();
} catch (error) {
console.error("Could not update call event:", error || "Unknown");
Expand Down

0 comments on commit 67224f4

Please sign in to comment.