Skip to content

Commit 6c274f7

Browse files
Add caching for processed order IDs to prevent duplicates
Implement cache for deduplication of order messages to avoid processing duplicates.
1 parent 2ff572d commit 6c274f7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export class ChatwootService {
4949

5050
private provider: any;
5151

52+
// Cache para deduplicação de orderMessage (evita mensagens duplicadas)
53+
private processedOrderIds: Map<string, number> = new Map();
54+
private readonly ORDER_CACHE_TTL_MS = 30000; // 30 segundos
55+
5256
constructor(
5357
private readonly waMonitor: WAMonitoringService,
5458
private readonly configService: ConfigService,
@@ -1795,6 +1799,20 @@ export class ChatwootService {
17951799
}
17961800

17971801
// Tratamento de Pedidos do Catálogo (WhatsApp Business Catalog)
1802+
if (typeKey === 'orderMessage' && result.orderId) {
1803+
const now = Date.now();
1804+
// Limpa entradas antigas do cache
1805+
this.processedOrderIds.forEach((timestamp, id) => {
1806+
if (now - timestamp > this.ORDER_CACHE_TTL_MS) {
1807+
this.processedOrderIds.delete(id);
1808+
}
1809+
});
1810+
// Verifica se já processou este orderId
1811+
if (this.processedOrderIds.has(result.orderId)) {
1812+
return undefined; // Ignora duplicado
1813+
}
1814+
this.processedOrderIds.set(result.orderId, now);
1815+
}
17981816
if (typeKey === 'orderMessage') {
17991817
// Extrai o valor - pode ser Long, objeto {low, high}, ou número direto
18001818
let rawPrice = 0;

0 commit comments

Comments
 (0)