Skip to content

Commit c132379

Browse files
committed
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.
1 parent f786263 commit c132379

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

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

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,7 @@ export class ChatwootService {
606606
this.logger.verbose(`--- Start createConversation ---`);
607607
this.logger.verbose(`Instance: ${JSON.stringify(instance)}`);
608608

609-
// If it already exists in the cache, return conversationId
610-
if (await this.cache.has(cacheKey)) {
611-
const conversationId = (await this.cache.get(cacheKey)) as number;
612-
this.logger.verbose(`Found conversation to: ${remoteJid}, conversation ID: ${conversationId}`);
613-
return conversationId;
614-
}
609+
// Always check Chatwoot first, cache only as fallback
615610

616611
// If lock already exists, wait until release or timeout
617612
if (await this.cache.has(lockKey)) {
@@ -623,11 +618,7 @@ export class ChatwootService {
623618
break;
624619
}
625620
await new Promise((res) => setTimeout(res, 300));
626-
if (await this.cache.has(cacheKey)) {
627-
const conversationId = (await this.cache.get(cacheKey)) as number;
628-
this.logger.verbose(`Resolves creation of: ${remoteJid}, conversation ID: ${conversationId}`);
629-
return conversationId;
630-
}
621+
// Removed cache check here to ensure we always check Chatwoot
631622
}
632623
}
633624

@@ -637,12 +628,9 @@ export class ChatwootService {
637628

638629
try {
639630
/*
640-
Double check after lock
641-
Utilizei uma nova verificação para evitar que outra thread execute entre o terminio do while e o set lock
631+
Double check after lock - REMOVED
632+
This was causing the system to use cached conversations instead of checking Chatwoot
642633
*/
643-
if (await this.cache.has(cacheKey)) {
644-
return (await this.cache.get(cacheKey)) as number;
645-
}
646634

647635
const client = await this.clientCw(instance);
648636
if (!client) return null;
@@ -749,14 +737,25 @@ export class ChatwootService {
749737
return null;
750738
}
751739

752-
let inboxConversation = this.findOpenConversation(contactConversations.payload, filterInbox.id);
740+
let inboxConversation = null;
753741

754-
if (!inboxConversation && this.provider.reopenConversation) {
755-
inboxConversation = await this.findAndReopenResolvedConversation(
756-
client,
757-
contactConversations.payload,
758-
filterInbox.id,
759-
);
742+
if (this.provider.reopenConversation) {
743+
inboxConversation = this.findOpenConversation(contactConversations.payload, filterInbox.id);
744+
745+
if (inboxConversation) {
746+
this.logger.verbose(
747+
`Found open conversation in reopenConversation mode: ${JSON.stringify(inboxConversation)}`,
748+
);
749+
} else {
750+
inboxConversation = await this.findAndReopenResolvedConversation(
751+
client,
752+
contactConversations.payload,
753+
filterInbox.id,
754+
);
755+
}
756+
} else {
757+
inboxConversation = this.findOpenConversation(contactConversations.payload, filterInbox.id);
758+
this.logger.verbose(`Found conversation: ${JSON.stringify(inboxConversation)}`);
760759
}
761760

762761
if (inboxConversation) {
@@ -765,6 +764,14 @@ export class ChatwootService {
765764
return inboxConversation.id;
766765
}
767766

767+
if (await this.cache.has(cacheKey)) {
768+
const conversationId = (await this.cache.get(cacheKey)) as number;
769+
this.logger.warn(
770+
`No active conversations found in Chatwoot, using cached conversation ID: ${conversationId} as fallback`,
771+
);
772+
return conversationId;
773+
}
774+
768775
const data = {
769776
contact_id: contactId.toString(),
770777
inbox_id: filterInbox.id.toString(),

0 commit comments

Comments
 (0)