From 0fca8c07460aaa759f69c14cf89a816e46e65abe Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 23 Aug 2024 14:56:33 -0300 Subject: [PATCH] fix: set events --- .../integrations/event/event.controller.ts | 4 +- src/api/integrations/event/event.schema.ts | 82 ++++++------------- .../event/webhook/webhook.controller.ts | 2 +- .../event/webhook/webhook.schema.ts | 30 +------ 4 files changed, 33 insertions(+), 85 deletions(-) diff --git a/src/api/integrations/event/event.controller.ts b/src/api/integrations/event/event.controller.ts index d6fa957c..220f60f1 100644 --- a/src/api/integrations/event/event.controller.ts +++ b/src/api/integrations/event/event.controller.ts @@ -80,7 +80,7 @@ export class EventController { data[this.name].events = []; } else { if (0 === data[this.name].events.length) { - data[this.name].events = this.events; + data[this.name].events = EventController.events; } } @@ -122,7 +122,7 @@ export class EventController { return data; } - public readonly events = [ + public static readonly events = [ 'APPLICATION_STARTUP', 'QRCODE_UPDATED', 'MESSAGES_SET', diff --git a/src/api/integrations/event/event.schema.ts b/src/api/integrations/event/event.schema.ts index 98b152ea..9ea9fac2 100644 --- a/src/api/integrations/event/event.schema.ts +++ b/src/api/integrations/event/event.schema.ts @@ -1,67 +1,39 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; -export * from '@api/integrations/event/webhook/webhook.schema'; +import { EventController } from './event.controller'; -const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { - const properties = {}; - propertyNames.forEach( - (property) => - (properties[property] = { - minLength: 1, - description: `The "${property}" cannot be empty`, - }), - ); - return { - if: { - propertyNames: { - enum: [...propertyNames], - }, - }, - then: { properties }, - }; -}; +export * from '@api/integrations/event/webhook/webhook.schema'; export const eventSchema: JSONSchema7 = { $id: v4(), type: 'object', properties: { - enabled: { type: 'boolean', enum: [true, false] }, - 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', - ], - }, + websocket: { + $ref: '#/$defs/event', + }, + rabbitmq: { + $ref: '#/$defs/event', + }, + sqs: { + $ref: '#/$defs/event', + }, + }, + $defs: { + event: { + type: 'object', + properties: { + enabled: { type: 'boolean', enum: [true, false] }, + events: { + type: 'array', + minItems: 0, + items: { + type: 'string', + enum: EventController.events, + }, + }, + }, + required: ['enabled'], }, }, - required: ['enabled'], - ...isNotEmpty('enabled'), }; diff --git a/src/api/integrations/event/webhook/webhook.controller.ts b/src/api/integrations/event/webhook/webhook.controller.ts index 337982d3..7799b28e 100644 --- a/src/api/integrations/event/webhook/webhook.controller.ts +++ b/src/api/integrations/event/webhook/webhook.controller.ts @@ -26,7 +26,7 @@ export class WebhookController extends EventController implements EventControlle data.webhook.events = []; } else { if (0 === data.webhook.events.length) { - data.webhook.events = this.events; + data.webhook.events = EventController.events; } } diff --git a/src/api/integrations/event/webhook/webhook.schema.ts b/src/api/integrations/event/webhook/webhook.schema.ts index 6a2310e2..eaf762ce 100644 --- a/src/api/integrations/event/webhook/webhook.schema.ts +++ b/src/api/integrations/event/webhook/webhook.schema.ts @@ -1,6 +1,8 @@ import { JSONSchema7 } from 'json-schema'; import { v4 } from 'uuid'; +import { EventController } from '../event.controller'; + const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { const properties = {}; propertyNames.forEach( @@ -36,33 +38,7 @@ export const webhookSchema: JSONSchema7 = { 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', - ], + enum: EventController.events, }, }, },