feat: Created settings Controller

This commit is contained in:
Davidson Gomes
2023-07-24 09:42:29 -03:00
parent be699d24a1
commit 73d9cd62a5
14 changed files with 301 additions and 2 deletions

View File

@@ -0,0 +1,34 @@
import { InstanceDto } from '../dto/instance.dto';
import { SettingsDto } from '../dto/settings.dto';
import { WAMonitoringService } from './monitor.service';
import { Logger } from '../../config/logger.config';
export class SettingsService {
constructor(private readonly waMonitor: WAMonitoringService) {}
private readonly logger = new Logger(SettingsService.name);
public create(instance: InstanceDto, data: SettingsDto) {
this.logger.verbose('create settings: ' + instance.instanceName);
this.waMonitor.waInstances[instance.instanceName].setSettings(data);
return { settings: { ...instance, settings: data } };
}
public async find(instance: InstanceDto): Promise<SettingsDto> {
try {
this.logger.verbose('find settings: ' + instance.instanceName);
const result = await this.waMonitor.waInstances[
instance.instanceName
].findSettings();
if (Object.keys(result).length === 0) {
throw new Error('Settings not found');
}
return result;
} catch (error) {
return { reject_call: false, msg_call: '', groups_ignore: false };
}
}
}

View File

@@ -125,6 +125,7 @@ import { Log } from '../../config/env.config';
import ProxyAgent from 'proxy-agent';
import { ChatwootService } from './chatwoot.service';
import { waMonitor } from '../whatsapp.module';
import { SettingsRaw } from '../models';
export class WAStartupService {
constructor(
@@ -143,6 +144,7 @@ export class WAStartupService {
public client: WASocket;
private readonly localWebhook: wa.LocalWebHook = {};
private readonly localChatwoot: wa.LocalChatwoot = {};
private readonly localSettings: wa.LocalSettings = {};
private stateConnection: wa.StateConnection = { state: 'close' };
public readonly storePath = join(ROOT_DIR, 'store');
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
@@ -341,6 +343,46 @@ export class WAStartupService {
return data;
}
private async loadSettings() {
this.logger.verbose('Loading settings');
const data = await this.repository.settings.find(this.instanceName);
this.localSettings.reject_call = data?.reject_call;
this.logger.verbose(`Settings reject_call: ${this.localSettings.reject_call}`);
this.localSettings.msg_call = data?.msg_call;
this.logger.verbose(`Settings msg_call: ${this.localSettings.msg_call}`);
this.localSettings.groups_ignore = data?.groups_ignore;
this.logger.verbose(`Settings groups_ignore: ${this.localSettings.groups_ignore}`);
this.logger.verbose('Settings loaded');
}
public async setSettings(data: SettingsRaw) {
this.logger.verbose('Setting settings');
await this.repository.settings.create(data, this.instanceName);
this.logger.verbose(`Settings reject_call: ${data.reject_call}`);
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
Object.assign(this.localSettings, data);
this.logger.verbose('Settings set');
}
public async findSettings() {
this.logger.verbose('Finding settings');
const data = await this.repository.settings.find(this.instanceName);
if (!data) {
this.logger.verbose('Settings not found');
throw new NotFoundException('Settings not found');
}
this.logger.verbose(`Settings url: ${data.reject_call}`);
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
return data;
}
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
const webhookLocal = this.localWebhook.events;
@@ -761,6 +803,7 @@ export class WAStartupService {
try {
this.loadWebhook();
this.loadChatwoot();
this.loadSettings();
this.instance.authState = await this.defineAuthState();