Skip to content

Commit 03a44cf

Browse files
Merge pull request #1798 from apresentame/fix/webhook_event
Permitir correta utilização da evolution quando mensagens não são persistidas no DB
2 parents 96f4b80 + 79f4a22 commit 03a44cf

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

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

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,16 +1446,7 @@ export class BaileysStartupService extends ChannelStartupService {
14461446
}
14471447
}
14481448

1449-
const findMessage = await this.prismaRepository.message.findFirst({
1450-
where: { instanceId: this.instanceId, key: { path: ['id'], equals: key.id } },
1451-
});
1452-
1453-
if (!findMessage) {
1454-
continue;
1455-
}
1456-
14571449
const message: any = {
1458-
messageId: findMessage.id,
14591450
keyId: key.id,
14601451
remoteJid: key?.remoteJid,
14611452
fromMe: key.fromMe,
@@ -1465,6 +1456,16 @@ export class BaileysStartupService extends ChannelStartupService {
14651456
instanceId: this.instanceId,
14661457
};
14671458

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

@@ -1480,7 +1481,9 @@ export class BaileysStartupService extends ChannelStartupService {
14801481
}
14811482

14821483
continue;
1483-
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
1484+
}
1485+
1486+
if (findMessage && update.status !== undefined && status[update.status] !== findMessage.status) {
14841487
if (!key.fromMe && key.remoteJid) {
14851488
readChatToUpdate[key.remoteJid] = true;
14861489

@@ -3438,17 +3441,20 @@ export class BaileysStartupService extends ChannelStartupService {
34383441
where: { id: message.id },
34393442
data: { key: { ...existingKey, deleted: true }, status: 'DELETED' },
34403443
});
3441-
const messageUpdate: any = {
3442-
messageId: message.id,
3443-
keyId: messageId,
3444-
remoteJid: response.key.remoteJid,
3445-
fromMe: response.key.fromMe,
3446-
participant: response.key?.remoteJid,
3447-
status: 'DELETED',
3448-
instanceId: this.instanceId,
3449-
};
3450-
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3444+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
3445+
const messageUpdate: any = {
3446+
messageId: message.id,
3447+
keyId: messageId,
3448+
remoteJid: response.key.remoteJid,
3449+
fromMe: response.key.fromMe,
3450+
participant: response.key?.remoteJid,
3451+
status: 'DELETED',
3452+
instanceId: this.instanceId,
3453+
};
3454+
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3455+
}
34513456
} else {
3457+
if (!message) return response;
34523458
await this.prismaRepository.message.deleteMany({ where: { id: message.id } });
34533459
}
34543460
this.sendDataWebhook(Events.MESSAGES_DELETE, {
@@ -3780,6 +3786,10 @@ export class BaileysStartupService extends ChannelStartupService {
37803786

37813787
private async formatUpdateMessage(data: UpdateMessageDto) {
37823788
try {
3789+
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
3790+
return data;
3791+
}
3792+
37833793
const msg: any = await this.getMessage(data.key, true);
37843794

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

38143824
try {
38153825
const oldMessage: any = await this.getMessage(data.key, true);
3816-
if (!oldMessage) throw new NotFoundException('Message not found');
3817-
if (oldMessage?.key?.remoteJid !== jid) {
3818-
throw new BadRequestException('RemoteJid does not match');
3819-
}
3820-
if (oldMessage?.messageTimestamp > Date.now() + 900000) {
3821-
// 15 minutes in milliseconds
3822-
throw new BadRequestException('Message is older than 15 minutes');
3826+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
3827+
if (!oldMessage) throw new NotFoundException('Message not found');
3828+
if (oldMessage?.key?.remoteJid !== jid) {
3829+
throw new BadRequestException('RemoteJid does not match');
3830+
}
3831+
if (oldMessage?.messageTimestamp > Date.now() + 900000) {
3832+
// 15 minutes in milliseconds
3833+
throw new BadRequestException('Message is older than 15 minutes');
3834+
}
38233835
}
38243836

38253837
const messageSent = await this.client.sendMessage(jid, { ...(options as any), edit: data.key });
@@ -3837,7 +3849,7 @@ export class BaileysStartupService extends ChannelStartupService {
38373849
);
38383850

38393851
const messageId = messageSent.message?.protocolMessage?.key?.id;
3840-
if (messageId) {
3852+
if (messageId && this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
38413853
let message = await this.prismaRepository.message.findFirst({
38423854
where: { key: { path: ['id'], equals: messageId } },
38433855
});
@@ -3849,6 +3861,7 @@ export class BaileysStartupService extends ChannelStartupService {
38493861
if ((message.key.valueOf() as any)?.deleted) {
38503862
new BadRequestException('You cannot edit deleted messages');
38513863
}
3864+
38523865
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
38533866
oldMessage.message.conversation = data.text;
38543867
} else {
@@ -3862,16 +3875,19 @@ export class BaileysStartupService extends ChannelStartupService {
38623875
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
38633876
},
38643877
});
3865-
const messageUpdate: any = {
3866-
messageId: message.id,
3867-
keyId: messageId,
3868-
remoteJid: messageSent.key.remoteJid,
3869-
fromMe: messageSent.key.fromMe,
3870-
participant: messageSent.key?.remoteJid,
3871-
status: 'EDITED',
3872-
instanceId: this.instanceId,
3873-
};
3874-
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3878+
3879+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
3880+
const messageUpdate: any = {
3881+
messageId: message.id,
3882+
keyId: messageId,
3883+
remoteJid: messageSent.key.remoteJid,
3884+
fromMe: messageSent.key.fromMe,
3885+
participant: messageSent.key?.remoteJid,
3886+
status: 'EDITED',
3887+
instanceId: this.instanceId,
3888+
};
3889+
await this.prismaRepository.messageUpdate.create({ data: messageUpdate });
3890+
}
38753891
}
38763892
}
38773893
}

0 commit comments

Comments
 (0)