diff --git a/.env.dev b/.env.dev index e637b744..1873395e 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. @@ -67,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 b7412927..3b1e20a0 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 = { @@ -97,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 }; @@ -175,6 +178,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: { @@ -201,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/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/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; diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index aa52e2af..eaf6c1bb 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, @@ -374,11 +374,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) => { @@ -1704,17 +1705,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; }