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.
This commit is contained in:
Muhsin Keloth
2025-09-25 18:19:09 +05:30
committed by GitHub
parent 03d0688cc2
commit cd2c58726f

View File

@@ -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