From fc30bb98526fa2fdad822bb53f87d7687049fa51 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Mon, 12 Jun 2023 14:40:26 -0300 Subject: [PATCH] feat: Added configuration of events by webhook of instances --- CHANGELOG.md | 1 + src/validate/validate.schema.ts | 56 +++++++++++++++++++ .../controllers/instance.controller.ts | 6 +- src/whatsapp/dto/instance.dto.ts | 1 + src/whatsapp/dto/webhook.dto.ts | 1 + src/whatsapp/models/webhook.model.ts | 2 + src/whatsapp/services/whatsapp.service.ts | 21 +++++-- src/whatsapp/types/wa.types.ts | 2 +- 8 files changed, 81 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c88766..9366295d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Route to update group subject * Route to update group description * Route to accept invite code +* Added configuration of events by webhook of instances ### Fixed diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index d74784b2..408fa32e 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -27,6 +27,34 @@ export const instanceNameSchema: JSONSchema7 = { properties: { instanceName: { type: 'string' }, webhook: { type: 'string' }, + events: { + type: 'array', + minItems: 1, + items: { + type: 'string', + enum: [ + 'APPLICATION_STARTUP', + 'QRCODE_UPDATED', + 'MESSAGES_SET', + 'MESSAGES_UPSERT', + 'MESSAGES_UPDATE', + 'SEND_MESSAGE', + 'CONTACTS_SET', + 'CONTACTS_UPSERT', + 'CONTACTS_UPDATE', + 'PRESENCE_UPDATE', + 'CHATS_SET', + 'CHATS_UPSERT', + 'CHATS_UPDATE', + 'CHATS_DELETE', + 'GROUPS_UPSERT', + 'GROUP_UPDATE', + 'GROUP_PARTICIPANTS_UPDATE', + 'CONNECTION_UPDATE', + 'NEW_JWT_TOKEN', + ], + }, + }, }, ...isNotEmpty('instanceName'), }; @@ -720,6 +748,34 @@ export const webhookSchema: JSONSchema7 = { properties: { url: { type: 'string' }, enabled: { type: 'boolean', enum: [true, false] }, + events: { + type: 'array', + minItems: 1, + items: { + type: 'string', + enum: [ + 'APPLICATION_STARTUP', + 'QRCODE_UPDATED', + 'MESSAGES_SET', + 'MESSAGES_UPSERT', + 'MESSAGES_UPDATE', + 'SEND_MESSAGE', + 'CONTACTS_SET', + 'CONTACTS_UPSERT', + 'CONTACTS_UPDATE', + 'PRESENCE_UPDATE', + 'CHATS_SET', + 'CHATS_UPSERT', + 'CHATS_UPDATE', + 'CHATS_DELETE', + 'GROUPS_UPSERT', + 'GROUP_UPDATE', + 'GROUP_PARTICIPANTS_UPDATE', + 'CONNECTION_UPDATE', + 'NEW_JWT_TOKEN', + ], + }, + }, }, required: ['url', 'enabled'], ...isNotEmpty('url'), diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 2b43ede4..d6d12a04 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -22,12 +22,10 @@ export class InstanceController { private readonly logger = new Logger(InstanceController.name); - public async createInstance({ instanceName, webhook }: InstanceDto) { - //verifica se modo da instancia é container + public async createInstance({ instanceName, webhook, events }: InstanceDto) { const mode = this.configService.get('AUTHENTICATION').INSTANCE.MODE; if (mode === 'container') { - //verifica se ja existe uma instancia criada com qualquer nome if (Object.keys(this.waMonitor.waInstances).length > 0) { throw new BadRequestException([ 'Instance already created', @@ -50,7 +48,7 @@ export class InstanceController { if (webhook) { try { - this.webhookService.create(instance, { enabled: true, url: webhook }); + this.webhookService.create(instance, { enabled: true, url: webhook, events }); } catch (error) { this.logger.log(error); } diff --git a/src/whatsapp/dto/instance.dto.ts b/src/whatsapp/dto/instance.dto.ts index 88c59348..90d1f9d3 100644 --- a/src/whatsapp/dto/instance.dto.ts +++ b/src/whatsapp/dto/instance.dto.ts @@ -1,4 +1,5 @@ export class InstanceDto { instanceName: string; webhook?: string; + events?: string[]; } diff --git a/src/whatsapp/dto/webhook.dto.ts b/src/whatsapp/dto/webhook.dto.ts index b87793f0..361009db 100644 --- a/src/whatsapp/dto/webhook.dto.ts +++ b/src/whatsapp/dto/webhook.dto.ts @@ -1,4 +1,5 @@ export class WebhookDto { enabled?: boolean; url?: string; + events?: string[]; } diff --git a/src/whatsapp/models/webhook.model.ts b/src/whatsapp/models/webhook.model.ts index 11c73015..3033c67f 100644 --- a/src/whatsapp/models/webhook.model.ts +++ b/src/whatsapp/models/webhook.model.ts @@ -5,12 +5,14 @@ export class WebhookRaw { _id?: string; url?: string; enabled?: boolean; + events?: string[]; } const webhookSchema = new Schema({ _id: { type: String, _id: true }, url: { type: String, required: true }, enabled: { type: Boolean, required: true }, + events: { type: [String], required: true }, }); export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook'); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 81dfc5c9..c79f2419 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -200,6 +200,7 @@ export class WAStartupService { const data = await this.repository.webhook.find(this.instanceName); this.localWebhook.url = data?.url; this.localWebhook.enabled = data?.enabled; + this.localWebhook.events = data?.events; } public async setWebhook(data: WebhookRaw) { @@ -212,12 +213,13 @@ export class WAStartupService { } public async sendDataWebhook(event: Events, data: T, local = true) { - const webhook = this.configService.get('WEBHOOK'); + const webhookGlobal = this.configService.get('WEBHOOK'); + const webhookLocal = this.localWebhook.events; const we = event.replace(/[\.-]/gm, '_').toUpperCase(); const transformedWe = we.replace(/_/gm, '-').toLowerCase(); const instance = this.configService.get('AUTHENTICATION').INSTANCE; - if (webhook.EVENTS[we]) { + if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) { if (local && instance.MODE !== 'container') { const { WEBHOOK_BY_EVENTS } = instance; @@ -229,6 +231,15 @@ export class WAStartupService { baseURL = this.localWebhook.url; } + this.logger.log({ + local: WAStartupService.name + '.sendDataWebhook-local', + url: baseURL, + event, + instance: this.instance.name, + data, + destination: this.localWebhook.url, + }); + try { if (this.localWebhook.enabled && isURL(this.localWebhook.url)) { const httpService = axios.create({ baseURL }); @@ -253,12 +264,13 @@ export class WAStartupService { }); } } - + } + if (webhookGlobal.EVENTS[we]) { const globalWebhook = this.configService.get('WEBHOOK').GLOBAL; let globalURL; - if (webhook.GLOBAL.WEBHOOK_BY_EVENTS) { + if (webhookGlobal.GLOBAL.WEBHOOK_BY_EVENTS) { globalURL = `${globalWebhook.URL}/${transformedWe}`; } else { globalURL = globalWebhook.URL; @@ -273,6 +285,7 @@ export class WAStartupService { } this.logger.log({ + local: WAStartupService.name + '.sendDataWebhook-global', url: globalURL, event, instance: this.instance.name, diff --git a/src/whatsapp/types/wa.types.ts b/src/whatsapp/types/wa.types.ts index c6db3111..4bc87db9 100644 --- a/src/whatsapp/types/wa.types.ts +++ b/src/whatsapp/types/wa.types.ts @@ -34,7 +34,7 @@ export declare namespace wa { profilePictureUrl?: string; }; - export type LocalWebHook = { enabled?: boolean; url?: string }; + export type LocalWebHook = { enabled?: boolean; url?: string; events?: string[] }; export type StateConnection = { instance?: string;