diff --git a/package.json b/package.json index 687e4cd6..da6f8fdb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/models/adapter.model.ts b/src/models/adapter.model.ts index c31ff9e6..c560468b 100644 --- a/src/models/adapter.model.ts +++ b/src/models/adapter.model.ts @@ -13,17 +13,30 @@ import { export interface Adapter { getToken?: (config: Config) => Promise<{ apiKey: string }>; - getContacts?: (config: Config) => Promise; + getContacts?: ( + config: Config, + req?: Request, + res?: Response + ) => Promise; createContact?: ( config: Config, - contact: ContactTemplate + contact: ContactTemplate, + req?: Request, + res?: Response ) => Promise; updateContact?: ( config: Config, id: string, - contact: ContactUpdate + contact: ContactUpdate, + req?: Request, + res?: Response ) => Promise; - deleteContact?: (config: Config, id: string) => Promise; + deleteContact?: ( + config: Config, + id: string, + req?: Request, + res?: Response + ) => Promise; getCalendarEvents?: ( config: Config, options?: CalendarFilterOptions | null diff --git a/src/models/controller.model.ts b/src/models/controller.model.ts index 8f9b6417..ee603761 100644 --- a/src/models/controller.model.ts +++ b/src/models/controller.model.ts @@ -67,7 +67,9 @@ export class Controller { infoLogger(`Fetching contacts…`, providerConfig); const fetchedContacts: Contact[] = await this.adapter.getContacts( - providerConfig + providerConfig, + req, + res ); if (!validate(this.ajv, contactsSchema, fetchedContacts)) { @@ -75,12 +77,12 @@ export class Controller { } 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) => @@ -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) @@ -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) && @@ -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); } } @@ -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]); @@ -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]); @@ -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); @@ -456,40 +458,36 @@ export class Controller { res: Response, next: NextFunction ): Promise { - 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); } } @@ -524,11 +522,11 @@ export class Controller { } public async updateCallEvent( - req: UpdateCallEventBridgeRequest, + req: BridgeRequest, res: Response, next: NextFunction ): Promise { - const { providerConfig: { apiKey = "" } = {}, body, params } = req; + const { providerConfig: { apiKey = "" } = {} } = req; try { if (!this.adapter.updateCallEvent) { @@ -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");