Skip to content

Commit 7cc324e

Browse files
committed
fix: include instance Id field in the instance configuration
1 parent cf89601 commit 7cc324e

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

src/whatsapp/controllers/instance.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,13 @@ export class InstanceController {
591591
};
592592
}
593593

594-
public async fetchInstances({ instanceName }: InstanceDto) {
594+
public async fetchInstances({ instanceName, instanceId }: InstanceDto) {
595595
if (instanceName) {
596596
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
597597
this.logger.verbose('instanceName: ' + instanceName);
598598
return this.waMonitor.instanceInfo(instanceName);
599+
} else if (instanceId) {
600+
return this.waMonitor.instanceInfoById(instanceId);
599601
}
600602

601603
this.logger.verbose('requested fetchInstances (all instances)');

src/whatsapp/repository/auth.repository.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Auth, ConfigService } from '../../config/env.config';
55
import { Logger } from '../../config/logger.config';
66
import { AUTH_DIR } from '../../config/path.config';
77
import { IInsert, Repository } from '../abstract/abstract.repository';
8+
import { InstanceDto } from '../dto/instance.dto';
89
import { AuthRaw, IAuthModel } from '../models';
910

1011
export class AuthRepository extends Repository {
@@ -63,4 +64,20 @@ export class AuthRepository extends Repository {
6364
return {};
6465
}
6566
}
67+
68+
public async findInstanceNameById(instanceId: string): Promise<string> {
69+
try {
70+
this.logger.verbose('finding auth by instanceId');
71+
if (this.dbSettings.ENABLED) {
72+
this.logger.verbose('finding auth in db');
73+
const response = await this.authModel.findOne({ instanceId });
74+
75+
return response._id;
76+
}
77+
78+
this.logger.verbose('finding auth in store is not supported');
79+
} catch (error) {
80+
return '';
81+
}
82+
}
6683
}

src/whatsapp/services/monitor.service.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,84 @@ export class WAMonitoringService {
159159
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
160160
}
161161

162+
public async instanceInfoById(instanceId?: string) {
163+
this.logger.verbose('get instance info');
164+
const instanceName = await this.repository.auth.findInstanceNameById(instanceId);
165+
if (instanceName && !this.waInstances[instanceName]) {
166+
throw new NotFoundException(`Instance "${instanceName}" not found`);
167+
}
168+
169+
const instances: any[] = [];
170+
171+
for await (const [key, value] of Object.entries(this.waInstances)) {
172+
if (value) {
173+
this.logger.verbose('get instance info: ' + key);
174+
let chatwoot: any;
175+
176+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
177+
178+
const findChatwoot = await this.waInstances[key].findChatwoot();
179+
180+
if (findChatwoot && findChatwoot.enabled) {
181+
chatwoot = {
182+
...findChatwoot,
183+
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
184+
};
185+
}
186+
187+
if (value.connectionStatus.state === 'open') {
188+
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
189+
190+
const instanceData = {
191+
instance: {
192+
instanceName: key,
193+
instanceId: (await this.repository.auth.find(key))?.instanceId,
194+
owner: value.wuid,
195+
profileName: (await value.getProfileName()) || 'not loaded',
196+
profilePictureUrl: value.profilePictureUrl,
197+
profileStatus: (await value.getProfileStatus()) || '',
198+
status: value.connectionStatus.state,
199+
},
200+
};
201+
202+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
203+
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
204+
205+
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
206+
207+
instanceData.instance['chatwoot'] = chatwoot;
208+
}
209+
210+
instances.push(instanceData);
211+
} else {
212+
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
213+
214+
const instanceData = {
215+
instance: {
216+
instanceName: key,
217+
instanceId: (await this.repository.auth.find(key))?.instanceId,
218+
status: value.connectionStatus.state,
219+
},
220+
};
221+
222+
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
223+
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
224+
225+
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
226+
227+
instanceData.instance['chatwoot'] = chatwoot;
228+
}
229+
230+
instances.push(instanceData);
231+
}
232+
}
233+
}
234+
235+
this.logger.verbose('return instance info: ' + instances.length);
236+
237+
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
238+
}
239+
162240
private delInstanceFiles() {
163241
this.logger.verbose('cron to delete instance files started');
164242
setInterval(async () => {

0 commit comments

Comments
 (0)