diff --git a/CHANGELOG.md b/CHANGELOG.md index aa07c370..007e3233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * Fixed error to send message in large groups * Docker files adjusted +* Fixed in the postman collection the webhookByEvent parameter by webhook_by_events +* Now it's getting the API URL directly in the request, no longer need the variable in the env file # 1.2.2 (2023-07-15 09:36) diff --git a/Docker/mongodb/docker-compose.yaml b/Docker/mongodb/docker-compose.yaml index 896887ea..86892877 100644 --- a/Docker/mongodb/docker-compose.yaml +++ b/Docker/mongodb/docker-compose.yaml @@ -15,8 +15,6 @@ services: volumes: - evolution_mongodb_data:/data/db - evolution_mongodb_configdb:/data/configdb - networks: - - evolution-net expose: - 27017 diff --git a/package.json b/package.json index 1f313a5c..87f5f908 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "1.2.2", + "version": "1.2.3", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { diff --git a/src/whatsapp/abstract/abstract.router.ts b/src/whatsapp/abstract/abstract.router.ts index e657c2c0..cb224cd6 100644 --- a/src/whatsapp/abstract/abstract.router.ts +++ b/src/whatsapp/abstract/abstract.router.ts @@ -160,8 +160,6 @@ export abstract class RouterBroker { const v = validate(ref, schema); - console.log(v, '@checkei aqui'); - if (!v.valid) { const message: any[] = v.errors.map(({ property, stack, schema }) => { let message: string; @@ -203,8 +201,6 @@ export abstract class RouterBroker { const v = validate(ref, schema); - console.log(v, '@checkei aqui'); - if (!v.valid) { const message: any[] = v.errors.map(({ property, stack, schema }) => { let message: string; diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index a2b432e5..4145acef 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -12,6 +12,7 @@ import { ChatwootService } from '../services/chatwoot.service'; import { Logger } from '../../config/logger.config'; import { wa } from '../types/wa.types'; import { RedisCache } from '../../db/redis.client'; +import { isURL } from 'class-validator'; export class InstanceController { constructor( @@ -27,18 +28,21 @@ export class InstanceController { private readonly logger = new Logger(InstanceController.name); - public async createInstance({ - instanceName, - webhook, - webhook_by_events, - events, - qrcode, - token, - chatwoot_account_id, - chatwoot_token, - chatwoot_url, - chatwoot_sign_msg, - }: InstanceDto) { + public async createInstance( + { + instanceName, + webhook, + webhook_by_events, + events, + qrcode, + token, + chatwoot_account_id, + chatwoot_token, + chatwoot_url, + chatwoot_sign_msg, + }: InstanceDto, + apiURL: string, + ) { this.logger.verbose('requested createInstance from ' + instanceName + ' instance'); const mode = this.configService.get('AUTHENTICATION').INSTANCE.MODE; @@ -82,6 +86,9 @@ export class InstanceController { let getEvents: string[]; if (webhook) { + if (!isURL(webhook, { require_tld: false })) { + throw new BadRequestException('Invalid "url" property in webhook'); + } this.logger.verbose('creating webhook'); try { this.webhookService.create(instance, { @@ -132,7 +139,11 @@ export class InstanceController { throw new BadRequestException('url is required'); } - const urlServer = this.configService.get('SERVER').URL; + if (!isURL(chatwoot_url, { require_tld: false })) { + throw new BadRequestException('Invalid "url" property in chatwoot'); + } + + const urlServer = apiURL; try { this.chatwootService.create(instance, { @@ -203,6 +214,10 @@ export class InstanceController { let getEvents: string[]; if (webhook) { + if (!isURL(webhook, { require_tld: false })) { + throw new BadRequestException('Invalid "url" property in webhook'); + } + this.logger.verbose('creating webhook'); try { this.webhookService.create(instance, { @@ -266,7 +281,11 @@ export class InstanceController { throw new BadRequestException('url is required'); } - const urlServer = this.configService.get('SERVER').URL; + if (!isURL(chatwoot_url, { require_tld: false })) { + throw new BadRequestException('Invalid "url" property in chatwoot'); + } + + const urlServer = apiURL; try { this.chatwootService.create(instance, { diff --git a/src/whatsapp/models/webhook.model.ts b/src/whatsapp/models/webhook.model.ts index 873491bf..62ee38f4 100644 --- a/src/whatsapp/models/webhook.model.ts +++ b/src/whatsapp/models/webhook.model.ts @@ -14,6 +14,7 @@ const webhookSchema = new Schema({ url: { type: String, required: true }, enabled: { type: Boolean, required: true }, events: { type: [String], required: true }, + webhook_by_events: { type: Boolean, required: true }, }); export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook'); diff --git a/src/whatsapp/routers/instance.router.ts b/src/whatsapp/routers/instance.router.ts index 799c8249..7dc35d30 100644 --- a/src/whatsapp/routers/instance.router.ts +++ b/src/whatsapp/routers/instance.router.ts @@ -23,11 +23,15 @@ export class InstanceRouter extends RouterBroker { logger.verbose('request query: '); logger.verbose(req.query); + + const apiURL = req.headers.host || req.hostname; + logger.verbose('API URL: ' + apiURL); + const response = await this.dataValidate({ request: req, schema: instanceNameSchema, ClassRef: InstanceDto, - execute: (instance) => instanceController.createInstance(instance), + execute: (instance) => instanceController.createInstance(instance, apiURL), }); return res.status(HttpStatus.CREATED).json(response);