fix(chatwoot): ajustar lógica de verificação de conversas e cache

Este commit modifica a lógica de verificação de conversas no serviço Chatwoot, garantindo que a busca por conversas ativas seja priorizada em relação ao uso de cache. A verificação de cache foi removida em pontos críticos para evitar que conversas desatualizadas sejam utilizadas, melhorando a precisão na recuperação de dados. Além disso, a lógica de reabertura de conversas foi refinada para garantir que as interações sejam tratadas corretamente, mantendo a experiência do usuário mais fluida.
This commit is contained in:
Vitordotpy 2025-09-29 15:26:24 -03:00
parent f7862637b1
commit c132379b3a

View File

@ -606,12 +606,7 @@ export class ChatwootService {
this.logger.verbose(`--- Start createConversation ---`); this.logger.verbose(`--- Start createConversation ---`);
this.logger.verbose(`Instance: ${JSON.stringify(instance)}`); this.logger.verbose(`Instance: ${JSON.stringify(instance)}`);
// If it already exists in the cache, return conversationId // Always check Chatwoot first, cache only as fallback
if (await this.cache.has(cacheKey)) {
const conversationId = (await this.cache.get(cacheKey)) as number;
this.logger.verbose(`Found conversation to: ${remoteJid}, conversation ID: ${conversationId}`);
return conversationId;
}
// If lock already exists, wait until release or timeout // If lock already exists, wait until release or timeout
if (await this.cache.has(lockKey)) { if (await this.cache.has(lockKey)) {
@ -623,11 +618,7 @@ export class ChatwootService {
break; break;
} }
await new Promise((res) => setTimeout(res, 300)); await new Promise((res) => setTimeout(res, 300));
if (await this.cache.has(cacheKey)) { // Removed cache check here to ensure we always check Chatwoot
const conversationId = (await this.cache.get(cacheKey)) as number;
this.logger.verbose(`Resolves creation of: ${remoteJid}, conversation ID: ${conversationId}`);
return conversationId;
}
} }
} }
@ -637,12 +628,9 @@ export class ChatwootService {
try { try {
/* /*
Double check after lock Double check after lock - REMOVED
Utilizei uma nova verificação para evitar que outra thread execute entre o terminio do while e o set lock This was causing the system to use cached conversations instead of checking Chatwoot
*/ */
if (await this.cache.has(cacheKey)) {
return (await this.cache.get(cacheKey)) as number;
}
const client = await this.clientCw(instance); const client = await this.clientCw(instance);
if (!client) return null; if (!client) return null;
@ -749,14 +737,25 @@ export class ChatwootService {
return null; return null;
} }
let inboxConversation = this.findOpenConversation(contactConversations.payload, filterInbox.id); let inboxConversation = null;
if (!inboxConversation && this.provider.reopenConversation) { if (this.provider.reopenConversation) {
inboxConversation = await this.findAndReopenResolvedConversation( inboxConversation = this.findOpenConversation(contactConversations.payload, filterInbox.id);
client,
contactConversations.payload, if (inboxConversation) {
filterInbox.id, this.logger.verbose(
); `Found open conversation in reopenConversation mode: ${JSON.stringify(inboxConversation)}`,
);
} else {
inboxConversation = await this.findAndReopenResolvedConversation(
client,
contactConversations.payload,
filterInbox.id,
);
}
} else {
inboxConversation = this.findOpenConversation(contactConversations.payload, filterInbox.id);
this.logger.verbose(`Found conversation: ${JSON.stringify(inboxConversation)}`);
} }
if (inboxConversation) { if (inboxConversation) {
@ -765,6 +764,14 @@ export class ChatwootService {
return inboxConversation.id; return inboxConversation.id;
} }
if (await this.cache.has(cacheKey)) {
const conversationId = (await this.cache.get(cacheKey)) as number;
this.logger.warn(
`No active conversations found in Chatwoot, using cached conversation ID: ${conversationId} as fallback`,
);
return conversationId;
}
const data = { const data = {
contact_id: contactId.toString(), contact_id: contactId.toString(),
inbox_id: filterInbox.id.toString(), inbox_id: filterInbox.id.toString(),