-
Notifications
You must be signed in to change notification settings - Fork 5.2k
fix: respect DATABASE_SAVE_DATA_CONTACTS in contact updates #2250
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
fix: respect DATABASE_SAVE_DATA_CONTACTS in contact updates #2250
Conversation
Reviewer's GuideConditionally 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.CONTACTSsequenceDiagram
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
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Outdated
Show resolved
Hide resolved
- 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.
|
@Vitordotpy ty! I'm new when it comes to open source contributions, what else we need to ship? |
|
Please, fix the lint with |
Sure, ty! |
📋 Description
The
DATABASE_SAVE_DATA_CONTACTSenvironment variable was being ignored in specific parts of thecontacts.upsert(profile picture updates) andcontacts.updatehandlers.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_DATAflags.🔗 Related Issue
Closes #(issue_number)
🧪 Type of Change
🧪 Testing
📸 Screenshots (if applicable)
✅ Checklist
📝 Additional Notes
Summary by Sourcery
Respect database configuration flags when processing WhatsApp contact updates to avoid persisting contacts when disabled.
Bug Fixes:
Enhancements: