mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-26 15:17:44 -06:00
feat: Save chatwoot creds
This commit is contained in:
26
src/whatsapp/services/chatwoot.service.ts
Normal file
26
src/whatsapp/services/chatwoot.service.ts
Normal 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: '' };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user