From c5360ec0a618b40a33fa5f2bcde1b97ea866a222 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Fri, 2 Jun 2023 18:56:44 +0530 Subject: [PATCH] feat: update jbuilder and query UNSTABLE --- app/models/conversation_with_label.rb | 36 +++++++++++++++++++ app/services/conversations/filter_service.rb | 10 +++--- .../partials/_conversation.json.jbuilder | 24 ++++++------- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/app/models/conversation_with_label.rb b/app/models/conversation_with_label.rb index 9e3208c52..3c4ddb061 100644 --- a/app/models/conversation_with_label.rb +++ b/app/models/conversation_with_label.rb @@ -36,13 +36,49 @@ # index_conversation_with_labels_on_last_activity_at (last_activity_at) # class ConversationWithLabel < ApplicationRecord + self.primary_key = :id + + belongs_to :account + belongs_to :inbox + belongs_to :assignee, class_name: 'User', optional: true + belongs_to :contact + belongs_to :contact_inbox + belongs_to :team, optional: true + belongs_to :campaign, optional: true + + has_many :mentions + has_many :messages + has_one :csat_survey_response + has_many :conversation_participants + has_many :notifications, as: :primary_actor + has_many :attachments, through: :messages + + scope :assigned_to, ->(agent) { where(assignee_id: agent.id) } + scope :unassigned, -> { where(assignee_id: nil) } + scope :assigned, -> { where.not(assignee_id: nil) } + scope :latest, -> { order(last_activity_at: :desc) } + def self.refresh # https://github.com/scenic-views/scenic#what-about-materialized-views # needs atleast one unuque index for concurrent refresh Scenic.database.refresh_materialized_view(table_name, concurrently: true, cascade: false) end + def label_list + labels_array + end + + def muted? + Redis::Alfred.get(mute_key).present? + end + def readonly? true end + + private + + def mute_key + format(Redis::RedisKeys::CONVERSATION_MUTE_KEY, id: id) + end end diff --git a/app/services/conversations/filter_service.rb b/app/services/conversations/filter_service.rb index 012dd662f..21728c4e4 100644 --- a/app/services/conversations/filter_service.rb +++ b/app/services/conversations/filter_service.rb @@ -36,20 +36,20 @@ class Conversations::FilterService < FilterService case current_filter['attribute_type'] when 'additional_attributes' - " conversations.additional_attributes ->> '#{attribute_key}' #{filter_operator_value} #{query_operator} " + " additional_attributes ->> '#{attribute_key}' #{filter_operator_value} #{query_operator} " when 'date_attributes' - " (conversations.#{attribute_key})::#{current_filter['data_type']} #{filter_operator_value}#{current_filter['data_type']} #{query_operator} " + " (#{attribute_key})::#{current_filter['data_type']} #{filter_operator_value}#{current_filter['data_type']} #{query_operator} " when 'standard' if attribute_key == 'labels' " tags.name #{filter_operator_value} #{query_operator} " else - " conversations.#{attribute_key} #{filter_operator_value} #{query_operator} " + " #{attribute_key} #{filter_operator_value} #{query_operator} " end end end def base_relation - Current.account.conversations.left_outer_joins(:labels) + ConversationWithLabel.where(account_id: Current.account.id) end def current_page @@ -58,7 +58,7 @@ class Conversations::FilterService < FilterService def conversations @conversations = @conversations.includes( - :taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :messages, :contact_inbox + :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team ) @conversations.latest.page(current_page) diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder index 82b9caacf..be69fe9f6 100644 --- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder +++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder @@ -16,20 +16,20 @@ json.meta do json.hmac_verified conversation.contact_inbox&.hmac_verified end -json.id conversation.display_id -if conversation.messages.first.blank? - 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([{ attachments: [{ file_attachment: [:blob] }] }]).last(10).map(&:push_event_data) -end +# json.id conversation.display_id +# if conversation.messages.first.blank? +# 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([{ attachments: [{ file_attachment: [:blob] }] }]).last(10).map(&:push_event_data) +# end json.account_id conversation.account_id json.additional_attributes conversation.additional_attributes json.agent_last_seen_at conversation.agent_last_seen_at.to_i json.assignee_last_seen_at conversation.assignee_last_seen_at.to_i -json.can_reply conversation.can_reply? +# json.can_reply conversation.can_reply? json.contact_last_seen_at conversation.contact_last_seen_at.to_i json.custom_attributes conversation.custom_attributes json.inbox_id conversation.inbox_id @@ -39,8 +39,8 @@ json.snoozed_until conversation.snoozed_until json.status conversation.status json.created_at conversation.created_at.to_i json.timestamp conversation.last_activity_at.to_i -json.first_reply_created_at conversation.first_reply_created_at.to_i -json.unread_count conversation.unread_incoming_messages.count -json.last_non_activity_message conversation.messages.non_activity_messages.first.try(:push_event_data) +# json.first_reply_created_at conversation.first_reply_created_at.to_i +# json.unread_count conversation.unread_incoming_messages.count +# json.last_non_activity_message conversation.messages.non_activity_messages.first.try(:push_event_data) json.last_activity_at conversation.last_activity_at.to_i json.priority conversation.priority