mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-30 18:47:51 +00:00 
			
		
		
		
	chore: Conversation custom attribute APIs (#3024)
This commit is contained in:
		
							
								
								
									
										6
									
								
								Gemfile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Gemfile
									
									
									
									
									
								
							| @@ -56,7 +56,6 @@ gem 'activerecord-import' | |||||||
| gem 'dotenv-rails' | gem 'dotenv-rails' | ||||||
| gem 'foreman' | gem 'foreman' | ||||||
| gem 'puma' | gem 'puma' | ||||||
| gem 'rack-timeout' |  | ||||||
| gem 'webpacker', '~> 5.x' | gem 'webpacker', '~> 5.x' | ||||||
| # metrics on heroku | # metrics on heroku | ||||||
| gem 'barnes' | gem 'barnes' | ||||||
| @@ -122,6 +121,11 @@ gem 'hairtrigger' | |||||||
|  |  | ||||||
| gem 'procore-sift' | gem 'procore-sift' | ||||||
|  |  | ||||||
|  | group :production, :staging do | ||||||
|  |   # we dont want request timing out in development while using byebug | ||||||
|  |   gem 'rack-timeout' | ||||||
|  | end | ||||||
|  |  | ||||||
| group :development do | group :development do | ||||||
|   gem 'annotate' |   gem 'annotate' | ||||||
|   gem 'bullet' |   gem 'bullet' | ||||||
|   | |||||||
| @@ -72,6 +72,11 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro | |||||||
|     @conversation.save! |     @conversation.save! | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   def custom_attributes | ||||||
|  |     @conversation.custom_attributes = params.permit(custom_attributes: {})[:custom_attributes] | ||||||
|  |     @conversation.save! | ||||||
|  |   end | ||||||
|  |  | ||||||
|   private |   private | ||||||
|  |  | ||||||
|   def set_conversation_status |   def set_conversation_status | ||||||
| @@ -112,6 +117,7 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro | |||||||
|  |  | ||||||
|   def conversation_params |   def conversation_params | ||||||
|     additional_attributes = params[:additional_attributes]&.permit! || {} |     additional_attributes = params[:additional_attributes]&.permit! || {} | ||||||
|  |     custom_attributes = params[:custom_attributes]&.permit! || {} | ||||||
|     status = params[:status].present? ? { status: params[:status] } : {} |     status = params[:status].present? ? { status: params[:status] } : {} | ||||||
|  |  | ||||||
|     # TODO: temporary fallback for the old bot status in conversation, we will remove after couple of releases |     # TODO: temporary fallback for the old bot status in conversation, we will remove after couple of releases | ||||||
| @@ -122,6 +128,7 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro | |||||||
|       contact_id: @contact_inbox.contact_id, |       contact_id: @contact_inbox.contact_id, | ||||||
|       contact_inbox_id: @contact_inbox.id, |       contact_inbox_id: @contact_inbox.id, | ||||||
|       additional_attributes: additional_attributes, |       additional_attributes: additional_attributes, | ||||||
|  |       custom_attributes: custom_attributes, | ||||||
|       snoozed_until: params[:snoozed_until] |       snoozed_until: params[:snoozed_until] | ||||||
|     }.merge(status) |     }.merge(status) | ||||||
|   end |   end | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ | |||||||
| #  additional_attributes :jsonb | #  additional_attributes :jsonb | ||||||
| #  agent_last_seen_at    :datetime | #  agent_last_seen_at    :datetime | ||||||
| #  contact_last_seen_at  :datetime | #  contact_last_seen_at  :datetime | ||||||
|  | #  custom_attributes     :jsonb | ||||||
| #  identifier            :string | #  identifier            :string | ||||||
| #  last_activity_at      :datetime         not null | #  last_activity_at      :datetime         not null | ||||||
| #  snoozed_until         :datetime | #  snoozed_until         :datetime | ||||||
|   | |||||||
| @@ -0,0 +1 @@ | |||||||
|  | json.custom_attributes @conversation.custom_attributes | ||||||
| @@ -33,5 +33,6 @@ 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.agent_last_seen_at conversation.agent_last_seen_at.to_i | ||||||
| json.unread_count conversation.unread_incoming_messages.count | json.unread_count conversation.unread_incoming_messages.count | ||||||
| json.additional_attributes conversation.additional_attributes | json.additional_attributes conversation.additional_attributes | ||||||
|  | json.custom_attributes conversation.custom_attributes | ||||||
| json.account_id conversation.account_id | json.account_id conversation.account_id | ||||||
| json.labels conversation.label_list | json.labels conversation.label_list | ||||||
|   | |||||||
| @@ -72,6 +72,7 @@ Rails.application.routes.draw do | |||||||
|               post :toggle_status |               post :toggle_status | ||||||
|               post :toggle_typing_status |               post :toggle_typing_status | ||||||
|               post :update_last_seen |               post :update_last_seen | ||||||
|  |               post :custom_attributes | ||||||
|             end |             end | ||||||
|           end |           end | ||||||
|  |  | ||||||
|   | |||||||
| @@ -0,0 +1,5 @@ | |||||||
|  | class AddCustomAttributesToConversations < ActiveRecord::Migration[6.1] | ||||||
|  |   def change | ||||||
|  |     add_column :conversations, :custom_attributes, :jsonb, default: {} | ||||||
|  |   end | ||||||
|  | end | ||||||
| @@ -10,7 +10,7 @@ | |||||||
| # | # | ||||||
| # It's strongly recommended that you check this file into your version control system. | # It's strongly recommended that you check this file into your version control system. | ||||||
|  |  | ||||||
| ActiveRecord::Schema.define(version: 2021_08_29_124254) do | ActiveRecord::Schema.define(version: 2021_09_16_060144) do | ||||||
|  |  | ||||||
|   # These are extensions that must be enabled in order to support this database |   # These are extensions that must be enabled in order to support this database | ||||||
|   enable_extension "pg_stat_statements" |   enable_extension "pg_stat_statements" | ||||||
| @@ -295,6 +295,7 @@ ActiveRecord::Schema.define(version: 2021_08_29_124254) do | |||||||
|     t.bigint "team_id" |     t.bigint "team_id" | ||||||
|     t.bigint "campaign_id" |     t.bigint "campaign_id" | ||||||
|     t.datetime "snoozed_until" |     t.datetime "snoozed_until" | ||||||
|  |     t.jsonb "custom_attributes", default: {} | ||||||
|     t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true |     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 ["account_id"], name: "index_conversations_on_account_id" | ||||||
|     t.index ["assignee_id", "account_id"], name: "index_conversations_on_assignee_id_and_account_id" |     t.index ["assignee_id", "account_id"], name: "index_conversations_on_assignee_id_and_account_id" | ||||||
|   | |||||||
| @@ -496,4 +496,37 @@ RSpec.describe 'Conversations API', type: :request do | |||||||
|       end |       end | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   describe 'POST /api/v1/accounts/{account.id}/conversations/:id/custom_attributes' do | ||||||
|  |     let(:conversation) { create(:conversation, account: account) } | ||||||
|  |  | ||||||
|  |     context 'when it is an unauthenticated user' do | ||||||
|  |       it 'returns unauthorized' do | ||||||
|  |         post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/custom_attributes" | ||||||
|  |  | ||||||
|  |         expect(response).to have_http_status(:unauthorized) | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     context 'when it is an authenticated user' do | ||||||
|  |       let(:agent) { create(:user, account: account, role: :agent) } | ||||||
|  |       let(:custom_attributes) { { user_id: 1001, created_date: '23/12/2012', subscription_id: 12 } } | ||||||
|  |       let(:valid_params) { { custom_attributes: custom_attributes } } | ||||||
|  |  | ||||||
|  |       before do | ||||||
|  |         create(:inbox_member, user: agent, inbox: conversation.inbox) | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'updates last seen' do | ||||||
|  |         post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/custom_attributes", | ||||||
|  |              headers: agent.create_new_auth_token, | ||||||
|  |              params: valid_params, | ||||||
|  |              as: :json | ||||||
|  |  | ||||||
|  |         expect(response).to have_http_status(:success) | ||||||
|  |         expect(conversation.reload.custom_attributes).not_to eq nil | ||||||
|  |         expect(conversation.reload.custom_attributes.count).to eq 3 | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |   end | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Muhsin Keloth
					Muhsin Keloth