Skip to content

Commit fb1fa4d

Browse files
committed
feat: add participantsData field maintaining backward
compatibility - Keep original participants array (string[]) for backward compatibility - Add new participantsData field with resolved phone numbers and metadata - Consumers can migrate gradually from participants to participantsData - No breaking changes to existing webhook integrations Payload structure: - participants: string[] (original JID strings) - participantsData: object[] (enhanced with phoneNumber, name, imgUrl)
1 parent 57ea670 commit fb1fa4d

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,30 +1596,57 @@ export class BaileysStartupService extends ChannelStartupService {
15961596
participants: string[];
15971597
action: ParticipantAction;
15981598
}) => {
1599+
// ENHANCEMENT: Adds participantsData field while maintaining backward compatibility
1600+
// MAINTAINS: participants: string[] (original JID strings)
1601+
// ADDS: participantsData: { jid: string, phoneNumber: string, name?: string, imgUrl?: string }[]
1602+
// This enables LID to phoneNumber conversion without breaking existing webhook consumers
1603+
1604+
// Helper to normalize participantId as phone number
1605+
const normalizePhoneNumber = (id: string): string => {
1606+
// Remove @lid, @s.whatsapp.net suffixes and extract just the number part
1607+
return id.split('@')[0];
1608+
};
1609+
15991610
try {
16001611
// Usa o mesmo método que o endpoint /group/participants
16011612
const groupParticipants = await this.findParticipants({ groupJid: participantsUpdate.id });
16021613

1614+
// Validação para garantir que temos dados válidos
1615+
if (!groupParticipants?.participants || !Array.isArray(groupParticipants.participants)) {
1616+
throw new Error('Invalid participant data received from findParticipants');
1617+
}
1618+
16031619
// Filtra apenas os participantes que estão no evento
16041620
const resolvedParticipants = participantsUpdate.participants.map((participantId) => {
16051621
const participantData = groupParticipants.participants.find(p => p.id === participantId);
16061622

1623+
let phoneNumber: string;
1624+
if (participantData?.phoneNumber) {
1625+
phoneNumber = participantData.phoneNumber;
1626+
} else {
1627+
phoneNumber = normalizePhoneNumber(participantId);
1628+
}
1629+
16071630
return {
16081631
jid: participantId,
1609-
phoneNumber: participantData?.phoneNumber || participantId,
1632+
phoneNumber,
16101633
name: participantData?.name,
16111634
imgUrl: participantData?.imgUrl,
16121635
};
16131636
});
16141637

1638+
// Mantém formato original + adiciona dados resolvidos
16151639
const enhancedParticipantsUpdate = {
16161640
...participantsUpdate,
1617-
participants: resolvedParticipants
1641+
participants: participantsUpdate.participants, // Mantém array original de strings
1642+
participantsData: resolvedParticipants // Adiciona dados resolvidos em campo separado
16181643
};
16191644

16201645
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, enhancedParticipantsUpdate);
16211646
} catch (error) {
1622-
console.log('Erro ao buscar dados dos participantes para webhook:', error);
1647+
this.logger.error(
1648+
`Failed to resolve participant data for GROUP_PARTICIPANTS_UPDATE webhook: ${error.message} | Group: ${participantsUpdate.id} | Participants: ${participantsUpdate.participants.length}`
1649+
);
16231650
// Fallback - envia sem conversão
16241651
this.sendDataWebhook(Events.GROUP_PARTICIPANTS_UPDATE, participantsUpdate);
16251652
}

0 commit comments

Comments
 (0)