diff --git a/src/apps/chatwoot/client/ContactConversationService.ts b/src/apps/chatwoot/client/ContactConversationService.ts index d8cd5502..901315f7 100644 --- a/src/apps/chatwoot/client/ContactConversationService.ts +++ b/src/apps/chatwoot/client/ContactConversationService.ts @@ -10,6 +10,7 @@ import { import { Conversation } from '@waha/apps/chatwoot/client/Conversation'; import { ContactIds, + ConversationResult, ConversationService, } from '@waha/apps/chatwoot/client/ConversationService'; import { ChatWootAPIConfig } from '@waha/apps/chatwoot/client/interfaces'; @@ -43,9 +44,10 @@ export class ContactConversationService { this.cache = CacheForConfig(config); } - public async upsertByContactInfo( + private async getByContactInfo( contactInfo: ContactInfo, - ): Promise { + upsert: boolean, + ): Promise { const chatId = contactInfo.ChatId(); // Check cache for chat id @@ -77,10 +79,21 @@ export class ContactConversationService { // // Get or create a conversation for this inbox // - const conversation = await this.conversationService.upsert({ + const contactIds = { id: cwContact.data.id, sourceId: cwContact.sourceId, - }); + }; + let conversation: ConversationResult; + if (upsert) { + conversation = await this.conversationService.upsert(contactIds); + } else { + conversation = await this.conversationService.find(contactIds); + if (!conversation) { + // Nothing found, do not save it in cache + return null; + } + } + this.logger.debug( `Using conversation for chat.id: ${chatId}, conversation.id: ${conversation.id}, contact.id: ${cwContact.sourceId}`, ); @@ -94,11 +107,15 @@ export class ContactConversationService { return ids; } - public async ConversationByContact( + private async conversationByContact( contactInfo: ContactInfo, - ): Promise { + upsert: boolean, + ): Promise { const chatId = contactInfo.ChatId(); - const ids = await this.upsertByContactInfo(contactInfo); + const ids = await this.getByContactInfo(contactInfo, upsert); + if (!ids) { + return null; + } const conversation = new Conversation( this.accountAPI, this.config.accountId, @@ -118,6 +135,26 @@ export class ContactConversationService { return conversation; } + /** + * Find or create if not exists conversation for the contact + */ + public async ConversationByContact(contactInfo: ContactInfo) { + const conversation = await this.conversationByContact(contactInfo, true); + if (!conversation) { + // Shouldn't happen, but for type safety + throw new Error('Conversation could not be created or found'); + } + return conversation; + } + + /** + * Find suitable conversation for the contact (based on filters) + * If nothing found - it doesn't create a new one + */ + public async FindConversationByContact(contactInfo: ContactInfo) { + return this.conversationByContact(contactInfo, false); + } + public ConversationById(conversationId: number): Conversation { return new Conversation( this.accountAPI, diff --git a/src/apps/chatwoot/client/ConversationService.ts b/src/apps/chatwoot/client/ConversationService.ts index 5d2b317b..fe1ba823 100644 --- a/src/apps/chatwoot/client/ConversationService.ts +++ b/src/apps/chatwoot/client/ConversationService.ts @@ -24,14 +24,20 @@ export class ConversationService { private logger: ILogger, ) {} - private async find(contact: ContactIds): Promise { + public async find(contact: ContactIds): Promise { const result: { payload: contact_conversations } = (await this.accountAPI.contacts.listConversations({ accountId: this.config.accountId, id: contact.id, })) as any; const conversations = result.payload; - return this.selector.select(conversations); + const conversation = this.selector.select(conversations); + if (conversation) { + this.logger.debug( + `Found existing conversation.id: ${conversation.id} for contact.id: ${contact.id}, contact.sourceId: ${contact.sourceId}`, + ); + } + return conversation; } private async create(contact: ContactIds): Promise { @@ -50,9 +56,6 @@ export class ConversationService { if (!conversation) { conversation = await this.create(contact); } - this.logger.debug( - `Using conversation.id: ${conversation.id} for contact.id: ${contact.id}, contact.sourceId: ${contact.sourceId}`, - ); return conversation; } diff --git a/src/apps/chatwoot/consumers/waha/message.ack.ts b/src/apps/chatwoot/consumers/waha/message.ack.ts index fd9e96bb..a8fe1636 100644 --- a/src/apps/chatwoot/consumers/waha/message.ack.ts +++ b/src/apps/chatwoot/consumers/waha/message.ack.ts @@ -94,7 +94,16 @@ class MessageAckHandler { this.locale, ); const conversation = - await this.contactConversationService.ConversationByContact(contactInfo); + await this.contactConversationService.FindConversationByContact( + contactInfo, + ); + + if (!conversation) { + this.logger.debug( + `No suitable conversation found to mark as read for chat.id: ${payload.from}`, + ); + return; + } this.info.onConversationId(conversation.conversationId); const sourceId = conversation.sourceId;