Skip to content

Conversation

@gabrielmouallem
Copy link

@gabrielmouallem gabrielmouallem commented Nov 24, 2025

📋 Description

The DATABASE_SAVE_DATA_CONTACTS environment variable was being ignored in specific parts of the contacts.upsert (profile picture updates) and contacts.update handlers.

This PR adds the missing conditional checks to ensure that contact data is only saved to the database when this configuration is explicitly enabled, maintaining consistency with other SAVE_DATA flags.

🔗 Related Issue

Closes #(issue_number)

🧪 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
  • Tested with different connection types (if applicable)

📸 Screenshots (if applicable)

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios
  • Any dependent changes have been merged and published

📝 Additional Notes

Summary by Sourcery

Respect database configuration flags when processing WhatsApp contact updates to avoid persisting contacts when disabled.

Bug Fixes:

  • Guard WhatsApp contact update and upsert operations behind the DATABASE.SAVE_DATA.CONTACTS flag so contacts are not written when contact persistence is disabled.

Enhancements:

  • Minor formatting cleanups in logging and message repository initialization for WhatsApp integration.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Nov 24, 2025

Reviewer's Guide

Conditionally persist WhatsApp contact data based on DATABASE.SAVE_DATA.CONTACTS while leaving webhook and Chatwoot integrations intact, and make minor formatting adjustments.

Sequence diagram for conditional WhatsApp contact persistence based on DATABASE.SAVE_DATA.CONTACTS

sequenceDiagram
  actor "WhatsApp" as WhatsApp
  participant "BaileysStartupService" as Baileys
  participant "ConfigService" as Config
  participant "PrismaRepository (Database)" as Prisma
  participant "Webhook consumer" as Webhook
  participant "Chatwoot integration" as Chatwoot

  "WhatsApp" ->> "BaileysStartupService": "Contacts update event with contacts list"
  activate "BaileysStartupService"

  "BaileysStartupService" ->> "Webhook consumer": "sendDataWebhook(\"CONTACTS_UPDATE\", contacts)"

  loop "For each updated contact"
    "BaileysStartupService" ->> "ConfigService": "get(\"DATABASE\").SAVE_DATA.CONTACTS"
    "ConfigService" -->> "BaileysStartupService": "Boolean flag"

    alt "SAVE_DATA.CONTACTS is true"
      "BaileysStartupService" ->> "PrismaRepository (Database)": "contact.updateMany({ where: { remoteJid, instanceId }, data: { profilePicUrl } })"
    else "SAVE_DATA.CONTACTS is false"
      "BaileysStartupService" --x "PrismaRepository (Database)": "Skip contact.updateMany"
    end

    "BaileysStartupService" ->> "ConfigService": "get(\"CHATWOOT\").ENABLED"
    "ConfigService" -->> "BaileysStartupService": "Boolean flag"

    alt "CHATWOOT is enabled and localChatwoot.enabled"
      "BaileysStartupService" ->> "Chatwoot integration": "Sync contact data (e.g., profile picture)"
    else "CHATWOOT disabled"
      "BaileysStartupService" --x "Chatwoot integration": "Skip Chatwoot sync"
    end
  end

  deactivate "BaileysStartupService"
Loading

File-Level Changes

Change Details Files
Gate contact profile update operations on the DATABASE.SAVE_DATA.CONTACTS flag.
  • Wrap the Prisma contact.updateMany calls inside a conditional that checks DATABASE.SAVE_DATA.CONTACTS before executing
  • Preserve webhook emission and Chatwoot integration logic regardless of the database persistence flag
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Conditionally upsert contacts into the database during bulk contact sync based on configuration.
  • Surround the Prisma contact.upsert transaction with a DATABASE.SAVE_DATA.CONTACTS check so contacts are only persisted when enabled
  • Keep upstream webhook dispatch for CONTACTS_UPDATE events independent from database writes
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Apply minor code formatting adjustments for logging and message repository initialization.
  • Adjust indentation in qrcodeTerminal logging for readability
  • Re-indent initialization of the messagesRepository Set for consistent formatting
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Possibly linked issues

  • #(not specified): PR adds conditional checks so contacts are only persisted when DATABASE_SAVE_DATA_CONTACTS is enabled, fixing reported bug.

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 there - I've reviewed your changes - here's some feedback:

  • In the updatedContacts.map(...) block, the Prisma updateMany call is assigned to a variable but never awaited or returned from the async callback, so the database updates will not actually be executed; consider returning the update promise (or collecting them separately) so Promise.all awaits them.
  • You repeatedly call this.configService.get(...) inside per-contact loops; consider caching DATABASE.SAVE_DATA.CONTACTS (and similar flags) in a local constant before the loop to avoid redundant lookups on each iteration.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the updatedContacts.map(...) block, the Prisma updateMany call is assigned to a variable but never awaited or returned from the async callback, so the database updates will not actually be executed; consider returning the update promise (or collecting them separately) so Promise.all awaits them.
- You repeatedly call this.configService.get(...) inside per-contact loops; consider caching DATABASE.SAVE_DATA.CONTACTS (and similar flags) in a local constant before the loop to avoid redundant lookups on each iteration.

## Individual Comments

### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:842-845` </location>
<code_context>
-                where: { remoteJid: contact.remoteJid, instanceId: this.instanceId },
-                data: { profilePicUrl: contact.profilePicUrl },
-              });
+              let update;
+              if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS) {
+                update = this.prismaRepository.contact.updateMany({
+                  where: { remoteJid: contact.remoteJid, instanceId: this.instanceId },
+                  data: { profilePicUrl: contact.profilePicUrl },
+                });
+              }
+
</code_context>

<issue_to_address>
**issue (bug_risk):** The `update` variable is never used or awaited inside the async mapper, which can hide DB errors and make the intent unclear.

Within `updatedContacts.map(async (contact) => { ... })`, `update` is set to `this.prismaRepository.contact.updateMany(...)` but never awaited or returned. As a result, the DB write runs in the background, errors may be lost, and `Promise.all` does not actually wait for the update to finish. If the write must succeed, either `await this.prismaRepository.contact.updateMany(...)` or `return this.prismaRepository.contact.updateMany(...)` so `Promise.all` awaits it. If it’s intentionally best-effort, remove `let update` and call `void this.prismaRepository.contact.updateMany(...)` to clarify intent.
</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.

- Added missing conditional checks for `DATABASE_SAVE_DATA_CONTACTS` in `contacts.upsert` and `contacts.update` handlers.
- Fixed an issue where profile picture updates were attempting to save to the database even when disabled.
- Fixed an unawaited promise in `contacts.upsert` to ensure database operations complete correctly.
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.

1 participant