Skip to content

Commit 2e67c77

Browse files
author
Hamza Shahid
committed
fix: handle undefined instance in sendMessage controller with proper 404
1 parent 6029672 commit 2e67c77

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

src/api/controllers/sendMessage.controller.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
SendTextDto,
1616
} from '@api/dto/sendMessage.dto';
1717
import { WAMonitoringService } from '@api/services/monitor.service';
18-
import { BadRequestException } from '@exceptions';
18+
import { BadRequestException, NotFoundException } from '@exceptions';
1919
import { isBase64, isURL } from 'class-validator';
2020
import emojiRegex from 'emoji-regex';
2121

@@ -31,12 +31,22 @@ function isEmoji(str: string) {
3131
export class SendMessageController {
3232
constructor(private readonly waMonitor: WAMonitoringService) {}
3333

34+
private getInstance(instanceName: string) {
35+
const instance = this.waMonitor.waInstances[instanceName];
36+
if (!instance) {
37+
throw new NotFoundException(
38+
`Instance "${instanceName}" not found or not connected. Please connect the instance first.`,
39+
);
40+
}
41+
return instance;
42+
}
43+
3444
public async sendTemplate({ instanceName }: InstanceDto, data: SendTemplateDto) {
35-
return await this.waMonitor.waInstances[instanceName].templateMessage(data);
45+
return await this.getInstance(instanceName).templateMessage(data);
3646
}
3747

3848
public async sendText({ instanceName }: InstanceDto, data: SendTextDto) {
39-
return await this.waMonitor.waInstances[instanceName].textMessage(data);
49+
return await this.getInstance(instanceName).textMessage(data);
4050
}
4151

4252
public async sendMedia({ instanceName }: InstanceDto, data: SendMediaDto, file?: any) {
@@ -45,63 +55,62 @@ export class SendMessageController {
4555
}
4656

4757
if (file || isURL(data?.media) || isBase64(data?.media)) {
48-
return await this.waMonitor.waInstances[instanceName].mediaMessage(data, file);
58+
return await this.getInstance(instanceName).mediaMessage(data, file);
4959
}
5060
throw new BadRequestException('Owned media must be a url or base64');
5161
}
5262

5363
public async sendPtv({ instanceName }: InstanceDto, data: SendPtvDto, file?: any) {
5464
if (file || isURL(data?.video) || isBase64(data?.video)) {
55-
return await this.waMonitor.waInstances[instanceName].ptvMessage(data, file);
65+
return await this.getInstance(instanceName).ptvMessage(data, file);
5666
}
5767
throw new BadRequestException('Owned media must be a url or base64');
5868
}
5969

6070
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto, file?: any) {
6171
if (file || isURL(data.sticker) || isBase64(data.sticker)) {
62-
return await this.waMonitor.waInstances[instanceName].mediaSticker(data, file);
72+
return await this.getInstance(instanceName).mediaSticker(data, file);
6373
}
6474
throw new BadRequestException('Owned media must be a url or base64');
6575
}
6676

6777
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) {
6878
if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) {
69-
// Si file existe y tiene buffer, o si es una URL o Base64, continúa
70-
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
79+
return await this.getInstance(instanceName).audioWhatsapp(data, file);
7180
} else {
7281
console.error('El archivo no tiene buffer o el audio no es una URL o Base64 válida');
7382
throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer');
7483
}
7584
}
7685

7786
public async sendButtons({ instanceName }: InstanceDto, data: SendButtonsDto) {
78-
return await this.waMonitor.waInstances[instanceName].buttonMessage(data);
87+
return await this.getInstance(instanceName).buttonMessage(data);
7988
}
8089

8190
public async sendLocation({ instanceName }: InstanceDto, data: SendLocationDto) {
82-
return await this.waMonitor.waInstances[instanceName].locationMessage(data);
91+
return await this.getInstance(instanceName).locationMessage(data);
8392
}
8493

8594
public async sendList({ instanceName }: InstanceDto, data: SendListDto) {
86-
return await this.waMonitor.waInstances[instanceName].listMessage(data);
95+
return await this.getInstance(instanceName).listMessage(data);
8796
}
8897

8998
public async sendContact({ instanceName }: InstanceDto, data: SendContactDto) {
90-
return await this.waMonitor.waInstances[instanceName].contactMessage(data);
99+
return await this.getInstance(instanceName).contactMessage(data);
91100
}
92101

93102
public async sendReaction({ instanceName }: InstanceDto, data: SendReactionDto) {
94103
if (!isEmoji(data.reaction)) {
95104
throw new BadRequestException('Reaction must be a single emoji or empty string');
96105
}
97-
return await this.waMonitor.waInstances[instanceName].reactionMessage(data);
106+
return await this.getInstance(instanceName).reactionMessage(data);
98107
}
99108

100109
public async sendPoll({ instanceName }: InstanceDto, data: SendPollDto) {
101-
return await this.waMonitor.waInstances[instanceName].pollMessage(data);
110+
return await this.getInstance(instanceName).pollMessage(data);
102111
}
103112

104113
public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto, file?: any) {
105-
return await this.waMonitor.waInstances[instanceName].statusMessage(data, file);
114+
return await this.getInstance(instanceName).statusMessage(data, file);
106115
}
107116
}

0 commit comments

Comments
 (0)