Skip to content

Commit 6101c8d

Browse files
committed
fix: corrigido disparo de eventos quando nao usa a opcao da ENV de salvar mensagens no banco
1 parent cf95c02 commit 6101c8d

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,16 +1443,7 @@ export class BaileysStartupService extends ChannelStartupService {
14431443
}
14441444
}
14451445

1446-
const findMessage = await this.prismaRepository.message.findFirst({
1447-
where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } },
1448-
});
1449-
1450-
if (!findMessage) {
1451-
continue;
1452-
}
1453-
14541446
const message: any = {
1455-
messageId: findMessage.id,
14561447
keyId: key.id,
14571448
remoteJid: key?.remoteJid,
14581449
fromMe: key.fromMe,
@@ -1462,6 +1453,16 @@ export class BaileysStartupService extends ChannelStartupService {
14621453
instanceId: this.instanceId,
14631454
};
14641455

1456+
let findMessage: any;
1457+
const configDatabaseData = this.configService.get<Database>('DATABASE').SAVE_DATA;
1458+
if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
1459+
findMessage = await this.prismaRepository.message.findFirst({
1460+
where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } },
1461+
});
1462+
1463+
if (findMessage) message.messageId = findMessage.id;
1464+
}
1465+
14651466
if (update.message === null && update.status === undefined) {
14661467
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
14671468

@@ -1477,7 +1478,9 @@ export class BaileysStartupService extends ChannelStartupService {
14771478
}
14781479

14791480
continue;
1480-
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
1481+
}
1482+
1483+
if (findMessage && update.status !== undefined && status[update.status] !== findMessage.status) {
14811484
if (!key.fromMe && key.remoteJid) {
14821485
readChatToUpdate[key.remoteJid] = true;
14831486

@@ -3431,16 +3434,18 @@ export class BaileysStartupService extends ChannelStartupService {
34313434
where: { id: message.id },
34323435
data: { key: { ...existingKey, deleted: true }, status: 'DELETED' },
34333436
});
3434-
const messageUpdate: any = {
3435-
messageId: message.id,
3436-
keyId: messageId,
3437-
remoteJid: response.key.remoteJid,
3438-
fromMe: response.key.fromMe,
3439-
participant: response.key?.remoteJid,
3440-
status: 'DELETED',
3441-
instanceId: this.instanceId,
3442-
};
3443-
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3437+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
3438+
const messageUpdate: any = {
3439+
messageId: message.id,
3440+
keyId: messageId,
3441+
remoteJid: response.key.remoteJid,
3442+
fromMe: response.key.fromMe,
3443+
participant: response.key?.remoteJid,
3444+
status: 'DELETED',
3445+
instanceId: this.instanceId,
3446+
};
3447+
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3448+
}
34443449
} else {
34453450
await this.prismaRepository.message.deleteMany({ where: { id: message.id } });
34463451
}
@@ -3771,6 +3776,10 @@ export class BaileysStartupService extends ChannelStartupService {
37713776

37723777
private async formatUpdateMessage(data: UpdateMessageDto) {
37733778
try {
3779+
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
3780+
return data;
3781+
}
3782+
37743783
const msg: any = await this.getMessage(data.key, true);
37753784

37763785
if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') {
@@ -3804,13 +3813,15 @@ export class BaileysStartupService extends ChannelStartupService {
38043813

38053814
try {
38063815
const oldMessage: any = await this.getMessage(data.key, true);
3807-
if (!oldMessage) throw new NotFoundException('Message not found');
3808-
if (oldMessage?.key?.remoteJid !== jid) {
3809-
throw new BadRequestException('RemoteJid does not match');
3810-
}
3811-
if (oldMessage?.messageTimestamp > Date.now() + 900000) {
3812-
// 15 minutes in milliseconds
3813-
throw new BadRequestException('Message is older than 15 minutes');
3816+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
3817+
if (!oldMessage) throw new NotFoundException('Message not found');
3818+
if (oldMessage?.key?.remoteJid !== jid) {
3819+
throw new BadRequestException('RemoteJid does not match');
3820+
}
3821+
if (oldMessage?.messageTimestamp > Date.now() + 900000) {
3822+
// 15 minutes in milliseconds
3823+
throw new BadRequestException('Message is older than 15 minutes');
3824+
}
38143825
}
38153826

38163827
const messageSent = await this.client.sendMessage(jid, { ...(options as any), edit: data.key });
@@ -3828,7 +3839,7 @@ export class BaileysStartupService extends ChannelStartupService {
38283839
);
38293840

38303841
const messageId = messageSent.message?.protocolMessage?.key?.id;
3831-
if (messageId) {
3842+
if (messageId && this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
38323843
let message = await this.prismaRepository.message.findFirst({
38333844
where: { key: { path: ['id'], equals: messageId } },
38343845
});
@@ -3840,6 +3851,7 @@ export class BaileysStartupService extends ChannelStartupService {
38403851
if ((message.key.valueOf() as any)?.deleted) {
38413852
new BadRequestException('You cannot edit deleted messages');
38423853
}
3854+
38433855
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
38443856
oldMessage.message.conversation = data.text;
38453857
} else {
@@ -3853,16 +3865,19 @@ export class BaileysStartupService extends ChannelStartupService {
38533865
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
38543866
},
38553867
});
3856-
const messageUpdate: any = {
3857-
messageId: message.id,
3858-
keyId: messageId,
3859-
remoteJid: messageSent.key.remoteJid,
3860-
fromMe: messageSent.key.fromMe,
3861-
participant: messageSent.key?.remoteJid,
3862-
status: 'EDITED',
3863-
instanceId: this.instanceId,
3864-
};
3865-
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3868+
3869+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
3870+
const messageUpdate: any = {
3871+
messageId: message.id,
3872+
keyId: messageId,
3873+
remoteJid: messageSent.key.remoteJid,
3874+
fromMe: messageSent.key.fromMe,
3875+
participant: messageSent.key?.remoteJid,
3876+
status: 'EDITED',
3877+
instanceId: this.instanceId,
3878+
};
3879+
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3880+
}
38663881
}
38673882
}
38683883
}

0 commit comments

Comments
 (0)