feat: Save chatwoot creds

This commit is contained in:
Davidson Gomes
2023-07-12 15:41:07 -03:00
parent 86ce00365a
commit 57b61070d9
19 changed files with 478 additions and 71 deletions

View File

@@ -0,0 +1,26 @@
import { InstanceDto } from '../dto/instance.dto';
import { ChatwootDto } from '../dto/chatwoot.dto';
import { WAMonitoringService } from './monitor.service';
import { Logger } from '../../config/logger.config';
export class ChatwootService {
constructor(private readonly waMonitor: WAMonitoringService) {}
private readonly logger = new Logger(ChatwootService.name);
public create(instance: InstanceDto, data: ChatwootDto) {
this.logger.verbose('create chatwoot: ' + instance.instanceName);
this.waMonitor.waInstances[instance.instanceName].setChatwoot(data);
return { chatwoot: { ...instance, chatwoot: data } };
}
public async find(instance: InstanceDto): Promise<ChatwootDto> {
try {
this.logger.verbose('find chatwoot: ' + instance.instanceName);
return await this.waMonitor.waInstances[instance.instanceName].findChatwoot();
} catch (error) {
return { enabled: null, url: '' };
}
}
}

View File

@@ -114,6 +114,7 @@ import { MessageUpQuery } from '../repository/messageUp.repository';
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
import Long from 'long';
import { WebhookRaw } from '../models/webhook.model';
import { ChatwootRaw } from '../models/chatwoot.model';
import { dbserver } from '../../db/db.connect';
import NodeCache from 'node-cache';
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
@@ -138,6 +139,7 @@ export class WAStartupService {
private readonly instance: wa.Instance = {};
public client: WASocket;
private readonly localWebhook: wa.LocalWebHook = {};
private readonly localChatwoot: wa.LocalChatwoot = {};
private stateConnection: wa.StateConnection = { state: 'close' };
private readonly storePath = join(ROOT_DIR, 'store');
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
@@ -268,6 +270,34 @@ export class WAStartupService {
return data;
}
public async setChatwoot(data: ChatwootRaw) {
this.logger.verbose('Setting chatwoot');
await this.repository.chatwoot.create(data, this.instanceName);
this.logger.verbose(`Chatwoot account id: ${data.account_id}`);
this.logger.verbose(`Chatwoot token: ${data.token}`);
this.logger.verbose(`Chatwoot url: ${data.url}`);
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
Object.assign(this.localChatwoot, data);
this.logger.verbose('Chatwoot set');
}
public async findChatwoot() {
this.logger.verbose('Finding chatwoot');
const data = await this.repository.chatwoot.find(this.instanceName);
if (!data) {
this.logger.verbose('Chatwoot not found');
throw new NotFoundException('Chatwoot not found');
}
this.logger.verbose(`Chatwoot account id: ${data.account_id}`);
this.logger.verbose(`Chatwoot token: ${data.token}`);
this.logger.verbose(`Chatwoot url: ${data.url}`);
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
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;