fix: message_type inconsistency across message end points (#10108)

The `before_type_cast` method sometimes returns a string for
`message_type`, creating inconsistencies in different payloads. This
pull request will remove all `before_type_cast` usage and replace it
with `to_i` methods.
This commit is contained in:
Muhsin Keloth
2024-09-16 16:14:35 +05:30
committed by GitHub
parent b9ff164041
commit 05b8486538
13 changed files with 40 additions and 22 deletions

View File

@@ -137,7 +137,7 @@ class Message < ApplicationRecord
def push_event_data def push_event_data
data = attributes.symbolize_keys.merge( data = attributes.symbolize_keys.merge(
created_at: created_at.to_i, created_at: created_at.to_i,
message_type: message_type_before_type_cast, message_type: message_type.to_i,
conversation_id: conversation.display_id, conversation_id: conversation.display_id,
conversation: conversation_push_event_data conversation: conversation_push_event_data
) )

View File

@@ -1,6 +1,6 @@
json.id message.id json.id message.id
json.content message.content json.content message.content
json.message_type message.message_type_before_type_cast json.message_type message.message_type.to_i
json.content_type message.content_type json.content_type message.content_type
json.source_id message.source_id json.source_id message.source_id
json.inbox_id message.inbox_id json.inbox_id message.inbox_id

View File

@@ -17,7 +17,7 @@ json.messages do
json.content message.content json.content message.content
json.id message.id json.id message.id
json.sender_name message.sender.name if message.sender json.sender_name message.sender.name if message.sender
json.message_type message.message_type_before_type_cast json.message_type message.message_type.to_i
json.created_at message.created_at.to_i json.created_at message.created_at.to_i
end end
end end

View File

@@ -3,7 +3,7 @@ json.content message.content
json.inbox_id message.inbox_id json.inbox_id message.inbox_id
json.echo_id message.echo_id if message.echo_id json.echo_id message.echo_id if message.echo_id
json.conversation_id message.conversation.display_id json.conversation_id message.conversation.display_id
json.message_type message.message_type_before_type_cast json.message_type message.message_type.to_i
json.content_type message.content_type json.content_type message.content_type
json.status message.status json.status message.status
json.content_attributes message.content_attributes json.content_attributes message.content_attributes

View File

@@ -1,6 +1,6 @@
json.id resource.id json.id resource.id
json.content resource.content json.content resource.content
json.message_type resource.message_type_before_type_cast json.message_type resource.message_type.to_i
json.content_type resource.content_type json.content_type resource.content_type
json.content_attributes resource.content_attributes json.content_attributes resource.content_attributes
json.created_at resource.created_at.to_i json.created_at resource.created_at.to_i

View File

@@ -2,7 +2,7 @@ json.id @message.id
json.content @message.content json.content @message.content
json.inbox_id @message.inbox_id json.inbox_id @message.inbox_id
json.conversation_id @message.conversation.display_id json.conversation_id @message.conversation.display_id
json.message_type @message.message_type_before_type_cast json.message_type @message.message_type.to_i
json.created_at @message.created_at.to_i json.created_at @message.created_at.to_i
json.private @message.private json.private @message.private
json.source_id @message.source_id json.source_id @message.source_id

View File

@@ -2,7 +2,7 @@ json.payload do
json.array! @messages do |message| json.array! @messages do |message|
json.id message.id json.id message.id
json.content message.content json.content message.content
json.message_type message.message_type_before_type_cast json.message_type message.message_type.to_i
json.content_type message.content_type json.content_type message.content_type
json.content_attributes message.content_attributes json.content_attributes message.content_attributes
json.created_at message.created_at.to_i json.created_at message.created_at.to_i

View File

@@ -1,6 +1,6 @@
json.id resource.id json.id resource.id
json.content resource.content json.content resource.content
json.message_type resource.message_type_before_type_cast json.message_type resource.message_type.to_i
json.content_type resource.content_type json.content_type resource.content_type
json.content_attributes resource.content_attributes json.content_attributes resource.content_attributes
json.created_at resource.created_at.to_i json.created_at resource.created_at.to_i

View File

@@ -112,6 +112,7 @@ RSpec.describe 'Search', type: :request do
expect(response_data[:payload].keys).to contain_exactly(:messages) expect(response_data[:payload].keys).to contain_exactly(:messages)
expect(response_data[:payload][:messages].length).to eq 2 expect(response_data[:payload][:messages].length).to eq 2
expect(response_data[:payload][:messages].first[:message_type]).to eq 0
end end
end end
end end

View File

@@ -13,6 +13,21 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do
Widget::TokenService.new(payload: { source_id: second_session.source_id, inbox_id: web_widget.inbox.id }).generate_token Widget::TokenService.new(payload: { source_id: second_session.source_id, inbox_id: web_widget.inbox.id }).generate_token
end end
def conversation_params
{
website_token: web_widget.website_token,
contact: {
name: 'contact-name',
email: 'contact-email@chatwoot.com',
phone_number: '+919745313456'
},
message: {
content: 'This is a test message'
},
custom_attributes: { order_id: '12345' }
}
end
describe 'GET /api/v1/widget/conversations' do describe 'GET /api/v1/widget/conversations' do
context 'with a conversation' do context 'with a conversation' do
it 'returns the correct conversation params' do it 'returns the correct conversation params' do
@@ -47,21 +62,10 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do
end end
describe 'POST /api/v1/widget/conversations' do describe 'POST /api/v1/widget/conversations' do
it 'creates a conversation' do it 'creates a conversation with correct details' do
post '/api/v1/widget/conversations', post '/api/v1/widget/conversations',
headers: { 'X-Auth-Token' => token }, headers: { 'X-Auth-Token' => token },
params: { params: conversation_params,
website_token: web_widget.website_token,
contact: {
name: 'contact-name',
email: 'contact-email@chatwoot.com',
phone_number: '+919745313456'
},
message: {
content: 'This is a test message'
},
custom_attributes: { order_id: '12345' }
},
as: :json as: :json
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
@@ -70,8 +74,19 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do
expect(json_response['contact']['email']).to eq 'contact-email@chatwoot.com' expect(json_response['contact']['email']).to eq 'contact-email@chatwoot.com'
expect(json_response['contact']['phone_number']).to eq '+919745313456' expect(json_response['contact']['phone_number']).to eq '+919745313456'
expect(json_response['contact']['name']).to eq 'contact-name' expect(json_response['contact']['name']).to eq 'contact-name'
end
it 'creates a conversation with correct message and custom attributes' do
post '/api/v1/widget/conversations',
headers: { 'X-Auth-Token' => token },
params: conversation_params,
as: :json
expect(response).to have_http_status(:success)
json_response = response.parsed_body
expect(json_response['custom_attributes']['order_id']).to eq '12345' expect(json_response['custom_attributes']['order_id']).to eq '12345'
expect(json_response['messages'][0]['content']).to eq 'This is a test message' expect(json_response['messages'][0]['content']).to eq 'This is a test message'
expect(json_response['messages'][0]['message_type']).to eq 0
end end
it 'create a conversation with a name and without an email' do it 'create a conversation with a name and without an email' do

View File

@@ -54,6 +54,7 @@ RSpec.describe '/api/v1/widget/messages', type: :request do
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
json_response = response.parsed_body json_response = response.parsed_body
expect(json_response['content']).to eq(message_params[:content]) expect(json_response['content']).to eq(message_params[:content])
expect(json_response['message_type']).to eq 0
end end
it 'does not create the message' do it 'does not create the message' do

View File

@@ -26,6 +26,7 @@ RSpec.describe 'Public Inbox Contact Conversation Messages API', type: :request
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
data = response.parsed_body data = response.parsed_body
expect(data['content']).to eq('hello') expect(data['content']).to eq('hello')
expect(data['message_type']).to eq(0)
end end
it 'does not create the message' do it 'does not create the message' do

View File

@@ -79,7 +79,7 @@ RSpec.describe Message do
external_source_ids: message.external_source_ids, external_source_ids: message.external_source_ids,
id: message.id, id: message.id,
inbox_id: message.inbox_id, inbox_id: message.inbox_id,
message_type: message.message_type_before_type_cast, message_type: message.message_type.to_i,
private: message.private, private: message.private,
processed_message_content: message.processed_message_content, processed_message_content: message.processed_message_content,
sender_id: message.sender_id, sender_id: message.sender_id,