evolution-api icon indicating copy to clipboard operation
evolution-api copied to clipboard

fix: respect DATABASE_SAVE_DATA_CONTACTS in contact updates

Open gabrielmouallem opened this issue 2 weeks ago โ€ข 1 comments

๐Ÿ“‹ 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

  • [x] ๐Ÿ› 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

  • [x] Manual testing completed
  • [x] Functionality verified in development environment
  • [x] No breaking changes introduced
  • [x] Tested with different connection types (if applicable)

๐Ÿ“ธ Screenshots (if applicable)

โœ… Checklist

  • [x] My code follows the project's style guidelines
  • [x] I have performed a self-review of my code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [x] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [x] I have manually tested my changes thoroughly
  • [x] I have verified the changes work with different scenarios
  • [x] 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.

gabrielmouallem avatar Nov 24 '25 02:11 gabrielmouallem

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"

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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Nov 24 '25 02:11 sourcery-ai[bot]

@Vitordotpy ty! I'm new when it comes to open source contributions, what else we need to ship?

gabrielmouallem avatar Dec 04 '25 19:12 gabrielmouallem