Skip to content

fix: Erro na criação de conversation quando já existe uma conversation de outro inbox para o mesmo usuário #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

victoreduardo
Copy link

@victoreduardo victoreduardo commented May 3, 2025

Objetivo:

Ao buscar pela conversa do contato, é retornado todas as conversas de todos os inbox (o endpoint não permite filtrar pelo inbox), retornando conversas de outro inbox. O problema ocorre quando há uma conversa para o Usuario1 no InboxA, e um novo canal é adicionado no CW (inbox) onde o mesmo contato (Usuario1) também existe.

Com isso, contactConversations.payload existe e há um registro (porém do inbox errado). Ao filtrar pelo inbox na linha 704, não há conversation, gerando um erro. A conversation que existe no payload é de outro inbox.

Assim, é preciso filtrar o contactConversations.payload antes de realizar a operação, garantindo que, caso não exista, a conversation será criada, como esperado.

Mudanças:

  • Define inboxConversation a partir das conversas do usuário e filtrando por inbox.
  • Move inboxConversation (anteriormente dentro do condicional) para fora, verificando se o inbox correto realmente existe, ao invés de verificar TODOS os inboxes retornados.

Summary by Sourcery

Fix conversation retrieval logic in Chatwoot service to correctly handle conversations across different inboxes for the same contact

Bug Fixes:

  • Correctly filter conversations by specific inbox to prevent errors when multiple conversations exist for the same contact
  • Ensure conversation retrieval works correctly when a contact exists in multiple inboxes

Enhancements:

  • Improved conversation filtering logic to find the correct conversation in the specific inbox
  • Simplified conversation lookup and status handling

Copy link

sourcery-ai bot commented May 3, 2025

Reviewer's Guide

This pull request modifies the conversation retrieval logic to filter conversations by the specific inbox ID before checking for existence and processing. This prevents errors that occurred when a contact had conversations in other inboxes but not the target one.

Sequence Diagram for Updated Conversation Retrieval Logic

sequenceDiagram
    participant Caller
    participant Svc as ChatwootService
    participant API as ChatwootAPIClient
    participant Cache

    Caller->>Svc: Request conversation for contact in specific inbox (filterInbox)
    Svc->>API: Get conversations for contact (all inboxes)
    API-->>Svc: Returns contactConversations.payload
    Svc->>Svc: inboxConversation = Filter payload by filterInbox.id
    alt inboxConversation exists
        Svc->>Svc: Potentially toggle status via API if needed
        Svc->>Cache: Set inboxConversation.id
        Svc-->>Caller: Return existing inboxConversation.id
    else inboxConversation does NOT exist
        Svc->>API: Create new conversation for filterInbox
        API-->>Svc: New conversation details
        Svc->>Cache: Set new conversation ID
        Svc-->>Caller: Return new conversation ID
    end
Loading

File-Level Changes

Change Details Files
Pre-filter conversations by the target inbox ID.
  • Filter contactConversations.payload by filterInbox.id to define inboxConversation upfront.
  • Change the main conditional from checking contactConversations.payload.length to checking if inboxConversation exists.
  • Refactor internal logic to use the pre-filtered inboxConversation directly, removing redundant finds.
  • Adjust logging messages to reflect the use of inboxConversation.
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Possibly linked issues

  • #0: PR fixes the TypeError in createConversation by correctly filtering user conversations by inbox.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

neocol83 added a commit to neocol83/evolution-api that referenced this pull request May 3, 2025
@DavidsonGomes DavidsonGomes merged commit bff3bf5 into EvolutionAPI:develop May 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants