Skip to content

Commit

Permalink
Merge pull request #137 from vilsonei/main
Browse files Browse the repository at this point in the history
Melhorias e correções
  • Loading branch information
jrCleber authored Jun 21, 2024
2 parents 32c3a1b + 601ae83 commit a6ad0b9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type DBOptions = {
CONTACTS: boolean;
CHATS: boolean;
LOGS: boolean;
ACTIVITY_LOGS: boolean;
};

export type StoreConf = {
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
38 changes: 20 additions & 18 deletions src/middle/logger.middle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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>('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();
}
Expand Down
28 changes: 16 additions & 12 deletions src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 16 additions & 13 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -374,11 +374,12 @@ export class WAStartupService {

this.instanceQr.count++;

const qrCodeOptions: QrCode = this.configService.get<QrCode>('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) => {
Expand Down Expand Up @@ -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>('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;
}
Expand Down

0 comments on commit a6ad0b9

Please sign in to comment.