diff --git a/app/controllers/api/v1/accounts/conversations_controller.rb b/app/controllers/api/v1/accounts/conversations_controller.rb index 59dc051a7..5e7c7d433 100644 --- a/app/controllers/api/v1/accounts/conversations_controller.rb +++ b/app/controllers/api/v1/accounts/conversations_controller.rb @@ -69,6 +69,7 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro def update_last_seen @conversation.agent_last_seen_at = DateTime.now.utc + @conversation.assignee_last_seen_at = DateTime.now.utc if assignee? @conversation.save! end @@ -136,4 +137,8 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro def conversation_finder @conversation_finder ||= ConversationFinder.new(current_user, params) end + + def assignee? + @conversation.assignee_id? && current_user == @conversation.assignee + end end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 71f6d0a36..bc4db57b6 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -5,6 +5,7 @@ # id :integer not null, primary key # additional_attributes :jsonb # agent_last_seen_at :datetime +# assignee_last_seen_at :datetime # contact_last_seen_at :datetime # custom_attributes :jsonb # identifier :string diff --git a/app/views/api/v1/accounts/conversations/messages/index.json.jbuilder b/app/views/api/v1/accounts/conversations/messages/index.json.jbuilder index db58b3faa..e1a3e1125 100644 --- a/app/views/api/v1/accounts/conversations/messages/index.json.jbuilder +++ b/app/views/api/v1/accounts/conversations/messages/index.json.jbuilder @@ -4,6 +4,7 @@ json.meta do json.contact @conversation.contact.push_event_data json.assignee @conversation.assignee.push_event_data if @conversation.assignee.present? json.agent_last_seen_at @conversation.agent_last_seen_at + json.assignee_last_seen_at @conversation.assignee_last_seen_at end json.payload do diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder index 1e35e8217..411cbf8f1 100644 --- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder +++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder @@ -31,6 +31,7 @@ json.can_reply conversation.can_reply? json.timestamp conversation.last_activity_at.to_i json.contact_last_seen_at conversation.contact_last_seen_at.to_i 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.unread_count conversation.unread_incoming_messages.count json.additional_attributes conversation.additional_attributes json.custom_attributes conversation.custom_attributes diff --git a/db/migrate/20210922082754_add_assignee_last_seen.rb b/db/migrate/20210922082754_add_assignee_last_seen.rb new file mode 100644 index 000000000..41ff17818 --- /dev/null +++ b/db/migrate/20210922082754_add_assignee_last_seen.rb @@ -0,0 +1,5 @@ +class AddAssigneeLastSeen < ActiveRecord::Migration[6.1] + def change + add_column :conversations, :assignee_last_seen_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index dcb1fe612..2b3d5d274 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_09_16_060144) do +ActiveRecord::Schema.define(version: 2021_09_22_082754) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -296,6 +296,7 @@ ActiveRecord::Schema.define(version: 2021_09_16_060144) do t.bigint "campaign_id" t.datetime "snoozed_until" t.jsonb "custom_attributes", default: {} + t.datetime "assignee_last_seen_at" t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true t.index ["account_id"], name: "index_conversations_on_account_id" t.index ["assignee_id", "account_id"], name: "index_conversations_on_assignee_id_and_account_id" diff --git a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb index ec53ec853..fdb8e51d5 100644 --- a/spec/controllers/api/v1/accounts/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations_controller_spec.rb @@ -393,6 +393,19 @@ RSpec.describe 'Conversations API', type: :request do expect(response).to have_http_status(:success) expect(conversation.reload.agent_last_seen_at).not_to eq nil end + + it 'updates assignee last seen' do + conversation.update!(assignee_id: agent.id) + + expect(conversation.reload.assignee_last_seen_at).to eq nil + + post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen", + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(conversation.reload.assignee_last_seen_at).not_to eq nil + end end end