Files
leadchat/app/presenters/messages/search_data_presenter.rb
Tanmay Deep Sharma 75f75ce786 fix: sanitize integer fields to prevent Elasticsearch mapping errors (#13276)
## Linear task:

https://linear.app/chatwoot/issue/CW-6318/searchkickimporterror-type-=-mapper-parsing-exception-reason-=-failed

## Description

Fixes Elasticsearch `mapper_parsing_exception` errors that occur when
`campaign_id` contain non-numeric string values

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

- Unit tests
- use a local OpenSearch 3.4.0 cluster to verify actual indexing
behavior.


## Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Removes `campaign_id` from the message search index payload to
simplify `additional_attributes`, keeping only `automation_rule_id`.
> 
> - `Messages::SearchDataPresenter#additional_attributes_data` now
returns only `automation_rule_id`
> - Specs updated to stop asserting `campaign_id` and continue
validating `automation_rule_id` and email subject handling
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
5a9c8eb794a044e3f258b644f67a6731de9e904c. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
2026-01-22 18:29:49 +05:30

58 lines
1.2 KiB
Ruby

class Messages::SearchDataPresenter < SimpleDelegator
def search_data
{
**searchable_content,
**message_attributes,
additional_attributes: additional_attributes_data,
conversation: conversation_data
}
end
private
def searchable_content
{
content: content,
attachments: attachment_data,
content_attributes: content_attributes_data
}
end
def message_attributes
{
account_id: account_id,
inbox_id: inbox_id,
conversation_id: conversation_id,
message_type: message_type,
private: private,
created_at: created_at,
source_id: source_id,
sender_id: sender_id,
sender_type: sender_type
}
end
def attachment_data
attachments.filter_map do |a|
{ transcribed_text: a.meta&.dig('transcribed_text') }
end.presence
end
def content_attributes_data
email_subject = content_attributes.dig(:email, :subject)
return {} if email_subject.blank?
{ email: { subject: email_subject } }
end
def conversation_data
{ id: conversation.display_id }
end
def additional_attributes_data
{
automation_rule_id: content_attributes&.dig('automation_rule_id')
}
end
end