From cd5646b4b9aee239d68887bf8687d23a52a18c31 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 5 Apr 2022 14:00:07 +0530 Subject: [PATCH] fix: Skip `contact_last_seen_at` in empty conversation payload (#4387) * Fix undefined `last_seen_at` empty conversation * chore: remove the comment Co-authored-by: Sojan Jose --- .../api/v1/widget/messages/index.json.jbuilder | 2 +- .../api/v1/widget/messages_controller_spec.rb | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/views/api/v1/widget/messages/index.json.jbuilder b/app/views/api/v1/widget/messages/index.json.jbuilder index d7a441b84..29e80815d 100644 --- a/app/views/api/v1/widget/messages/index.json.jbuilder +++ b/app/views/api/v1/widget/messages/index.json.jbuilder @@ -12,5 +12,5 @@ json.payload do end end json.meta do - json.contact_last_seen_at @conversation.contact_last_seen_at.to_i + json.contact_last_seen_at @conversation.contact_last_seen_at.to_i if @conversation.present? end diff --git a/spec/controllers/api/v1/widget/messages_controller_spec.rb b/spec/controllers/api/v1/widget/messages_controller_spec.rb index f203f11c8..208327c74 100644 --- a/spec/controllers/api/v1/widget/messages_controller_spec.rb +++ b/spec/controllers/api/v1/widget/messages_controller_spec.rb @@ -9,8 +9,8 @@ RSpec.describe '/api/v1/widget/messages', type: :request do let(:payload) { { source_id: contact_inbox.source_id, inbox_id: web_widget.inbox.id } } let(:token) { ::Widget::TokenService.new(payload: payload).generate_token } - before do - 2.times.each { create(:message, account: account, inbox: web_widget.inbox, conversation: conversation) } + before do |example| + 2.times.each { create(:message, account: account, inbox: web_widget.inbox, conversation: conversation) } unless example.metadata[:skip_before] end describe 'GET /api/v1/widget/messages' do @@ -25,6 +25,18 @@ RSpec.describe '/api/v1/widget/messages', type: :request do json_response = JSON.parse(response.body) # 2 messages created + 2 messages by the email hook expect(json_response['payload'].length).to eq(4) + expect(json_response['meta']).not_to be_empty + end + + it 'returns empty messages', :skip_before do + get api_v1_widget_messages_url, + params: { website_token: web_widget.website_token }, + headers: { 'X-Auth-Token' => token }, + as: :json + + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + expect(json_response['payload'].length).to eq(0) end end end