Skip to content

Commit 96821f5

Browse files
committed
Refactor BaileysStartupService updateMessage method
1 parent c1494ca commit 96821f5

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
@@ -3888,7 +3888,7 @@ export class BaileysStartupService extends ChannelStartupService {
38883888
}
38893889

38903890
public async updateMessage(data: UpdateMessageDto) {
3891-
const jid = createJid(data.number);
3891+
const jid = this.createJid(data.number);
38923892

38933893
const options = await this.formatUpdateMessage(data);
38943894

@@ -3898,13 +3898,72 @@ export class BaileysStartupService extends ChannelStartupService {
38983898
}
38993899

39003900
try {
3901-
return await this.client.sendMessage(jid, {
3901+
const response = await this.client.sendMessage(jid, {
39023902
...(options as any),
39033903
edit: data.key,
39043904
});
3905+
if (response) {
3906+
const messageId = response.message?.protocolMessage?.key?.id;
3907+
if (messageId) {
3908+
let message = await this.prismaRepository.message.findFirst({
3909+
where: {
3910+
key: {
3911+
path: ['id'],
3912+
equals: messageId,
3913+
},
3914+
},
3915+
});
3916+
if (!message) throw new NotFoundException('Message not found');
3917+
3918+
if (!(message.key.valueOf() as any).fromMe) {
3919+
new BadRequestException('You cannot edit others messages');
3920+
}
3921+
if ((message.key.valueOf() as any)?.deleted) {
3922+
new BadRequestException('You cannot edit deleted messages');
3923+
}
3924+
3925+
const updateMessage = this.prepareMessage({ ...response });
3926+
message = await this.prismaRepository.message.update({
3927+
where: { id: message.id },
3928+
data: {
3929+
message: {
3930+
...updateMessage?.message?.[updateMessage.messageType]?.editedMessage,
3931+
},
3932+
status: 'EDITED',
3933+
},
3934+
});
3935+
const messageUpdate: any = {
3936+
messageId: message.id,
3937+
keyId: messageId,
3938+
remoteJid: response.key.remoteJid,
3939+
fromMe: response.key.fromMe,
3940+
participant: response.key?.remoteJid,
3941+
status: 'EDITED',
3942+
instanceId: this.instanceId,
3943+
};
3944+
await this.prismaRepository.messageUpdate.create({
3945+
data: messageUpdate,
3946+
});
3947+
3948+
this.sendDataWebhook(Events.MESSAGES_EDITED, {
3949+
id: message.id,
3950+
instanceId: message.instanceId,
3951+
key: message.key,
3952+
messageType: message.messageType,
3953+
status: 'EDITED',
3954+
source: message.source,
3955+
messageTimestamp: message.messageTimestamp,
3956+
pushName: message.pushName,
3957+
participant: message.participant,
3958+
message: message.message,
3959+
});
3960+
}
3961+
}
3962+
3963+
return response;
39053964
} catch (error) {
39063965
this.logger.error(error);
3907-
throw new BadRequestException(error.toString());
3966+
throw error;
39083967
}
39093968
}
39103969

0 commit comments

Comments
 (0)