From 61fcd608c800d86465e5d18ea880545970a62ccc Mon Sep 17 00:00:00 2001 From: Vilsonei Machado Date: Thu, 20 Jun 2024 11:48:33 -0300 Subject: [PATCH 1/4] =?UTF-8?q?Tratativa=20para=20n=C3=A3o=20permitir=20a?= =?UTF-8?q?=20cria=C3=A7=C3=A3o=20de=20uma=20nova=20instancia=20quando=20u?= =?UTF-8?q?ma=20j=C3=A1=20existe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/instance.controller.ts | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index e64a4f76..fd5e0823 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -109,23 +109,27 @@ export class InstanceController { } try { + let instance: WAStartupService; + instance = this.waMonitor.waInstances.get(instanceName); if ( - this.waMonitor.waInstances.get(instanceName)?.connectionStatus?.state === 'open' + instance?.connectionStatus?.state === 'open' ) { throw 'Instance already connected'; } - const instance = new WAStartupService( - this.configService, - this.eventEmitter, - this.repository, - this.providerFiles, - ); - await instance.setInstanceName(instanceName); - this.waMonitor.waInstances.set(instance.instanceName, instance); - this.waMonitor.delInstanceTime(instance.instanceName); - - this.waMonitor.waInstances.set(instanceName, instance); + if (!instance || !instance.connectionStatus || instance?.connectionStatus?.state === 'refused') { + instance = new WAStartupService( + this.configService, + this.eventEmitter, + this.repository, + this.providerFiles, + ); + await instance.setInstanceName(instanceName); + this.waMonitor.waInstances.set(instance.instanceName, instance); + this.waMonitor.delInstanceTime(instance.instanceName); + + this.waMonitor.waInstances.set(instanceName, instance); + } const state = instance?.connectionStatus?.state; From 71681823cfae5ec3ed81ab8f908ad9b8051f6864 Mon Sep 17 00:00:00 2001 From: Vilsonei Machado Date: Thu, 20 Jun 2024 13:42:57 -0300 Subject: [PATCH 2/4] =?UTF-8?q?Inclus=C3=A3o=20da=20vari=C3=A1vel=20de=20c?= =?UTF-8?q?onfigura=C3=A7=C3=A3o=20DATABASE=5FSAVE=5FACTIVITY=5FLOGS=20par?= =?UTF-8?q?a=20permitir=20salvar=20os=20ou=20n=C3=A3o=20os=20logs=20de=20a?= =?UTF-8?q?tividade=20no=20banco=20de=20dados.=20Quando=20a=20vari=C3=A1ve?= =?UTF-8?q?l=20n=C3=A3o=20=C3=A9=20declarada=20o=20padr=C3=A3o=20=C3=A9=20?= =?UTF-8?q?true?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dev | 1 + src/config/env.config.ts | 2 ++ src/middle/logger.middle.ts | 38 ++++++++++++----------- src/whatsapp/services/whatsapp.service.ts | 26 +++++++++------- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/.env.dev b/.env.dev index e637b744..19225321 100644 --- a/.env.dev +++ b/.env.dev @@ -49,6 +49,7 @@ DATABASE_SAVE_MESSAGE_UPDATE=true DATABASE_SAVE_DATA_CONTACTS=true DATABASE_SAVE_DATA_CHATS=true DATABASE_SAVE_LOGS=true +DATABASE_SAVE_ACTIVITY_LOGS=true # Global Webhook # It will notify all events of all businesses and instances. diff --git a/src/config/env.config.ts b/src/config/env.config.ts index b7412927..bb4f4aab 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -70,6 +70,7 @@ export type DBOptions = { CONTACTS: boolean; CHATS: boolean; LOGS: boolean; + ACTIVITY_LOGS: boolean; }; export type StoreConf = { @@ -175,6 +176,7 @@ export class ConfigService { CONTACTS: process.env?.DATABASE_SAVE_DATA_CONTACTS === 'true', CHATS: process.env?.DATABASE_SAVE_DATA_CHATS === 'true', LOGS: process.env?.DATABASE_SAVE_LOGS === 'true', + ACTIVITY_LOGS: process.env?.DATABASE_SAVE_ACTIVITY_LOGS ? process.env?.DATABASE_SAVE_ACTIVITY_LOGS === 'true' : true }, }, PROVIDER: { diff --git a/src/middle/logger.middle.ts b/src/middle/logger.middle.ts index 316b38c4..8cee40f1 100644 --- a/src/middle/logger.middle.ts +++ b/src/middle/logger.middle.ts @@ -34,7 +34,7 @@ import { NextFunction, Request, Response } from 'express'; import { Logger } from '../config/logger.config'; import { Repository } from '../repository/repository.service'; -import { ConfigService } from '../config/env.config'; +import { ConfigService, Database } from '../config/env.config'; export class LoggerMiddleware { constructor( @@ -56,24 +56,26 @@ export class LoggerMiddleware { }); } - this.repository.activityLogs - .create({ - data: { - context: 'LoggerMiddleware', - type: 'http', - content: { - originalUrl: req.originalUrl, - method: req.method.toUpperCase(), - headers: JSON.stringify(req.headers), - params: JSON.stringify(req?.params || {}), - query: JSON.stringify(req?.query || {}), - body: JSON.stringify(req?.body || {}), + if (this.configService.get('DATABASE').DB_OPTIONS.ACTIVITY_LOGS) { + this.repository.activityLogs + .create({ + data: { + context: 'LoggerMiddleware', + type: 'http', + content: { + originalUrl: req.originalUrl, + method: req.method.toUpperCase(), + headers: JSON.stringify(req.headers), + params: JSON.stringify(req?.params || {}), + query: JSON.stringify(req?.query || {}), + body: JSON.stringify(req?.body || {}), + }, + description: 'Request received', + instanceId: req?.params?.instanceId ? Number(req.params.instanceId) : undefined, }, - description: 'Request received', - instanceId: req?.params?.instanceId ? Number(req.params.instanceId) : undefined, - }, - }) - .catch((error) => logger.error(error)); + }) + .catch((error) => logger.error(error)); + } next(); } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index aa52e2af..eb0cbb83 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -120,7 +120,7 @@ import { GroupUpdateParticipantDto, } from '../dto/group.dto'; import Long from 'long'; -import NodeCache from 'node-cache'; +import NodeCache, { Data } from 'node-cache'; import { AuthState, AuthStateProvider, @@ -1704,17 +1704,19 @@ export class WAStartupService { }; } catch (error) { this.logger.error(error); - this.repository.activityLogs - .create({ - data: { - type: 'error', - context: WAStartupService.name, - description: 'Error on get media message', - content: [error?.toString(), JSON.stringify(error?.stack)], - instanceId: this.instance.id, - }, - }) - .catch((error) => this.logger.error(error)); + if (this.configService.get('DATABASE').DB_OPTIONS.ACTIVITY_LOGS) { + this.repository.activityLogs + .create({ + data: { + type: 'error', + context: WAStartupService.name, + description: 'Error on get media message', + content: [error?.toString(), JSON.stringify(error?.stack)], + instanceId: this.instance.id, + }, + }) + .catch((error) => this.logger.error(error)); + } if (inner) { return; } From 3151a928e072504cb12093bdddc87966ac5903b8 Mon Sep 17 00:00:00 2001 From: Vilsonei Machado Date: Thu, 20 Jun 2024 13:57:38 -0300 Subject: [PATCH 3/4] =?UTF-8?q?Inclus=C3=A3o=20das=20vari=C3=A1veis=20para?= =?UTF-8?q?=20personaliza=C3=A7=C3=A3o=20das=20cores=20do=20QrCode=20no=20?= =?UTF-8?q?arquivo=20de=20configura=C3=A7=C3=A3o.=20Caso=20as=20vari=C3=A1?= =?UTF-8?q?veis=20n=C3=A3o=20sejam=20declaradas,=20ser=C3=A3o=20utilizadas?= =?UTF-8?q?=20as=20cores=20light(#ffffff)=20e=20dark(#198754)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dev | 2 ++ src/config/env.config.ts | 4 ++++ src/whatsapp/services/whatsapp.service.ts | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.env.dev b/.env.dev index 19225321..a921d82f 100644 --- a/.env.dev +++ b/.env.dev @@ -68,6 +68,8 @@ CONFIG_SESSION_PHONE_NAME=Edge # Set qrcode display limit QRCODE_LIMIT=5 QRCODE_EXPIRATION_TIME=20 # seconds +QRCODE_LIGHT_COLOR=#ffffff +QRCODE_DARK_COLOR=#198754 # Maximun time to connect to whatsapp CONNECTION_TIMEOUT=120 # seconds diff --git a/src/config/env.config.ts b/src/config/env.config.ts index bb4f4aab..3b1e20a0 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -98,6 +98,8 @@ export type ProviderSession = { export type QrCode = { LIMIT: number; EXPIRATION_TIME: number; + LIGHT_COLOR: string; + DARK_COLOR: string; }; export type Jwt = { EXPIRIN_IN: number; SECRET: string }; @@ -203,6 +205,8 @@ export class ConfigService { QRCODE: { LIMIT: Number.parseInt(process.env?.QRCODE_LIMIT || '10'), EXPIRATION_TIME: Number.parseInt(process.env?.QRCODE_EXPIRATION_TIME || '60'), + LIGHT_COLOR: process.env?.QRCODE_LIGHT_COLOR ? process.env?.QRCODE_LIGHT_COLOR : '#ffffff', + DARK_COLOR: process.env?.QRCODE_DARK_COLOR ? process.env?.QRCODE_DARK_COLOR : '#198754' }, CONNECTION_TIMEOUT: Number.parseInt(process.env?.CONNECTION_TIMEOUT || '300'), AUTHENTICATION: { diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index eb0cbb83..bd53a387 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -1,3 +1,4 @@ +import { QrCode } from './../../config/env.config'; /** * ┌──────────────────────────────────────────────────────────────────────────────┐ * │ @author jrCleber │ @@ -374,11 +375,12 @@ export class WAStartupService { this.instanceQr.count++; + const qrCodeOptions: QrCode = this.configService.get('QRCODE'); const optsQrcode: QRCodeToDataURLOptions = { margin: 3, scale: 4, errorCorrectionLevel: 'H', - color: { light: '#ffffff', dark: '#198754' }, + color: { light: qrCodeOptions.LIGHT_COLOR, dark: qrCodeOptions.DARK_COLOR }, }; qrcode.toDataURL(qr, optsQrcode, (error, base64) => { From 601ae83268709ac4cf2704a01ea72f73cd09c976 Mon Sep 17 00:00:00 2001 From: Vilsonei Machado Date: Thu, 20 Jun 2024 21:39:54 -0300 Subject: [PATCH 4/4] =?UTF-8?q?Corre=C3=A7=C3=A3o=20de=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dev | 4 ++-- src/whatsapp/services/whatsapp.service.ts | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.env.dev b/.env.dev index a921d82f..1873395e 100644 --- a/.env.dev +++ b/.env.dev @@ -68,8 +68,8 @@ CONFIG_SESSION_PHONE_NAME=Edge # Set qrcode display limit QRCODE_LIMIT=5 QRCODE_EXPIRATION_TIME=20 # seconds -QRCODE_LIGHT_COLOR=#ffffff -QRCODE_DARK_COLOR=#198754 +QRCODE_LIGHT_COLOR='#ffffff' +QRCODE_DARK_COLOR='#198754' # Maximun time to connect to whatsapp CONNECTION_TIMEOUT=120 # seconds diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index bd53a387..eaf6c1bb 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -1,4 +1,3 @@ -import { QrCode } from './../../config/env.config'; /** * ┌──────────────────────────────────────────────────────────────────────────────┐ * │ @author jrCleber │