Skip to content

Commit

Permalink
fix: init store folders
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Jun 10, 2024
1 parent 21d9849 commit 7ebf237
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 96 deletions.
26 changes: 26 additions & 0 deletions src/api/repository/repository.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
193 changes: 97 additions & 96 deletions src/api/services/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<HttpServer>('SERVER').URL;

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)}`,
};
}
}

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<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 (expose) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;

instanceData.instance['token'] = this.waInstances[key].token;

if (this.configService.get<Chatwoot>('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<HttpServer>('SERVER').URL;

instanceData.instance['token'] = this.waInstances[key].token;

if (this.configService.get<Chatwoot>('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<HttpServer>('SERVER').URL;

// 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)}`,
// };
// }
// }

// 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<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 (expose) {
// instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;

// instanceData.instance['token'] = this.waInstances[key].token;

// if (this.configService.get<Chatwoot>('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<HttpServer>('SERVER').URL;

// instanceData.instance['token'] = this.waInstances[key].token;

// if (this.configService.get<Chatwoot>('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) {
Expand Down

0 comments on commit 7ebf237

Please sign in to comment.