From fcdee75f39fc675ae47387591f19b6b569f82b0f Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 23 Aug 2024 14:24:32 -0300 Subject: [PATCH] fix: set webhook --- .../integrations/event/event.controller.ts | 3 +- .../event/webhook/webhook.controller.ts | 12 ++- .../event/webhook/webhook.schema.ts | 82 ++++++++++--------- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/api/integrations/event/event.controller.ts b/src/api/integrations/event/event.controller.ts index f77a9e77..d6fa957c 100644 --- a/src/api/integrations/event/event.controller.ts +++ b/src/api/integrations/event/event.controller.ts @@ -89,7 +89,8 @@ export class EventController { instanceId: this.monitor.waInstances[instanceName].instanceId, }, update: { - ...data[this.name], + enabled: data[this.name]?.enabled, + events: data[this.name].events, }, create: { enabled: data[this.name]?.enabled, diff --git a/src/api/integrations/event/webhook/webhook.controller.ts b/src/api/integrations/event/webhook/webhook.controller.ts index c1ef8274..dc065d1c 100644 --- a/src/api/integrations/event/webhook/webhook.controller.ts +++ b/src/api/integrations/event/webhook/webhook.controller.ts @@ -30,18 +30,24 @@ export class WebhookController extends EventController implements EventControlle } } + console.log('data.webhook', data); + return this.prisma.webhook.upsert({ where: { instanceId: this.monitor.waInstances[instanceName].instanceId, }, update: { - ...data.webhook, + enabled: data.webhook?.enabled, + events: data.webhook?.events, + url: data.webhook?.url, + webhookBase64: data.webhook.base64, + webhookByEvents: data.webhook.byEvents, }, create: { enabled: data.webhook?.enabled, - events: data.webhook.events, + events: data.webhook?.events, instanceId: this.monitor.waInstances[instanceName].instanceId, - url: data.webhook.url, + url: data.webhook?.url, webhookBase64: data.webhook.base64, webhookByEvents: data.webhook.byEvents, }, diff --git a/src/api/integrations/event/webhook/webhook.schema.ts b/src/api/integrations/event/webhook/webhook.schema.ts index 82a3a5c2..6a2310e2 100644 --- a/src/api/integrations/event/webhook/webhook.schema.ts +++ b/src/api/integrations/event/webhook/webhook.schema.ts @@ -24,45 +24,51 @@ export const webhookSchema: JSONSchema7 = { $id: v4(), type: 'object', properties: { - enabled: { type: 'boolean' }, - url: { type: 'string' }, - webhookByEvents: { type: 'boolean' }, - webhookBase64: { type: 'boolean' }, - events: { - type: 'array', - minItems: 0, - items: { - type: 'string', - enum: [ - 'APPLICATION_STARTUP', - 'QRCODE_UPDATED', - 'MESSAGES_SET', - 'MESSAGES_UPSERT', - 'MESSAGES_EDITED', - 'MESSAGES_UPDATE', - 'MESSAGES_DELETE', - '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', - 'LABELS_EDIT', - 'LABELS_ASSOCIATION', - 'CALL', - 'TYPEBOT_START', - 'TYPEBOT_CHANGE_STATUS', - ], + webhook: { + type: 'object', + properties: { + enabled: { type: 'boolean' }, + url: { type: 'string' }, + byEvents: { type: 'boolean' }, + base64: { type: 'boolean' }, + events: { + type: 'array', + minItems: 0, + items: { + type: 'string', + enum: [ + 'APPLICATION_STARTUP', + 'QRCODE_UPDATED', + 'MESSAGES_SET', + 'MESSAGES_UPSERT', + 'MESSAGES_EDITED', + 'MESSAGES_UPDATE', + 'MESSAGES_DELETE', + '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', + 'LABELS_EDIT', + 'LABELS_ASSOCIATION', + 'CALL', + 'TYPEBOT_START', + 'TYPEBOT_CHANGE_STATUS', + ], + }, + }, }, + required: ['enabled', 'url'], + ...isNotEmpty('enabled', 'url'), }, }, - required: ['enabled', 'url'], - ...isNotEmpty('enabled', 'url'), + required: ['webhook'], };