fix: set events

This commit is contained in:
Davidson Gomes 2024-08-23 14:56:33 -03:00
parent d7b3230db9
commit 0fca8c0746
4 changed files with 33 additions and 85 deletions

View File

@ -80,7 +80,7 @@ export class EventController {
data[this.name].events = []; data[this.name].events = [];
} else { } else {
if (0 === data[this.name].events.length) { 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; return data;
} }
public readonly events = [ public static readonly events = [
'APPLICATION_STARTUP', 'APPLICATION_STARTUP',
'QRCODE_UPDATED', 'QRCODE_UPDATED',
'MESSAGES_SET', 'MESSAGES_SET',

View File

@ -1,67 +1,39 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
export * from '@api/integrations/event/webhook/webhook.schema'; import { EventController } from './event.controller';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { export * from '@api/integrations/event/webhook/webhook.schema';
const properties = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
};
};
export const eventSchema: JSONSchema7 = { export const eventSchema: JSONSchema7 = {
$id: v4(), $id: v4(),
type: 'object', type: 'object',
properties: { properties: {
enabled: { type: 'boolean', enum: [true, false] }, websocket: {
events: { $ref: '#/$defs/event',
type: 'array', },
minItems: 0, rabbitmq: {
items: { $ref: '#/$defs/event',
type: 'string', },
enum: [ sqs: {
'APPLICATION_STARTUP', $ref: '#/$defs/event',
'QRCODE_UPDATED', },
'MESSAGES_SET', },
'MESSAGES_UPSERT', $defs: {
'MESSAGES_EDITED', event: {
'MESSAGES_UPDATE', type: 'object',
'MESSAGES_DELETE', properties: {
'SEND_MESSAGE', enabled: { type: 'boolean', enum: [true, false] },
'CONTACTS_SET', events: {
'CONTACTS_UPSERT', type: 'array',
'CONTACTS_UPDATE', minItems: 0,
'PRESENCE_UPDATE', items: {
'CHATS_SET', type: 'string',
'CHATS_UPSERT', enum: EventController.events,
'CHATS_UPDATE', },
'CHATS_DELETE', },
'GROUPS_UPSERT', },
'GROUP_UPDATE', required: ['enabled'],
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'LABELS_EDIT',
'LABELS_ASSOCIATION',
'CALL',
'TYPEBOT_START',
'TYPEBOT_CHANGE_STATUS',
],
},
}, },
}, },
required: ['enabled'],
...isNotEmpty('enabled'),
}; };

View File

@ -26,7 +26,7 @@ export class WebhookController extends EventController implements EventControlle
data.webhook.events = []; data.webhook.events = [];
} else { } else {
if (0 === data.webhook.events.length) { if (0 === data.webhook.events.length) {
data.webhook.events = this.events; data.webhook.events = EventController.events;
} }
} }

View File

@ -1,6 +1,8 @@
import { JSONSchema7 } from 'json-schema'; import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import { EventController } from '../event.controller';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => { const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties = {}; const properties = {};
propertyNames.forEach( propertyNames.forEach(
@ -36,33 +38,7 @@ export const webhookSchema: JSONSchema7 = {
minItems: 0, minItems: 0,
items: { items: {
type: 'string', type: 'string',
enum: [ enum: EventController.events,
'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',
],
}, },
}, },
}, },