fix(inbox): deduplica mensagens inbound por whatsapp_message_id - #58
fix(inbox): deduplica mensagens inbound por whatsapp_message_id#58felipetruman wants to merge 1 commit into
Conversation
O Meta reenvia o webhook (retry) com o mesmo whatsapp_message_id. Sem guard, a mensagem era persistida 2x e a IA disparada 2x, corrompendo contadores e o inbox silenciosamente. Adiciona checagem via findMessageByWhatsAppId (que já existia mas não era usada) no topo de handleInboundMessage, antes do RPC e do fallback legacy — cobre ambos os caminhos. Fail-open: se a checagem falhar, processa normalmente (melhor duplicar que perder mensagem).
|
@felipetruman is attempting to deploy a commit to the Thales Laray Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthrough
ChangesInbound Message Deduplication
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/inbox/inbox-webhook.ts (1)
112-112: 💤 Low valueOptional: Consider using English for consistency with other log messages.
The console log message uses Portuguese, while most other logs in this file use English (e.g., lines 145, 154, 162, 194). For developer-facing logs, consistency aids scanability.
♻️ Suggested change for consistency
- console.log(`🔁 [INBOX] Mensagem duplicada ignorada (whatsapp_message_id=${payload.messageId})`) + console.log(`🔁 [INBOX] Duplicate message ignored (whatsapp_message_id=${payload.messageId})`)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/inbox/inbox-webhook.ts` at line 112, The console.log message in the duplicate message check uses Portuguese text ("Mensagem duplicada ignorada") while other log messages in the file use English for consistency. Replace the Portuguese message with its English equivalent while preserving the emoji, tag, and dynamic messageId parameter to maintain consistency with other logs in the file like those found at lines 145, 154, 162, and 194.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/inbox/inbox-webhook.ts`:
- Line 112: The console.log message in the duplicate message check uses
Portuguese text ("Mensagem duplicada ignorada") while other log messages in the
file use English for consistency. Replace the Portuguese message with its
English equivalent while preserving the emoji, tag, and dynamic messageId
parameter to maintain consistency with other logs in the file like those found
at lines 145, 154, 162, and 194.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3b29b53a-81f5-43fd-a3b6-6b6e60458bb4
📒 Files selected for processing (1)
lib/inbox/inbox-webhook.ts
Companion no nível do banco da guard app-level (thaleslaray#58). O índice do baseline era não-unique (só performance), permitindo duplicatas em retries do Meta. Troca por UNIQUE INDEX parcial (WHERE whatsapp_message_id IS NOT NULL), garantindo dedup no DB mesmo se a guard app-level falhar. Inclui nota de deduplicação prévia para bases já em uso (UNIQUE falha se houver duplicatas existentes).
Problema
O Meta reenvia o webhook (retry) com o mesmo
whatsapp_message_id. HojehandleInboundMessageinsere a mensagem direto (via RPCprocess_inbound_messageou fallbackcreateMessage) sem checar duplicata — não há unique constraint eminbox_messages.whatsapp_message_id(índice existe mas é não-unique). Resultado: mensagem persistida 2x e IA disparada 2x, corrompendo contadores e o inbox silenciosamente.Correção
Guard no topo de
handleInboundMessage, antes do RPC e do fallback legacy (cobre ambos): usafindMessageByWhatsAppId(que já existia mas estava sem uso) e retorna cedo se a mensagem já existe.Test plan
inbox_messages, IA dispara 1x.Summary by CodeRabbit