Skip to content

Commit 6029672

Browse files
author
Hamza Shahid
committed
fix: return instance from database without requiring memory load
1 parent 757a883 commit 6029672

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

src/api/services/monitor.service.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,40 @@ export class WAMonitoringService {
130130
}
131131

132132
public async instanceInfoById(instanceId?: string, number?: string) {
133-
let instanceName: string;
133+
const clientName = this.configService.get<Database>('DATABASE').CONNECTION.CLIENT_NAME;
134+
135+
const where: any = { clientName };
134136
if (instanceId) {
135-
instanceName = await this.prismaRepository.instance.findFirst({ where: { id: instanceId } }).then((r) => r?.name);
136-
if (!instanceName) {
137-
throw new NotFoundException(`Instance "${instanceId}" not found`);
138-
}
137+
where.id = instanceId;
139138
} else if (number) {
140-
instanceName = await this.prismaRepository.instance.findFirst({ where: { number } }).then((r) => r?.name);
141-
if (!instanceName) {
142-
throw new NotFoundException(`Instance "${number}" not found`);
143-
}
139+
where.number = number;
144140
}
145141

146-
if (!instanceName) {
147-
throw new NotFoundException(`Instance "${instanceId}" not found`);
148-
}
142+
const instances = await this.prismaRepository.instance.findMany({
143+
where,
144+
include: {
145+
Chatwoot: true,
146+
Proxy: true,
147+
Rabbitmq: true,
148+
Nats: true,
149+
Sqs: true,
150+
Websocket: true,
151+
Setting: true,
152+
_count: {
153+
select: {
154+
Message: true,
155+
Contact: true,
156+
Chat: true,
157+
},
158+
},
159+
},
160+
});
149161

150-
if (instanceName && !this.waInstances[instanceName]) {
151-
throw new NotFoundException(`Instance "${instanceName}" not found`);
162+
if (instances.length === 0) {
163+
throw new NotFoundException(`Instance "${instanceId || number}" not found`);
152164
}
153165

154-
const instanceNames = instanceName ? [instanceName] : null;
155-
156-
return this.instanceInfo(instanceNames);
166+
return instances;
157167
}
158168

159169
public async cleaningUp(instanceName: string) {

0 commit comments

Comments
 (0)