Skip to content

Commit 7d9dd64

Browse files
authored
Update monitor.service.ts
Isso permite que as operações assíncronas sejam executadas em paralelo
1 parent 41bea89 commit 7d9dd64

1 file changed

Lines changed: 41 additions & 60 deletions

File tree

src/whatsapp/services/monitor.service.ts

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -74,81 +74,62 @@ export class WAMonitoringService {
7474
}
7575
}
7676

77-
public async instanceInfo(instanceName?: string) {
77+
public async instanceInfo(instanceName?: string) {
7878
this.logger.verbose('get instance info');
79-
if (instanceName && !this.waInstances[instanceName]) {
80-
throw new NotFoundException(`Instance "${instanceName}" not found`);
81-
}
82-
83-
const instances: any[] = [];
84-
85-
for await (const [key, value] of Object.entries(this.waInstances)) {
86-
if (value) {
87-
this.logger.verbose('get instance info: ' + key);
88-
let chatwoot: any;
89-
90-
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
91-
92-
const findChatwoot = await this.waInstances[key].findChatwoot();
93-
94-
if (findChatwoot && findChatwoot.enabled) {
95-
chatwoot = {
96-
...findChatwoot,
97-
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
98-
};
99-
}
10079

101-
if (value.connectionStatus.state === 'open') {
102-
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
80+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
10381

104-
const instanceData = {
82+
const instances: any[] = await Promise.all(
83+
Object.entries(this.waInstances).map(async ([key, value]) => {
84+
if (!value || !value.connectionStatus || value.connectionStatus.state !== 'open') {
85+
return {
10586
instance: {
10687
instanceName: key,
107-
owner: value.wuid,
108-
profileName: (await value.getProfileName()) || 'not loaded',
109-
profilePictureUrl: value.profilePictureUrl,
110-
profileStatus: (await value.getProfileStatus()) || '',
111-
status: value.connectionStatus.state,
88+
status: value?.connectionStatus?.state || 'unknown',
11289
},
11390
};
91+
}
11492

115-
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
116-
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
117-
118-
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
119-
120-
instanceData.instance['chatwoot'] = chatwoot;
93+
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
94+
95+
const instanceData: any = {
96+
instance: {
97+
instanceName: key,
98+
owner: value.wuid,
99+
profileName: (await value.getProfileName()) || 'not loaded',
100+
profilePictureUrl: value.profilePictureUrl,
101+
profileStatus: (await value.getProfileStatus()) || '',
102+
status: 'open',
103+
},
104+
};
105+
106+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
107+
instanceData.instance.serverUrl = urlServer;
108+
instanceData.instance.apikey = (await this.repository.auth.find(key))?.apikey;
109+
110+
const findChatwoot = await this.waInstances[key].findChatwoot();
111+
if (findChatwoot && findChatwoot.enabled) {
112+
instanceData.instance.chatwoot = {
113+
...findChatwoot,
114+
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
115+
};
121116
}
117+
}
122118

123-
instances.push(instanceData);
124-
} else {
125-
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
126-
127-
const instanceData = {
128-
instance: {
129-
instanceName: key,
130-
status: value.connectionStatus.state,
131-
},
132-
};
133-
134-
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
135-
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
136-
137-
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
119+
return instanceData;
120+
}),
121+
);
138122

139-
instanceData.instance['chatwoot'] = chatwoot;
140-
}
123+
this.logger.verbose('return instance info: ' + instances.length);
141124

142-
instances.push(instanceData);
143-
}
144-
}
125+
if (instanceName) {
126+
const instance = instances.find((i) => i.instance.instanceName === instanceName);
127+
return instance || [];
145128
}
146129

147-
this.logger.verbose('return instance info: ' + instances.length);
148-
149-
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
130+
return instances;
150131
}
151-
132+
152133
private delInstanceFiles() {
153134
this.logger.verbose('cron to delete instance files started');
154135
setInterval(async () => {

0 commit comments

Comments
 (0)