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 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<T> {
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);

View File

@ -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[] = [];
// const instances: any[] = [];
for await (const [key, value] of Object.entries(this.waInstances)) {
if (value) {
let chatwoot: any;
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
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<HttpServer>('SERVER').URL;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
const findChatwoot = await this.waInstances[key].findChatwoot();
// if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
// const findChatwoot = await this.waInstances[key].findChatwoot();
if (findChatwoot && findChatwoot.enabled) {
chatwoot = {
...findChatwoot,
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
};
}
}
// 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,
};
// 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)}`,
};
}
// let integration: any;
// if (this.waInstances[key].integration === Integration.WHATSAPP_BUSINESS) {
// integration = {
// ...findIntegration,
// 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') {
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 (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<HttpServer>('SERVER').URL;
// if (expose) {
// 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);
} else {
const instanceData = {
instance: {
instanceName: key,
instanceId: this.waInstances[key].instanceId,
status: value.connectionStatus.state,
},
};
// 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<HttpServer>('SERVER').URL;
// if (expose) {
// 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) {
return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances];
}
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
// 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) {