refactor: change webhook reception for all instances

This commit refactors the webhook reception to handle all instances at once. Previously, each instance had its own webhook endpoint, but now there is a single endpoint for all instances. This change simplifies the codebase and reduces the potential for errors.

The main changes include:
- Modifying the `InstanceController` to update the webhook URL for all instances.
- Modifying the `WebhookController` to handle the reception of webhooks for all instances.
- Modifying the `IndexRouter` and `WebhookRouter` to add a new route for the webhook reception endpoint.
- Modifying the `ServerModule` to inject the `PrismaRepository` into the `WebhookService`.
- Modifying the `WebhookService` to handle the reception of webhooks for all instances.

These changes improve the maintainability and scalability of the application, as there is no longer a need to manage individual webhook endpoints for each instance.
This commit is contained in:
Davidson Gomes
2024-07-12 08:52:18 -03:00
parent 90e03e6d1e
commit 4737c71ae1
7 changed files with 55 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
import { Router } from 'express';
import fs from 'fs';
import { configService } from '../../config/env.config';
import { configService, WaBusiness } from '../../config/env.config';
import { authGuard } from '../guards/auth.guard';
import { instanceExistsGuard, instanceLoggedGuard } from '../guards/instance.guard';
import { ChatwootRouter } from '../integrations/chatwoot/routes/chatwoot.router';
@@ -9,6 +9,7 @@ import { RabbitmqRouter } from '../integrations/rabbitmq/routes/rabbitmq.router'
import { SqsRouter } from '../integrations/sqs/routes/sqs.router';
import { TypebotRouter } from '../integrations/typebot/routes/typebot.router';
import { WebsocketRouter } from '../integrations/websocket/routes/websocket.router';
import { webhookController } from '../server.module';
import { ChatRouter } from './chat.router';
import { GroupRouter } from './group.router';
import { InstanceRouter } from './instance.router';
@@ -59,6 +60,17 @@ router
.use('/sqs', new SqsRouter(...guards).router)
.use('/typebot', new TypebotRouter(...guards).router)
.use('/proxy', new ProxyRouter(...guards).router)
.use('/label', new LabelRouter(...guards).router);
.use('/label', new LabelRouter(...guards).router)
.get('/webhook/meta', async (req, res) => {
if (req.query['hub.verify_token'] === configService.get<WaBusiness>('WA_BUSINESS').TOKEN_WEBHOOK)
res.send(req.query['hub.challenge']);
else res.send('Error, wrong validation token');
})
.post('/webhook/meta', async (req, res) => {
const { body } = req;
const response = await webhookController.receiveWebhook(body);
return res.status(200).json(response);
});
export { HttpStatus, router };

View File

@@ -1,6 +1,6 @@
import { RequestHandler, Router } from 'express';
import { ConfigService, WaBusiness } from '../../config/env.config';
import { ConfigService } from '../../config/env.config';
import { instanceSchema, webhookSchema } from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router';
import { InstanceDto } from '../dto/instance.dto';
@@ -31,21 +31,6 @@ export class WebhookRouter extends RouterBroker {
});
res.status(HttpStatus.OK).json(response);
})
.post(this.routerPath('whatsapp'), async (req, res) => {
const response = await this.dataValidate<InstanceDto>({
request: req,
schema: instanceSchema,
ClassRef: InstanceDto,
execute: (instance, data) => webhookController.receiveWebhook(instance, data),
});
res.status(HttpStatus.OK).json(response);
})
.get(this.routerPath('whatsapp'), async (req, res) => {
if (req.query['hub.verify_token'] === this.configService.get<WaBusiness>('WA_BUSINESS').TOKEN_WEBHOOK)
res.send(req.query['hub.challenge']);
else res.send('Error, wrong validation token');
});
}