Skip to content

Commit 7e65cb1

Browse files
committed
fix: find intance by number
1 parent a931ee7 commit 7e65cb1

File tree

4 files changed

+44
-88
lines changed

4 files changed

+44
-88
lines changed

src/whatsapp/controllers/instance.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,13 @@ export class InstanceController {
642642
};
643643
}
644644

645-
public async fetchInstances({ instanceName, instanceId }: InstanceDto) {
645+
public async fetchInstances({ instanceName, instanceId, number }: InstanceDto) {
646646
if (instanceName) {
647647
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
648648
this.logger.verbose('instanceName: ' + instanceName);
649649
return this.waMonitor.instanceInfo(instanceName);
650-
} else if (instanceId) {
651-
return this.waMonitor.instanceInfoById(instanceId);
650+
} else if (instanceId || number) {
651+
return this.waMonitor.instanceInfoById(instanceId, number);
652652
}
653653

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

src/whatsapp/repository/auth.repository.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ 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 { AuthRaw, IAuthModel } from '../models';
8+
import { AuthRaw, IAuthModel, IntegrationModel } from '../models';
99

1010
export class AuthRepository extends Repository {
11-
constructor(private readonly authModel: IAuthModel, readonly configService: ConfigService) {
11+
constructor(
12+
private readonly authModel: IAuthModel,
13+
private readonly integrationModel: IntegrationModel,
14+
readonly configService: ConfigService,
15+
) {
1216
super(configService);
1317
this.auth = configService.get<Auth>('AUTHENTICATION');
1418
}
@@ -110,4 +114,22 @@ export class AuthRepository extends Repository {
110114
return null;
111115
}
112116
}
117+
118+
public async findInstanceNameByNumber(number: string): Promise<string | null> {
119+
try {
120+
this.logger.verbose('finding auth by number');
121+
if (this.dbSettings.ENABLED) {
122+
this.logger.verbose('finding auth in db');
123+
const instance = await this.integrationModel.findOne({ number });
124+
125+
const response = await this.authModel.findOne({ _id: instance._id });
126+
127+
return response._id;
128+
}
129+
130+
this.logger.verbose('finding auth in store is not supported');
131+
} catch (error) {
132+
return null;
133+
}
134+
}
113135
}

src/whatsapp/services/monitor.service.ts

Lines changed: 16 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class WAMonitoringService {
107107
};
108108
}
109109

110-
const findIntegration = await this.repository.integration.find(key);
110+
const findIntegration = await this.waInstances[key].findIntegration();
111111
const integration = {
112112
...findIntegration,
113113
webhook_wa_business: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`,
@@ -170,9 +170,21 @@ export class WAMonitoringService {
170170
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
171171
}
172172

173-
public async instanceInfoById(instanceId?: string) {
173+
public async instanceInfoById(instanceId?: string, number?: string) {
174174
this.logger.verbose('get instance info');
175-
const instanceName = await this.repository.auth.findInstanceNameById(instanceId);
175+
let instanceName: string;
176+
if (instanceId) {
177+
instanceName = await this.repository.auth.findInstanceNameById(instanceId);
178+
if (!instanceName) {
179+
throw new NotFoundException(`Instance "${instanceId}" not found`);
180+
}
181+
} else if (number) {
182+
instanceName = await this.repository.auth.findInstanceNameByNumber(number);
183+
if (!instanceName) {
184+
throw new NotFoundException(`Instance "${number}" not found`);
185+
}
186+
}
187+
176188
if (!instanceName) {
177189
throw new NotFoundException(`Instance "${instanceId}" not found`);
178190
}
@@ -181,85 +193,7 @@ export class WAMonitoringService {
181193
throw new NotFoundException(`Instance "${instanceName}" not found`);
182194
}
183195

184-
const instances: any[] = [];
185-
186-
for await (const [key, value] of Object.entries(this.waInstances)) {
187-
if (value) {
188-
this.logger.verbose('get instance info: ' + key);
189-
let chatwoot: any;
190-
191-
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
192-
193-
const findChatwoot = await this.waInstances[key].findChatwoot();
194-
195-
if (findChatwoot && findChatwoot.enabled) {
196-
chatwoot = {
197-
...findChatwoot,
198-
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
199-
};
200-
}
201-
202-
const findIntegration = await this.repository.integration.find(key);
203-
const integration = {
204-
...findIntegration,
205-
webhook_wa_business: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`,
206-
};
207-
208-
if (value.connectionStatus.state === 'open') {
209-
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
210-
211-
const instanceData = {
212-
instance: {
213-
instanceName: key,
214-
instanceId: (await this.repository.auth.find(key))?.instanceId,
215-
owner: value.wuid,
216-
profileName: (await value.getProfileName()) || 'not loaded',
217-
profilePictureUrl: value.profilePictureUrl,
218-
profileStatus: (await value.getProfileStatus()) || '',
219-
status: value.connectionStatus.state,
220-
},
221-
};
222-
223-
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
224-
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
225-
226-
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
227-
228-
instanceData.instance['chatwoot'] = chatwoot;
229-
230-
instanceData.instance['integration'] = integration;
231-
}
232-
233-
instances.push(instanceData);
234-
} else {
235-
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);
236-
237-
const instanceData = {
238-
instance: {
239-
instanceName: key,
240-
instanceId: (await this.repository.auth.find(key))?.instanceId,
241-
status: value.connectionStatus.state,
242-
},
243-
};
244-
245-
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
246-
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
247-
248-
instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey;
249-
250-
instanceData.instance['chatwoot'] = chatwoot;
251-
252-
instanceData.instance['integration'] = integration;
253-
}
254-
255-
instances.push(instanceData);
256-
}
257-
}
258-
}
259-
260-
this.logger.verbose('return instance info: ' + instances.length);
261-
262-
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
196+
return this.instanceInfo(instanceName);
263197
}
264198

265199
private delInstanceFiles() {

src/whatsapp/whatsapp.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const sqsRepository = new SqsRepository(SqsModel, configService);
8383
const integrationRepository = new IntegrationRepository(IntegrationModel, configService);
8484
const chatwootRepository = new ChatwootRepository(ChatwootModel, configService);
8585
const settingsRepository = new SettingsRepository(SettingsModel, configService);
86-
const authRepository = new AuthRepository(AuthModel, configService);
86+
const authRepository = new AuthRepository(AuthModel, IntegrationModel, configService);
8787
const labelRepository = new LabelRepository(LabelModel, configService);
8888

8989
export const repository = new RepositoryBroker(

0 commit comments

Comments
 (0)