Skip to content

Commit 52a8d9e

Browse files
committed
fix: normalize remoteJid in message updates and handle race condition in contact cache
1 parent f46699e commit 52a8d9e

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,15 @@ export class BaileysStartupService extends ChannelStartupService {
15651565

15661566
for await (const { key, update } of args) {
15671567
const keyAny = key as any;
1568-
const normalizedRemoteJid = keyAny.remoteJid?.replace(/:.*$/, '');
1569-
const normalizedParticipant = keyAny.participant?.replace(/:.*$/, '');
1568+
if (keyAny.remoteJid) {
1569+
keyAny.remoteJid = keyAny.remoteJid.replace(/:.*$/, '');
1570+
}
1571+
if (keyAny.participant) {
1572+
keyAny.participant = keyAny.participant.replace(/:.*$/, '');
1573+
}
1574+
1575+
const normalizedRemoteJid = keyAny.remoteJid;
1576+
const normalizedParticipant = keyAny.participant;
15701577

15711578
if (settings?.groupsIgnore && normalizedRemoteJid?.includes('@g.us')) {
15721579
continue;
@@ -1644,18 +1651,38 @@ export class BaileysStartupService extends ChannelStartupService {
16441651

16451652
const searchId = originalMessageId || key.id;
16461653

1647-
const messages = (await this.prismaRepository.$queryRaw`
1648-
SELECT * FROM "Message"
1649-
WHERE "instanceId" = ${this.instanceId}
1650-
AND "key"->>'id' = ${searchId}
1651-
LIMIT 1
1652-
`) as any[];
1653-
findMessage = messages[0] || null;
1654+
let retries = 0;
1655+
const maxRetries = 3;
1656+
1657+
while (retries < maxRetries) {
1658+
const messages = (await this.prismaRepository.$queryRaw`
1659+
SELECT * FROM "Message"
1660+
WHERE "instanceId" = ${this.instanceId}
1661+
AND "key"->>'id' = ${searchId}
1662+
LIMIT 1
1663+
`) as any[];
1664+
findMessage = messages[0] || null;
1665+
1666+
if (findMessage?.id) {
1667+
break;
1668+
}
1669+
1670+
retries++;
1671+
if (retries < maxRetries) {
1672+
await delay(2000);
1673+
}
1674+
}
16541675

16551676
if (!findMessage?.id) {
16561677
this.logger.warn(`Original message not found for update. Skipping. Key: ${JSON.stringify(key)}`);
16571678
continue;
16581679
}
1680+
if (findMessage?.key?.remoteJid && findMessage.key.remoteJid !== key.remoteJid) {
1681+
this.logger.verbose(
1682+
`Updating key.remoteJid from ${key.remoteJid} to ${findMessage.key.remoteJid} based on stored message`,
1683+
);
1684+
key.remoteJid = findMessage.key.remoteJid;
1685+
}
16591686
message.messageId = findMessage.id;
16601687
}
16611688

src/utils/onWhatsappCache.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,24 @@ export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
164164
logger.verbose(
165165
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
166166
);
167-
await prismaRepository.isOnWhatsapp.create({
168-
data: dataPayload,
169-
});
167+
try {
168+
await prismaRepository.isOnWhatsapp.create({
169+
data: dataPayload,
170+
});
171+
} catch (error: any) {
172+
// Check for unique constraint violation (Prisma error code P2002)
173+
if (error.code === 'P2002' && error.meta?.target?.includes('remoteJid')) {
174+
logger.verbose(
175+
`[saveOnWhatsappCache] Race condition detected for ${remoteJid}, updating existing record instead.`,
176+
);
177+
await prismaRepository.isOnWhatsapp.update({
178+
where: { remoteJid: remoteJid },
179+
data: dataPayload,
180+
});
181+
} else {
182+
throw error;
183+
}
184+
}
170185
}
171186
} catch (e) {
172187
// Loga o erro mas não para a execução dos outros promises

0 commit comments

Comments
 (0)