diff --git a/src/api/repository/repository.service.ts b/src/api/repository/repository.service.ts index 9c9bbf0eb..7dfc5d3b6 100644 --- a/src/api/repository/repository.service.ts +++ b/src/api/repository/repository.service.ts @@ -1,4 +1,6 @@ import { PrismaClient } from '@prisma/client'; +import fs from 'fs'; +import { join } from 'path'; import { ConfigService } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; @@ -13,6 +15,30 @@ export class Query { export class PrismaRepository extends PrismaClient { constructor(private readonly configService: ConfigService) { super(); + + this.initStoreFolders(); + } + + private async initStoreFolders() { + try { + const storePath = join(process.cwd(), 'store'); + + this.logger.verbose('creating store path: ' + storePath); + + const tempDir = join(storePath, 'temp'); + const chatwootDir = join(storePath, 'chatwoot'); + + if (!fs.existsSync(chatwootDir)) { + this.logger.verbose('creating chatwoot dir: ' + chatwootDir); + fs.mkdirSync(chatwootDir, { recursive: true }); + } + if (!fs.existsSync(tempDir)) { + this.logger.verbose('creating temp dir: ' + tempDir); + fs.mkdirSync(tempDir, { recursive: true }); + } + } catch (error) { + this.logger.error(error); + } } private readonly logger = new Logger(PrismaRepository.name); diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index af5c1a27a..a963b5b15 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -3,16 +3,7 @@ import EventEmitter2 from 'eventemitter2'; import { opendirSync, readdirSync, rmSync } from 'fs'; import { join } from 'path'; -import { - Auth, - CacheConf, - Chatwoot, - ConfigService, - Database, - DelInstance, - HttpServer, - ProviderSession, -} from '../../config/env.config'; +import { CacheConf, Chatwoot, ConfigService, Database, DelInstance, ProviderSession } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config'; import { NotFoundException } from '../../exceptions'; @@ -77,92 +68,102 @@ export class WAMonitoringService { throw new NotFoundException(`Instance "${instanceName}" not found`); } - const instances: any[] = []; - - for await (const [key, value] of Object.entries(this.waInstances)) { - if (value) { - let chatwoot: any; - const urlServer = this.configService.get('SERVER').URL; - - if (this.configService.get('CHATWOOT').ENABLED) { - const findChatwoot = await this.waInstances[key].findChatwoot(); - - if (findChatwoot && findChatwoot.enabled) { - chatwoot = { - ...findChatwoot, - webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`, - }; - } - } - - const findIntegration = { - integration: this.waInstances[key].integration, - token: this.waInstances[key].token, - number: this.waInstances[key].number, - }; - - let integration: any; - if (this.waInstances[key].integration === Integration.WHATSAPP_BUSINESS) { - integration = { - ...findIntegration, - webhookWaBusiness: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`, - }; - } - - const expose = this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES; - - if (value.connectionStatus.state === 'open') { - const instanceData = { - instance: { - instanceName: key, - instanceId: this.waInstances[key].instanceId, - owner: value.wuid, - profileName: (await value.getProfileName()) || 'not loaded', - profilePictureUrl: value.profilePictureUrl, - profileStatus: (await value.getProfileStatus()) || '', - status: value.connectionStatus.state, - }, - }; - - if (expose) { - instanceData.instance['serverUrl'] = this.configService.get('SERVER').URL; - - instanceData.instance['token'] = this.waInstances[key].token; - - if (this.configService.get('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot; - - instanceData.instance['integration'] = integration; - } - - instances.push(instanceData); - } else { - const instanceData = { - instance: { - instanceName: key, - instanceId: this.waInstances[key].instanceId, - status: value.connectionStatus.state, - }, - }; - - if (expose) { - instanceData.instance['serverUrl'] = this.configService.get('SERVER').URL; - - instanceData.instance['token'] = this.waInstances[key].token; - - if (this.configService.get('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot; - - instanceData.instance['integration'] = integration; - } - - instances.push(instanceData); - } - } - } - - if (arrayReturn) { - return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances]; - } - return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; + // const instances: any[] = []; + + return await this.prismaRepository.instance.findMany({ + include: { + Chatwoot: true, + Proxy: true, + Rabbitmq: true, + Sqs: true, + Websocket: true, + Setting: true, + }, + }); + // for await (const [key, value] of Object.entries(this.waInstances)) { + // if (value) { + // let chatwoot: any; + // const urlServer = this.configService.get('SERVER').URL; + + // if (this.configService.get('CHATWOOT').ENABLED) { + // const findChatwoot = await this.waInstances[key].findChatwoot(); + + // if (findChatwoot && findChatwoot.enabled) { + // chatwoot = { + // ...findChatwoot, + // webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`, + // }; + // } + // } + + // const findIntegration = { + // integration: this.waInstances[key].integration, + // token: this.waInstances[key].token, + // number: this.waInstances[key].number, + // }; + + // let integration: any; + // if (this.waInstances[key].integration === Integration.WHATSAPP_BUSINESS) { + // integration = { + // ...findIntegration, + // webhookWaBusiness: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`, + // }; + // } + + // const expose = this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES; + + // if (value.connectionStatus.state === 'open') { + // const instanceData = { + // instance: { + // instanceName: key, + // instanceId: this.waInstances[key].instanceId, + // owner: value.wuid, + // profileName: (await value.getProfileName()) || 'not loaded', + // profilePictureUrl: value.profilePictureUrl, + // profileStatus: (await value.getProfileStatus()) || '', + // status: value.connectionStatus.state, + // }, + // }; + + // if (expose) { + // instanceData.instance['serverUrl'] = this.configService.get('SERVER').URL; + + // instanceData.instance['token'] = this.waInstances[key].token; + + // if (this.configService.get('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot; + + // instanceData.instance['integration'] = integration; + // } + + // instances.push(instanceData); + // } else { + // const instanceData = { + // instance: { + // instanceName: key, + // instanceId: this.waInstances[key].instanceId, + // status: value.connectionStatus.state, + // }, + // }; + + // if (expose) { + // instanceData.instance['serverUrl'] = this.configService.get('SERVER').URL; + + // instanceData.instance['token'] = this.waInstances[key].token; + + // if (this.configService.get('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot; + + // instanceData.instance['integration'] = integration; + // } + + // instances.push(instanceData); + // } + // } + // } + + // if (arrayReturn) { + // return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances]; + // } + // return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; } public async instanceInfoById(instanceId?: string, number?: string) {