Skip to content

Commit ee343f2

Browse files
authored
Update monitor.service.ts
Evolution ap i 1.5.1 - Problemas ao ler / consultar instancias (FETCH_INSTANCES) Ao consultar instancias as mesmas nao apareciam, seja via manager, seja via postman
1 parent bd64b0c commit ee343f2

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

src/whatsapp/services/monitor.service.ts

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { join } from 'path';
77
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
88
import { Logger } from '../../config/logger.config';
99
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
10+
// inserido por francis inicio
11+
import { NotFoundException } from '../../exceptions';
12+
// inserido por francis fim
1013
import { dbserver } from '../../libs/db.connect';
1114
import { RedisCache } from '../../libs/redis.client';
1215
import {
@@ -72,7 +75,7 @@ export class WAMonitoringService {
7275
}, 1000 * 60 * time);
7376
}
7477
}
75-
78+
/* ocultado por francis inicio
7679
public async instanceInfo(instanceName?: string) {
7780
this.logger.verbose('get instance info');
7881
@@ -128,6 +131,96 @@ export class WAMonitoringService {
128131
return instances;
129132
}
130133
134+
ocultado por francis fim */
135+
136+
// inserido por francis inicio
137+
138+
public async instanceInfo(instanceName?: string) {
139+
this.logger.verbose('get instance info');
140+
if (instanceName && !this.waInstances[instanceName]) {
141+
throw new NotFoundException(`Instance "${instanceName}" not found`);
142+
}
143+
144+
const instances: any[] = [];
145+
146+
for await (const [key, value] of Object.entries(this.waInstances)) {
147+
if (value) {
148+
this.logger.verbose('get instance info: ' + key);
149+
let chatwoot: any;
150+
151+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
152+
153+
const findChatwoot = await this.waInstances[key].findChatwoot();
154+
155+
if (findChatwoot && findChatwoot.enabled) {
156+
chatwoot = {
157+
...findChatwoot,
158+
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
159+
};
160+
}
161+
162+
if (value.connectionStatus.state === 'open') {
163+
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
164+
165+
const instanceData = {
166+
instance: {
167+
instanceName: key,
168+
owner: value.wuid,
169+
profileName: (await value.getProfileName()) || 'not loaded',
170+
profilePictureUrl: value.profilePictureUrl,
171+
profileStatus: (await value.getProfileStatus()) || '',
172+
status: value.connectionStatus.state,
173+
},
174+
};
175+
176+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
177+
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
178+
179+
instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;
180+
181+
instanceData.instance['chatwoot'] = chatwoot;
182+
}
183+
184+
instances.push(instanceData);
185+
} else {
186+
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
187+
188+
const instanceData = {
189+
instance: {
190+
instanceName: key,
191+
status: value.connectionStatus.state,
192+
},
193+
};
194+
195+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
196+
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
197+
198+
instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;
199+
200+
instanceData.instance['chatwoot'] = chatwoot;
201+
}
202+
203+
instances.push(instanceData);
204+
}
205+
}
206+
}
207+
208+
this.logger.verbose('return instance info: ' + instances.length);
209+
210+
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
211+
}
212+
213+
214+
215+
// inserido por francis fim
216+
217+
218+
219+
220+
221+
222+
223+
131224
private delInstanceFiles() {
132225
this.logger.verbose('cron to delete instance files started');
133226
setInterval(async () => {

0 commit comments

Comments
 (0)