Skip to content

Conversation

@jpavrdev
Copy link

@jpavrdev jpavrdev commented Feb 3, 2026

📋 Description

Correção de um problema onde o QR Code não estava aparecendo corretamente.
Foi feito um ajuste no arquivo whatsapp.bailey.service.ts para garantir a exibição correta do código.

🔗 Related Issue

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧹 Code cleanup
  • 🔒 Security fix

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced

📸 Screenshots (if applicable)

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly

Summary by Sourcery

Bug Fixes:

  • Prevent infinite reconnection loop on initial WhatsApp connection close so the QR code can be generated and shown correctly.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 3, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts WhatsApp Baileys disconnect handling to avoid an infinite reconnect loop during the initial connection phase so the QR code can be displayed correctly.

Sequence diagram for updated WhatsApp Baileys disconnect and reconnect flow

sequenceDiagram
  actor User
  participant WebApp
  participant BaileysStartupService
  participant WhatsAppServer

  User->>WebApp: Open_whatsapp_connection_screen
  WebApp->>BaileysStartupService: connectToWhatsapp(phoneNumber)
  BaileysStartupService->>WhatsAppServer: initiate_connection
  WhatsAppServer-->>BaileysStartupService: connection_update(qr_generated)
  BaileysStartupService-->>WebApp: emit_qrcode(qrcode)
  WebApp-->>User: display_qrcode

  rect rgb(230,230,250)
    WhatsAppServer-->>BaileysStartupService: connection_closed(lastDisconnect)
    BaileysStartupService->>BaileysStartupService: determine_statusCode
    BaileysStartupService->>BaileysStartupService: isInitialConnection = !instance.wuid && instance.qrcode.count == 0

    alt initial_connection_and_qr_not_used_yet
      BaileysStartupService->>BaileysStartupService: log Initial_connection_closed_wait_for_qr
      BaileysStartupService-->>WhatsAppServer: no_reconnect
    else subsequent_disconnect_and_shouldReconnect
      BaileysStartupService->>BaileysStartupService: shouldReconnect = !codesToNotReconnect.includes(statusCode)
      BaileysStartupService->>WhatsAppServer: reconnect_if_shouldReconnect
    else disconnect_with_forbidden_or_logged_out
      BaileysStartupService->>BaileysStartupService: stop_reconnect
    end
  end
Loading

File-Level Changes

Change Details Files
Guard the disconnect handler to avoid reconnecting on the very first connection attempt before a QR code is generated.
  • Compute an isInitialConnection flag based on the absence of wuid and zero QR code generation count.
  • Short‑circuit the disconnect handler when isInitialConnection is true, logging a message and skipping reconnection.
  • Preserve existing behavior for shouldReconnect logic and subsequent reconnection handling when not in the initial connection state.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Possibly linked issues

  • #: PR adjusts Baileys reconnection/initial connection logic to ensure the QR Code appears, addressing the reported issue

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

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 - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:432` </location>
<code_context>
       const shouldReconnect = !codesToNotReconnect.includes(statusCode);
+
+      // Check if this is the initial connection before QR code generation
+      const isInitialConnection = !this.instance.wuid && this.instance.qrcode.count === 0;
+
+      if (isInitialConnection) {
</code_context>

<issue_to_address>
**issue (bug_risk):** Guard against `this.instance.qrcode` being undefined before accessing `.count`.

If `qrcode` isn’t initialized when disconnect runs, this will throw at runtime. Consider a defensive access pattern (e.g. `this.instance.qrcode?.count ?? 0`) so this remains safe during unusual initialization or disconnect sequences.
</issue_to_address>

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.

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