From cd2c58726f2dc094dc06b15bc42ba37cbb04887b Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 25 Sep 2025 18:19:09 +0530 Subject: [PATCH] fix: Ensure message is always present in `conversation_created` webhook for WhatsApp attachment messages (#12507) Fixes https://github.com/chatwoot/chatwoot/issues/11753 and https://github.com/chatwoot/chatwoot/issues/12442 **Problem** When a WhatsApp conversation started with a media message, the conversation created webhook would sometimes fire before the message and its relationships were fully committed to the database. This resulted in the message being missing from the webhook payload, breaking external automations that rely on this field. **Solution** Added `ActiveRecord::Base.transaction` wrapper around the core message processing operations in `Whatsapp::IncomingMessageBaseService` to ensure atomic execution: - `set_conversation` (creates conversation) - `create_messages` (creates message with account_id) - `clear_message_source_id_from_redis` (cleanup) Now the webhook only triggers after all related data is fully persisted, guaranteeing message availability. --- app/services/whatsapp/incoming_message_base_service.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/services/whatsapp/incoming_message_base_service.rb b/app/services/whatsapp/incoming_message_base_service.rb index 0aed8dba0..315182fcd 100644 --- a/app/services/whatsapp/incoming_message_base_service.rb +++ b/app/services/whatsapp/incoming_message_base_service.rb @@ -32,9 +32,11 @@ class Whatsapp::IncomingMessageBaseService set_contact return unless @contact - set_conversation - create_messages - clear_message_source_id_from_redis + ActiveRecord::Base.transaction do + set_conversation + create_messages + clear_message_source_id_from_redis + end end def process_statuses