feat: adiciona headers no cadastro de webhook da instância

This commit is contained in:
Felipe Medeiros 2024-09-06 20:46:10 -03:00
parent a9e4860a77
commit 65c6ecff88
11 changed files with 21 additions and 2 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@ lerna-debug.log*
# Package
/yarn.lock
/pnpm-lock.yaml
/package-lock.json
# IDEs

View File

@ -181,6 +181,7 @@ model MessageUpdate {
model Webhook {
id String @id @default(cuid())
url String @db.VarChar(500)
headers Json? @db.Json
enabled Boolean? @default(true)
events Json? @db.Json
webhookByEvents Boolean? @default(false)

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Webhook" ADD COLUMN "headers" JSONB;

View File

@ -180,6 +180,7 @@ model MessageUpdate {
model Webhook {
id String @id @default(cuid())
url String @db.VarChar(500)
headers Json? @db.JsonB
enabled Boolean? @default(true) @db.Boolean
events Json? @db.JsonB
webhookByEvents Boolean? @default(false) @db.Boolean

View File

@ -151,6 +151,7 @@ export class InstanceController {
hash,
webhook: {
webhookUrl: instanceData?.webhook?.url,
webhookHeaders: instanceData?.webhook?.headers,
webhookByEvents: instanceData?.webhook?.byEvents,
webhookBase64: instanceData?.webhook?.base64,
},
@ -238,6 +239,7 @@ export class InstanceController {
hash,
webhook: {
webhookUrl: instanceData?.webhook?.url,
webhookHeaders: instanceData?.webhook?.headers,
webhookByEvents: instanceData?.webhook?.byEvents,
webhookBase64: instanceData?.webhook?.base64,
},

View File

@ -1,10 +1,12 @@
import { Constructor } from '@api/integrations/integration.dto';
import { JsonValue } from '@prisma/client/runtime/library';
export class EventDto {
webhook?: {
enabled?: boolean;
events?: string[];
url?: string;
headers?: JsonValue;
byEvents?: boolean;
base64?: boolean;
};
@ -30,6 +32,7 @@ export function EventInstanceMixin<TBase extends Constructor>(Base: TBase) {
webhook?: {
enabled?: boolean;
events?: string[];
headers?: JsonValue;
url?: string;
byEvents?: boolean;
base64?: boolean;

View File

@ -126,6 +126,7 @@ export class EventManager {
enabled: true,
events: data.webhook?.events,
url: data.webhook?.url,
headers: data.webhook?.headers,
base64: data.webhook?.base64,
byEvents: data.webhook?.byEvents,
},

View File

@ -38,6 +38,7 @@ export class WebhookController extends EventController implements EventControlle
enabled: data.webhook?.enabled,
events: data.webhook?.events,
url: data.webhook?.url,
headers: data.webhook?.headers,
webhookBase64: data.webhook.base64,
webhookByEvents: data.webhook.byEvents,
},
@ -46,6 +47,7 @@ export class WebhookController extends EventController implements EventControlle
events: data.webhook?.events,
instanceId: this.monitor.waInstances[instanceName].instanceId,
url: data.webhook?.url,
headers: data.webhook?.headers,
webhookBase64: data.webhook.base64,
webhookByEvents: data.webhook.byEvents,
},
@ -71,6 +73,7 @@ export class WebhookController extends EventController implements EventControlle
const webhookConfig = configService.get<Webhook>('WEBHOOK');
const webhookLocal = instance?.events;
const webhookHeaders = instance?.headers;
const we = event.replace(/[.-]/gm, '_').toUpperCase();
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
const enabledLog = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
@ -108,7 +111,10 @@ export class WebhookController extends EventController implements EventControlle
try {
if (instance?.enabled && isURL(instance.url, { require_tld: false })) {
const httpService = axios.create({ baseURL });
const httpService = axios.create({
baseURL,
headers: webhookHeaders as Record<string, string> | undefined,
});
await httpService.post('', webhookData);
}

View File

@ -3,7 +3,7 @@ import { InstanceDto } from '@api/dto/instance.dto';
import { EventDto } from '@api/integrations/event/event.dto';
import { HttpStatus } from '@api/routes/index.router';
import { eventManager } from '@api/server.module';
import { ConfigService, WaBusiness } from '@config/env.config';
import { ConfigService } from '@config/env.config';
import { instanceSchema, webhookSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';

View File

@ -31,6 +31,7 @@ export const webhookSchema: JSONSchema7 = {
properties: {
enabled: { type: 'boolean' },
url: { type: 'string' },
headers: { type: 'object' },
byEvents: { type: 'boolean' },
base64: { type: 'boolean' },
events: {

View File

@ -94,6 +94,7 @@ export declare namespace wa {
export type LocalWebHook = LocalEvent & {
url?: string;
headers?: JsonValue;
webhookByEvents?: boolean;
webhookBase64?: boolean;
};