Skip to content

Commit 5c285be

Browse files
matheusmartinsInspermatheusmartinsInsper
authored andcommitted
Refactor BaileysStartupService updateMessage method
1 parent f31fe50 commit 5c285be

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

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

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,7 +3290,7 @@ export class BaileysStartupService extends ChannelStartupService {
32903290
}
32913291

32923292
public async updateMessage(data: UpdateMessageDto) {
3293-
const jid = createJid(data.number);
3293+
const jid = this.createJid(data.number);
32943294

32953295
const options = await this.formatUpdateMessage(data);
32963296

@@ -3300,13 +3300,72 @@ export class BaileysStartupService extends ChannelStartupService {
33003300
}
33013301

33023302
try {
3303-
return await this.client.sendMessage(jid, {
3303+
const response = await this.client.sendMessage(jid, {
33043304
...(options as any),
33053305
edit: data.key,
33063306
});
3307+
if (response) {
3308+
const messageId = response.message?.protocolMessage?.key?.id;
3309+
if (messageId) {
3310+
let message = await this.prismaRepository.message.findFirst({
3311+
where: {
3312+
key: {
3313+
path: ['id'],
3314+
equals: messageId,
3315+
},
3316+
},
3317+
});
3318+
if (!message) throw new NotFoundException('Message not found');
3319+
3320+
if (!(message.key.valueOf() as any).fromMe) {
3321+
new BadRequestException('You cannot edit others messages');
3322+
}
3323+
if ((message.key.valueOf() as any)?.deleted) {
3324+
new BadRequestException('You cannot edit deleted messages');
3325+
}
3326+
3327+
const updateMessage = this.prepareMessage({ ...response });
3328+
message = await this.prismaRepository.message.update({
3329+
where: { id: message.id },
3330+
data: {
3331+
message: {
3332+
...updateMessage?.message?.[updateMessage.messageType]?.editedMessage,
3333+
},
3334+
status: 'EDITED',
3335+
},
3336+
});
3337+
const messageUpdate: any = {
3338+
messageId: message.id,
3339+
keyId: messageId,
3340+
remoteJid: response.key.remoteJid,
3341+
fromMe: response.key.fromMe,
3342+
participant: response.key?.remoteJid,
3343+
status: 'EDITED',
3344+
instanceId: this.instanceId,
3345+
};
3346+
await this.prismaRepository.messageUpdate.create({
3347+
data: messageUpdate,
3348+
});
3349+
3350+
this.sendDataWebhook(Events.MESSAGES_EDITED, {
3351+
id: message.id,
3352+
instanceId: message.instanceId,
3353+
key: message.key,
3354+
messageType: message.messageType,
3355+
status: 'EDITED',
3356+
source: message.source,
3357+
messageTimestamp: message.messageTimestamp,
3358+
pushName: message.pushName,
3359+
participant: message.participant,
3360+
message: message.message,
3361+
});
3362+
}
3363+
}
3364+
3365+
return response;
33073366
} catch (error) {
33083367
this.logger.error(error);
3309-
throw new BadRequestException(error.toString());
3368+
throw error;
33103369
}
33113370
}
33123371

0 commit comments

Comments
 (0)