From 6c274f71ae444ab241f35017352ad4d07676ed2f Mon Sep 17 00:00:00 2001 From: ValdecirMysian Date: Wed, 28 Jan 2026 12:18:27 -0300 Subject: [PATCH] Add caching for processed order IDs to prevent duplicates Implement cache for deduplication of order messages to avoid processing duplicates. --- .../chatwoot/services/chatwoot.service.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index 56094558..ff6d28f3 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -49,6 +49,10 @@ export class ChatwootService { private provider: any; + // Cache para deduplicação de orderMessage (evita mensagens duplicadas) + private processedOrderIds: Map = new Map(); + private readonly ORDER_CACHE_TTL_MS = 30000; // 30 segundos + constructor( private readonly waMonitor: WAMonitoringService, private readonly configService: ConfigService, @@ -1795,6 +1799,20 @@ export class ChatwootService { } // Tratamento de Pedidos do Catálogo (WhatsApp Business Catalog) + if (typeKey === 'orderMessage' && result.orderId) { + const now = Date.now(); + // Limpa entradas antigas do cache + this.processedOrderIds.forEach((timestamp, id) => { + if (now - timestamp > this.ORDER_CACHE_TTL_MS) { + this.processedOrderIds.delete(id); + } + }); + // Verifica se já processou este orderId + if (this.processedOrderIds.has(result.orderId)) { + return undefined; // Ignora duplicado + } + this.processedOrderIds.set(result.orderId, now); + } if (typeKey === 'orderMessage') { // Extrai o valor - pode ser Long, objeto {low, high}, ou número direto let rawPrice = 0;