Skip to content

Commit 0976109

Browse files
Merge pull request #2025 from guispiller/main
feat: convert LID to phoneNumber on GROUP_PARTICIPANTS_UPDATE webhook
2 parents b808dda + bedfb01 commit 0976109

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

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

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ export class BaileysStartupService extends ChannelStartupService {
504504
try {
505505
// Use raw SQL to avoid JSON path issues
506506
const webMessageInfo = (await this.prismaRepository.$queryRaw`
507-
SELECT * FROM "Message"
508-
WHERE "instanceId" = ${this.instanceId}
507+
SELECT * FROM "Message"
508+
WHERE "instanceId" = ${this.instanceId}
509509
AND "key"->>'id' = ${key.id}
510510
`) as proto.IWebMessageInfo[];
511511

@@ -1505,8 +1505,8 @@ export class BaileysStartupService extends ChannelStartupService {
15051505
if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
15061506
// Use raw SQL to avoid JSON path issues
15071507
const messages = (await this.prismaRepository.$queryRaw`
1508-
SELECT * FROM "Message"
1509-
WHERE "instanceId" = ${this.instanceId}
1508+
SELECT * FROM "Message"
1509+
WHERE "instanceId" = ${this.instanceId}
15101510
AND "key"->>'id' = ${key.id}
15111511
LIMIT 1
15121512
`) as any[];
@@ -1623,12 +1623,66 @@ export class BaileysStartupService extends ChannelStartupService {
16231623
});
16241624
},
16251625

1626-
'group-participants.update': (participantsUpdate: {
1626+
'group-participants.update': async (participantsUpdate: {
16271627
id: string;
16281628
participants: string[];
16291629
action: ParticipantAction;
16301630
}) => {
1631-
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
1631+
// ENHANCEMENT: Adds participantsData field while maintaining backward compatibility
1632+
// MAINTAINS: participants: string[] (original JID strings)
1633+
// ADDS: participantsData: { jid: string, phoneNumber: string, name?: string, imgUrl?: string }[]
1634+
// This enables LID to phoneNumber conversion without breaking existing webhook consumers
1635+
1636+
// Helper to normalize participantId as phone number
1637+
const normalizePhoneNumber = (id: string): string => {
1638+
// Remove @lid, @s.whatsapp.net suffixes and extract just the number part
1639+
return id.split('@')[0];
1640+
};
1641+
1642+
try {
1643+
// Usa o mesmo método que o endpoint /group/participants
1644+
const groupParticipants = await this.findParticipants({ groupJid: participantsUpdate.id });
1645+
1646+
// Validação para garantir que temos dados válidos
1647+
if (!groupParticipants?.participants || !Array.isArray(groupParticipants.participants)) {
1648+
throw new Error('Invalid participant data received from findParticipants');
1649+
}
1650+
1651+
// Filtra apenas os participantes que estão no evento
1652+
const resolvedParticipants = participantsUpdate.participants.map((participantId) => {
1653+
const participantData = groupParticipants.participants.find((p) => p.id === participantId);
1654+
1655+
let phoneNumber: string;
1656+
if (participantData?.phoneNumber) {
1657+
phoneNumber = participantData.phoneNumber;
1658+
} else {
1659+
phoneNumber = normalizePhoneNumber(participantId);
1660+
}
1661+
1662+
return {
1663+
jid: participantId,
1664+
phoneNumber,
1665+
name: participantData?.name,
1666+
imgUrl: participantData?.imgUrl,
1667+
};
1668+
});
1669+
1670+
// Mantém formato original + adiciona dados resolvidos
1671+
const enhancedParticipantsUpdate = {
1672+
...participantsUpdate,
1673+
participants: participantsUpdate.participants, // Mantém array original de strings
1674+
// Adiciona dados resolvidos em campo separado
1675+
participantsData: resolvedParticipants,
1676+
};
1677+
1678+
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, enhancedParticipantsUpdate);
1679+
} catch (error) {
1680+
this.logger.error(
1681+
`Failed to resolve participant data for GROUP_PARTICIPANTS_UPDATE webhook: ${error.message} | Group: ${participantsUpdate.id} | Participants: ${participantsUpdate.participants.length}`,
1682+
);
1683+
// Fallback - envia sem conversão
1684+
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
1685+
}
16321686

16331687
this.updateGroupMetadataCache(participantsUpdate.id);
16341688
},
@@ -4493,7 +4547,7 @@ export class BaileysStartupService extends ChannelStartupService {
44934547

44944548
// Use raw SQL to avoid JSON path issues
44954549
const result = await this.prismaRepository.$executeRaw`
4496-
UPDATE "Message"
4550+
UPDATE "Message"
44974551
SET "status" = ${status[4]}
44984552
WHERE "instanceId" = ${this.instanceId}
44994553
AND "key"->>'remoteJid' = ${remoteJid}
@@ -4518,7 +4572,7 @@ export class BaileysStartupService extends ChannelStartupService {
45184572
this.prismaRepository.chat.findFirst({ where: { remoteJid } }),
45194573
// Use raw SQL to avoid JSON path issues
45204574
this.prismaRepository.$queryRaw`
4521-
SELECT COUNT(*)::int as count FROM "Message"
4575+
SELECT COUNT(*)::int as count FROM "Message"
45224576
WHERE "instanceId" = ${this.instanceId}
45234577
AND "key"->>'remoteJid' = ${remoteJid}
45244578
AND ("key"->>'fromMe')::boolean = false

0 commit comments

Comments
 (0)