fix: init store folders

This commit is contained in:
Davidson Gomes 2024-06-10 11:28:28 -03:00
parent 21d984969a
commit 7ebf237c1c
2 changed files with 105 additions and 78 deletions

View File

@ -1,4 +1,6 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import fs from 'fs';
import { join } from 'path';
import { ConfigService } from '../../config/env.config'; import { ConfigService } from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
@ -13,6 +15,30 @@ export class Query<T> {
export class PrismaRepository extends PrismaClient { export class PrismaRepository extends PrismaClient {
constructor(private readonly configService: ConfigService) { constructor(private readonly configService: ConfigService) {
super(); 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); private readonly logger = new Logger(PrismaRepository.name);

View File

@ -3,16 +3,7 @@ import EventEmitter2 from 'eventemitter2';
import { opendirSync, readdirSync, rmSync } from 'fs'; import { opendirSync, readdirSync, rmSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import { import { CacheConf, Chatwoot, ConfigService, Database, DelInstance, ProviderSession } from '../../config/env.config';
Auth,
CacheConf,
Chatwoot,
ConfigService,
Database,
DelInstance,
HttpServer,
ProviderSession,
} from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config'; import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
import { NotFoundException } from '../../exceptions'; import { NotFoundException } from '../../exceptions';
@ -77,92 +68,102 @@ export class WAMonitoringService {
throw new NotFoundException(`Instance "${instanceName}" not found`); throw new NotFoundException(`Instance "${instanceName}" not found`);
} }
const instances: any[] = []; // const instances: any[] = [];
for await (const [key, value] of Object.entries(this.waInstances)) { return await this.prismaRepository.instance.findMany({
if (value) { include: {
let chatwoot: any; Chatwoot: true,
const urlServer = this.configService.get<HttpServer>('SERVER').URL; 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<HttpServer>('SERVER').URL;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) { // if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
const findChatwoot = await this.waInstances[key].findChatwoot(); // const findChatwoot = await this.waInstances[key].findChatwoot();
if (findChatwoot && findChatwoot.enabled) { // if (findChatwoot && findChatwoot.enabled) {
chatwoot = { // chatwoot = {
...findChatwoot, // ...findChatwoot,
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`, // webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
}; // };
} // }
} // }
const findIntegration = { // const findIntegration = {
integration: this.waInstances[key].integration, // integration: this.waInstances[key].integration,
token: this.waInstances[key].token, // token: this.waInstances[key].token,
number: this.waInstances[key].number, // number: this.waInstances[key].number,
}; // };
let integration: any; // let integration: any;
if (this.waInstances[key].integration === Integration.WHATSAPP_BUSINESS) { // if (this.waInstances[key].integration === Integration.WHATSAPP_BUSINESS) {
integration = { // integration = {
...findIntegration, // ...findIntegration,
webhookWaBusiness: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`, // webhookWaBusiness: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`,
}; // };
} // }
const expose = this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES; // const expose = this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
if (value.connectionStatus.state === 'open') { // if (value.connectionStatus.state === 'open') {
const instanceData = { // const instanceData = {
instance: { // instance: {
instanceName: key, // instanceName: key,
instanceId: this.waInstances[key].instanceId, // instanceId: this.waInstances[key].instanceId,
owner: value.wuid, // owner: value.wuid,
profileName: (await value.getProfileName()) || 'not loaded', // profileName: (await value.getProfileName()) || 'not loaded',
profilePictureUrl: value.profilePictureUrl, // profilePictureUrl: value.profilePictureUrl,
profileStatus: (await value.getProfileStatus()) || '', // profileStatus: (await value.getProfileStatus()) || '',
status: value.connectionStatus.state, // status: value.connectionStatus.state,
}, // },
}; // };
if (expose) { // if (expose) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL; // instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
instanceData.instance['token'] = this.waInstances[key].token; // instanceData.instance['token'] = this.waInstances[key].token;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot; // if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot;
instanceData.instance['integration'] = integration; // instanceData.instance['integration'] = integration;
} // }
instances.push(instanceData); // instances.push(instanceData);
} else { // } else {
const instanceData = { // const instanceData = {
instance: { // instance: {
instanceName: key, // instanceName: key,
instanceId: this.waInstances[key].instanceId, // instanceId: this.waInstances[key].instanceId,
status: value.connectionStatus.state, // status: value.connectionStatus.state,
}, // },
}; // };
if (expose) { // if (expose) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL; // instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
instanceData.instance['token'] = this.waInstances[key].token; // instanceData.instance['token'] = this.waInstances[key].token;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot; // if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot;
instanceData.instance['integration'] = integration; // instanceData.instance['integration'] = integration;
} // }
instances.push(instanceData); // instances.push(instanceData);
} // }
} // }
} // }
if (arrayReturn) { // if (arrayReturn) {
return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances]; // return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances];
} // }
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) { public async instanceInfoById(instanceId?: string, number?: string) {