From 5d5e75ce9ca62a153f75df788a6a4b7982ff703c Mon Sep 17 00:00:00 2001
From: Sojan Jose 
Date: Sun, 31 Jan 2021 13:05:07 +0530
Subject: [PATCH] Chore: Fix Deserialisation Error in Jobs (#1683)
- Ignore Jobs on de-serialization error
- Show empty conversations in chat UI
Fixes: #1480
---
 .../widgets/conversation/ConversationCard.vue |  6 ++++++
 .../dashboard/i18n/locale/en/chatlist.json    |  3 ++-
 app/jobs/application_job.rb                   |  4 ++++
 .../partials/_conversation.json.jbuilder      |  4 +++-
 .../accounts/conversations_controller_spec.rb | 20 ++++++++++++++++---
 5 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue b/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue
index 84ddd6b15..0b39013bf 100644
--- a/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue
+++ b/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue
@@ -35,6 +35,12 @@
           {{ this.$t(`${attachmentIconKey}.CONTENT`) }}
         
       
+      
+        
+        
+          {{ this.$t(`CHAT_LIST.NO_MESSAGES`) }}
+        
+      
       
         
           {{ dynamicTime(chat.timestamp) }}
diff --git a/app/javascript/dashboard/i18n/locale/en/chatlist.json b/app/javascript/dashboard/i18n/locale/en/chatlist.json
index a0b31427f..228554789 100644
--- a/app/javascript/dashboard/i18n/locale/en/chatlist.json
+++ b/app/javascript/dashboard/i18n/locale/en/chatlist.json
@@ -79,6 +79,7 @@
     },
     "RECEIVED_VIA_EMAIL": "Received via email",
     "VIEW_TWEET_IN_TWITTER": "View tweet in Twitter",
-    "REPLY_TO_TWEET": "Reply to this tweet"
+    "REPLY_TO_TWEET": "Reply to this tweet",
+    "NO_MESSAGES": "No Messages"
   }
 }
diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb
index a009ace51..c577b1908 100644
--- a/app/jobs/application_job.rb
+++ b/app/jobs/application_job.rb
@@ -1,2 +1,6 @@
 class ApplicationJob < ActiveJob::Base
+  # https://api.rubyonrails.org/v5.2.1/classes/ActiveJob/Exceptions/ClassMethods.html
+  discard_on ActiveJob::DeserializationError do |_job, error|
+    Rails.logger.error("Skipping job because of ActiveJob::DeserializationError (#{error.message})")
+  end
 end
diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
index 517f31054..50afe3b44 100644
--- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
+++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
@@ -11,7 +11,9 @@ json.meta do
 end
 
 json.id conversation.display_id
-if conversation.unread_incoming_messages.count.zero?
+if conversation.messages.count.zero?
+  json.messages []
+elsif conversation.unread_incoming_messages.count.zero?
   json.messages [conversation.messages.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last.try(:push_event_data)]
 else
   json.messages conversation.unread_messages.includes([:user, { attachments: [{ file_attachment: [:blob] }] }]).last(10).map(&:push_event_data)
diff --git a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb
index 5c88f2402..e2e025e82 100644
--- a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb
+++ b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb
@@ -14,19 +14,33 @@ RSpec.describe 'Conversations API', type: :request do
 
     context 'when it is an authenticated user' do
       let(:agent) { create(:user, account: account, role: :agent) }
+      let(:conversation) { create(:conversation, account: account) }
 
       before do
-        conversation = create(:conversation, account: account)
         create(:inbox_member, user: agent, inbox: conversation.inbox)
       end
 
-      it 'returns all conversations' do
+      it 'returns all conversations with messages' do
+        message = create(:message, conversation: conversation, account: account)
         get "/api/v1/accounts/#{account.id}/conversations",
             headers: agent.create_new_auth_token,
             as: :json
 
         expect(response).to have_http_status(:success)
-        expect(JSON.parse(response.body, symbolize_names: true)[:data][:meta][:all_count]).to eq(1)
+        body = JSON.parse(response.body, symbolize_names: true)
+        expect(body[:data][:meta][:all_count]).to eq(1)
+        expect(body[:data][:payload].first[:messages].first[:id]).to eq(message.id)
+      end
+
+      it 'returns conversations with empty messages array for conversations with out messages ' do
+        get "/api/v1/accounts/#{account.id}/conversations",
+            headers: agent.create_new_auth_token,
+            as: :json
+
+        expect(response).to have_http_status(:success)
+        body = JSON.parse(response.body, symbolize_names: true)
+        expect(body[:data][:meta][:all_count]).to eq(1)
+        expect(body[:data][:payload].first[:messages]).to eq([])
       end
     end
   end