Fix webhook validation

This commit is contained in:
Judson Cairo 2024-08-21 11:04:18 -03:00
parent b58ad83c12
commit dcef6919cb
2 changed files with 12 additions and 16 deletions

View File

@ -1,7 +1,7 @@
import { PrismaRepository } from '@api/repository/repository.service'; import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service'; import { WAMonitoringService } from '@api/services/monitor.service';
import { wa } from '@api/types/wa.types'; import { wa } from '@api/types/wa.types';
import { configService, Log, Webhook, Websocket } from '@config/env.config'; import { configService, Log, Webhook } from '@config/env.config';
import { Logger } from '@config/logger.config'; import { Logger } from '@config/logger.config';
import { BadRequestException, NotFoundException } from '@exceptions'; import { BadRequestException, NotFoundException } from '@exceptions';
import axios from 'axios'; import axios from 'axios';
@ -31,7 +31,7 @@ export class WebhookController extends EventController {
} }
await this.get(instanceName); await this.get(instanceName);
return this.prisma.webhook.upsert({ return this.prisma.webhook.upsert({
where: { where: {
instanceId: this.monitor.waInstances[instanceName].instanceId, instanceId: this.monitor.waInstances[instanceName].instanceId,
@ -89,12 +89,12 @@ export class WebhookController extends EventController {
apiKey?: string; apiKey?: string;
local?: boolean; local?: boolean;
}): Promise<void> { }): Promise<void> {
if (!configService.get<Websocket>('WEBSOCKET')?.ENABLED) { const instanceWebhook = await this.get(instanceName);
if (!instanceWebhook || !instanceWebhook.enabled) {
return; return;
} }
const instanceWebhook = await this.get(instanceName); const webhookConfig = configService.get<Webhook>('WEBHOOK');
const webhookGlobal = configService.get<Webhook>('WEBHOOK');
const webhookLocal = instanceWebhook?.events; const webhookLocal = instanceWebhook?.events;
const we = event.replace(/[.-]/gm, '_').toUpperCase(); const we = event.replace(/[.-]/gm, '_').toUpperCase();
const transformedWe = we.replace(/_/gm, '-').toLowerCase(); const transformedWe = we.replace(/_/gm, '-').toLowerCase();
@ -153,16 +153,12 @@ export class WebhookController extends EventController {
} }
} }
if (webhookGlobal.GLOBAL?.ENABLED) { if (webhookConfig.GLOBAL?.ENABLED) {
if (webhookGlobal.EVENTS[we]) { if (webhookConfig.EVENTS[we]) {
const globalWebhook = configService.get<Webhook>('WEBHOOK').GLOBAL; let globalURL = webhookConfig.GLOBAL.URL;
let globalURL; if (webhookConfig.GLOBAL.WEBHOOK_BY_EVENTS) {
globalURL = `${globalURL}/${transformedWe}`;
if (webhookGlobal.GLOBAL.WEBHOOK_BY_EVENTS) {
globalURL = `${globalWebhook.URL}/${transformedWe}`;
} else {
globalURL = globalWebhook.URL;
} }
if (enabledLog) { if (enabledLog) {
@ -176,7 +172,7 @@ export class WebhookController extends EventController {
} }
try { try {
if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) { if (isURL(globalURL)) {
const httpService = axios.create({ baseURL: globalURL }); const httpService = axios.create({ baseURL: globalURL });
await httpService.post('', webhookData); await httpService.post('', webhookData);

View File

@ -1,7 +1,7 @@
import { RouterBroker } from '@api/abstract/abstract.router'; import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto'; import { InstanceDto } from '@api/dto/instance.dto';
import { webhookController } from '@api/server.module';
import { HttpStatus } from '@api/routes/index.router'; import { HttpStatus } from '@api/routes/index.router';
import { webhookController } from '@api/server.module';
import { ConfigService, WaBusiness } from '@config/env.config'; import { ConfigService, WaBusiness } from '@config/env.config';
import { instanceSchema, webhookSchema } from '@validate/validate.schema'; import { instanceSchema, webhookSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express'; import { RequestHandler, Router } from 'express';