Skip to content

Corrigindo um bug no endpoint de findChats e permitindo paginação nos endpoints de findChats e findContacts #1334

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

pedro-php
Copy link
Contributor

@pedro-php pedro-php commented Mar 25, 2025

Fix

Problema

Ao enviar uma mensagem via dispositivo, dois comportamentos distintos ocorriam:

  1. Inicialmente, o evento MESSAGES_UPSERT atualizava o campo name da tabela Chat com o pushname do remetente.
  2. Em seguida, o evento MESSAGES_UPDATE sobrescrevia o campo name com uma string vazia de forma consistente, pois tentava fazer um update utilizando uma variável que sequer era definida — já que esse campo não era enviado pelo respectivo evento da Baileys.

O que foi alterado?

  • O campo name da tabela Chat não é mais atualizado durante o evento MESSAGES_UPDATE.
  • No evento MESSAGES_UPSERT, o campo name só é atualizado quando fromMe for falso, ou seja, apenas quando a mensagem for recebida (não enviada pelo próprio usuário).

Resolução

Agora, ao chamar um endpoint que retorna o campo name da tabela Chat (como o /chat/findChats/), o valor retornado será o correto — evitando strings vazias ou o nome do próprio remetente. O campo continua sendo atualizado normalmente ao receber mensagens.

Copy link
Contributor

sourcery-ai bot commented Mar 25, 2025

Reviewer's Guide by Sourcery

This pull request fixes a bug in the findChats endpoint and implements pagination for the findChats and findContacts endpoints. The bug fix ensures that the correct chat name is returned, while the pagination feature improves performance for users with many contacts and chats.

Sequence diagram for fetching chat name

sequenceDiagram
  participant Client
  participant ChannelStartupService
  participant PrismaRepository

  Client->>ChannelStartupService: Calls findChats endpoint
  ChannelStartupService->>PrismaRepository: Executes raw SQL query to fetch chats
  PrismaRepository-->>ChannelStartupService: Returns chat data including chatName from Chat table
  ChannelStartupService-->>Client: Returns chat data with correct chatName
Loading

Sequence diagram for Contact Pagination

sequenceDiagram
    participant Client
    participant ChannelStartupService
    participant PrismaRepository

    Client->>ChannelStartupService: Calls findContacts endpoint with page and offset
    ChannelStartupService->>PrismaRepository: Calls contact.findMany with take and skip parameters
    PrismaRepository-->>ChannelStartupService: Returns paginated contact data
    ChannelStartupService-->>Client: Returns paginated contact data
Loading

Updated class diagram for ChannelStartupService

classDiagram
    class ChannelStartupService {
        +findContacts(query: any): Promise<Contact[]>
        +findChats(query: any): Promise<Chat[]>
    }

    class Contact {
        id: string
        remoteJid: string
        pushName: string
        chatName: string
        profilePicUrl: string
        updatedAt: Date
        windowStart: Date
    }

    note for Contact "Added chatName attribute"

    class Chat {
        name: string
        createdAt: Date
    }

    ChannelStartupService -- Contact : returns
Loading

File-Level Changes

Change Details Files
Fixed a bug in the findChats endpoint that was returning the push name of the last user who sent a message in the chat instead of the chat name itself.
  • The chat name is no longer overwritten with the sender's push name in messages from groups.
  • Added a chatName parameter to the endpoint's return, which returns the chat name from the Chats table.
src/api/services/channel.service.ts
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Implemented pagination for the findChats and findContacts endpoints.
  • Added the ability to paginate the findChats and findContacts endpoints using the page and offset parameters.
  • In the findChats endpoint, pagination was added directly to the raw query.
  • In the findContacts endpoint, pagination was added to the take and skip parameters of Prisma.
src/api/services/channel.service.ts

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pedro-php - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding validation to ensure that page and offset are positive integers.
  • It might be worth extracting the pagination logic into a separate helper function to improve readability.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@MateusHirataAW
Copy link

up

1 similar comment
@marcosgalupo
Copy link

up

@DavidsonGomes DavidsonGomes changed the base branch from main to develop March 26, 2025 12:59
@DavidsonGomes DavidsonGomes merged commit b89f114 into EvolutionAPI:develop Mar 26, 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.

4 participants