diff --git a/app/controllers/api/v1/inboxes_controller.rb b/app/controllers/api/v1/inboxes_controller.rb index 00b6d0c1f..e9005a0c7 100644 --- a/app/controllers/api/v1/inboxes_controller.rb +++ b/app/controllers/api/v1/inboxes_controller.rb @@ -1,6 +1,6 @@ class Api::V1::InboxesController < Api::BaseController before_action :check_authorization - before_action :fetch_inbox, only: [:destroy] + before_action :fetch_inbox, only: [:destroy, :update] def index @inboxes = policy_scope(current_account.inboxes) @@ -11,6 +11,10 @@ class Api::V1::InboxesController < Api::BaseController head :ok end + def update + @inbox.update(inbox_update_params) + end + private def fetch_inbox @@ -20,4 +24,8 @@ class Api::V1::InboxesController < Api::BaseController def check_authorization authorize(Inbox) end + + def inbox_update_params + params.require(:inbox).permit(:enable_auto_assignment) + end end diff --git a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json index 659c0d7fc..00f53f8e1 100644 --- a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json +++ b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json @@ -74,7 +74,12 @@ "EDIT": { "API": { "SUCCESS_MESSAGE": "Widget color updated successfully", + "AUTO_ASSIGNMENT_SUCCESS_MESSAGE": "Auto assignment updated successfully", "ERROR_MESSAGE": "Could not update widget color. Please try again later." + }, + "AUTO_ASSIGNMENT": { + "ENABLED": "Enabled", + "DISABLED": "Disabled" } }, "DELETE": { @@ -96,7 +101,9 @@ "MESSENGER_SUB_HEAD": "Place this button inside your body tag", "INBOX_AGENTS": "Agents", "INBOX_AGENTS_SUB_TEXT": "Add or remove agents from this inbox", - "UPDATE": "Update" + "UPDATE": "Update", + "AUTO_ASSIGNMENT": "Enable auto assignment", + "AUTO_ASSIGNMENT_SUB_TEXT": "Enable or disable the automatic assignment of available agents on new conversations" } } } diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue index 3c5954233..7bce9e6f9 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue @@ -60,6 +60,20 @@ @select="$v.selectedAgents.$touch" /> +
+ + + +
@@ -82,6 +96,7 @@ export default { data() { return { selectedAgents: [], + autoAssignment: false, isUpdating: false, isAgentListUpdating: false, }; @@ -123,6 +138,7 @@ export default { this.$store.dispatch('agents/get'); this.$store.dispatch('inboxes/get').then(() => { this.fetchAttachedAgents(); + this.autoAssignment = this.inbox.enable_auto_assignment; }); }, async fetchAttachedAgents() { @@ -172,6 +188,19 @@ export default { this.showAlert(this.$t('INBOX_MGMT.EDIT.API.SUCCESS_MESSAGE')); } }, + async updateAutoAssignment() { + try { + await this.$store.dispatch('inboxes/updateAutoAssignment', { + id: this.currentInboxId, + inbox: { + enable_auto_assignment: this.autoAssignment, + }, + }); + this.showAlert(this.$t('INBOX_MGMT.EDIT.API.AUTO_ASSIGNMENT_SUCCESS_MESSAGE')); + } catch (error) { + this.showAlert(this.$t('INBOX_MGMT.EDIT.API.AUTO_ASSIGNMENT_SUCCESS_MESSAGE')); + } + }, getWidgetColor() { return typeof this.inbox.widget_color !== 'object' ? this.inbox.widget_color diff --git a/app/javascript/dashboard/store/modules/inboxes.js b/app/javascript/dashboard/store/modules/inboxes.js index 2b5557fda..b4237cba2 100644 --- a/app/javascript/dashboard/store/modules/inboxes.js +++ b/app/javascript/dashboard/store/modules/inboxes.js @@ -11,6 +11,7 @@ export const state = { isFetchingItem: false, isCreating: false, isUpdating: false, + isUpdatingAutoAssignment: false, isDeleting: false, }, }; @@ -76,6 +77,17 @@ export const actions = { throw new Error(error); } }, + updateAutoAssignment: async ({ commit }, { id, ...inboxParams }) => { + commit(types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: true }); + try { + const response = await InboxesAPI.update(id, inboxParams); + commit(types.default.EDIT_INBOXES, response.data); + commit(types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: false }); + } catch (error) { + commit(types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: false }); + throw new Error(error); + } + }, delete: async ({ commit }, inboxId) => { commit(types.default.SET_INBOXES_UI_FLAG, { isDeleting: true }); try { diff --git a/app/javascript/dashboard/store/modules/specs/inboxes/actions.spec.js b/app/javascript/dashboard/store/modules/specs/inboxes/actions.spec.js index f592ffff7..a127512b8 100644 --- a/app/javascript/dashboard/store/modules/specs/inboxes/actions.spec.js +++ b/app/javascript/dashboard/store/modules/specs/inboxes/actions.spec.js @@ -91,6 +91,31 @@ describe('#actions', () => { ]); }); }); + + describe('#updateAutoAssignment', () => { + it('sends correct actions if API is success', async () => { + let updatedInbox = inboxList[0]; + updatedInbox.enable_auto_assignment = false; + + axios.patch.mockResolvedValue({ data: updatedInbox }); + await actions.updateAutoAssignment({ commit }, { id: updatedInbox.id, inbox: { enable_auto_assignment: false} }); + expect(commit.mock.calls).toEqual([ + [types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: true }], + [types.default.EDIT_INBOXES, updatedInbox], + [types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: false }], + ]); + }); + it('sends correct actions if API is error', async () => { + axios.patch.mockRejectedValue({ message: 'Incorrect header' }); + await expect( + actions.updateAutoAssignment({ commit }, { id: inboxList[0].id, inbox: { enable_auto_assignment: false} }) + ).rejects.toThrow(Error); + expect(commit.mock.calls).toEqual([ + [types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: true }], + [types.default.SET_INBOXES_UI_FLAG, { isUpdatingAutoAssignment: false }], + ]); + }); + }); describe('#delete', () => { it('sends correct actions if API is success', async () => { diff --git a/app/javascript/dashboard/store/modules/specs/inboxes/fixtures.js b/app/javascript/dashboard/store/modules/specs/inboxes/fixtures.js index bb207df4d..6f05778a2 100644 --- a/app/javascript/dashboard/store/modules/specs/inboxes/fixtures.js +++ b/app/javascript/dashboard/store/modules/specs/inboxes/fixtures.js @@ -8,6 +8,7 @@ export default [ page_id: '12345', widget_color: null, website_token: null, + enable_auto_assignment: true, }, { id: 2, @@ -18,6 +19,7 @@ export default [ page_id: null, widget_color: '#7B64FF', website_token: 'randomid123', + enable_auto_assignment: true, }, { id: 3, @@ -28,6 +30,7 @@ export default [ page_id: null, widget_color: '#68BC00', website_token: 'randomid124', + enable_auto_assignment: true, }, { id: 4, @@ -38,5 +41,6 @@ export default [ page_id: null, widget_color: '#68BC00', website_token: 'randomid125', + enable_auto_assignment: true, }, ]; diff --git a/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js b/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js index d77f46fc0..5bd531a80 100644 --- a/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js +++ b/app/javascript/dashboard/store/modules/specs/inboxes/getters.spec.js @@ -22,6 +22,7 @@ describe('#getters', () => { page_id: '12345', widget_color: null, website_token: null, + enable_auto_assignment: true, }); }); @@ -32,6 +33,7 @@ describe('#getters', () => { isFetchingItem: false, isCreating: false, isUpdating: false, + isUpdatingAutoAssignment: false, isDeleting: false, }, }; @@ -40,6 +42,7 @@ describe('#getters', () => { isFetchingItem: false, isCreating: false, isUpdating: false, + isUpdatingAutoAssignment: false, isDeleting: false, }); }); diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 2fa1205a6..924deaaa1 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -151,6 +151,7 @@ class Conversation < ApplicationRecord def run_round_robin # return unless conversation.account.has_feature?(round_robin) # return unless conversation.account.round_robin_enabled? + return unless inbox.enable_auto_assignment return if assignee inbox.next_available_agent.then { |new_assignee| update_assignee(new_assignee) } diff --git a/app/models/inbox.rb b/app/models/inbox.rb index db6f3775c..fdc717c84 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -4,13 +4,14 @@ # # Table name: inboxes # -# id :integer not null, primary key -# channel_type :string -# name :string not null -# created_at :datetime not null -# updated_at :datetime not null -# account_id :integer not null -# channel_id :integer not null +# id :integer not null, primary key +# channel_type :string +# enable_auto_assignment :boolean default(TRUE) +# name :string not null +# created_at :datetime not null +# updated_at :datetime not null +# account_id :integer not null +# channel_id :integer not null # # Indexes # diff --git a/app/views/api/v1/inboxes/index.json.jbuilder b/app/views/api/v1/inboxes/index.json.jbuilder index 8d0103de2..3256f04cd 100644 --- a/app/views/api/v1/inboxes/index.json.jbuilder +++ b/app/views/api/v1/inboxes/index.json.jbuilder @@ -8,5 +8,6 @@ json.payload do json.page_id inbox.channel.try(:page_id) json.widget_color inbox.channel.try(:widget_color) json.website_token inbox.channel.try(:website_token) + json.enable_auto_assignment inbox.enable_auto_assignment end end diff --git a/config/routes.rb b/config/routes.rb index 85eba5b73..b1d718576 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,7 +44,7 @@ Rails.application.routes.draw do resource :profile, only: [:show, :update] resources :accounts, only: [:create] - resources :inboxes, only: [:index, :destroy] + resources :inboxes, only: [:index, :destroy, :update] resources :agents, except: [:show, :edit, :new] resources :labels, only: [:index] do collection do diff --git a/db/migrate/20200217192734_add_enable_auto_assignment_to_inboxes.rb b/db/migrate/20200217192734_add_enable_auto_assignment_to_inboxes.rb new file mode 100644 index 000000000..85dbc3a9b --- /dev/null +++ b/db/migrate/20200217192734_add_enable_auto_assignment_to_inboxes.rb @@ -0,0 +1,5 @@ +class AddEnableAutoAssignmentToInboxes < ActiveRecord::Migration[6.0] + def change + add_column :inboxes, :enable_auto_assignment, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 9a1376f90..daeeaae56 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,265 +10,267 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20_200_213_054_733) do +ActiveRecord::Schema.define(version: 2020_02_17_192734) do + # These are extensions that must be enabled in order to support this database - enable_extension 'plpgsql' + enable_extension "plpgsql" - create_table 'accounts', id: :serial, force: :cascade do |t| - t.string 'name', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false + create_table "accounts", id: :serial, force: :cascade do |t| + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end - create_table 'active_storage_attachments', force: :cascade do |t| - t.string 'name', null: false - t.string 'record_type', null: false - t.bigint 'record_id', null: false - t.bigint 'blob_id', null: false - t.datetime 'created_at', null: false - t.index ['blob_id'], name: 'index_active_storage_attachments_on_blob_id' - t.index %w[record_type record_id name blob_id], name: 'index_active_storage_attachments_uniqueness', unique: true + create_table "active_storage_attachments", force: :cascade do |t| + t.string "name", null: false + t.string "record_type", null: false + t.bigint "record_id", null: false + t.bigint "blob_id", null: false + t.datetime "created_at", null: false + t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" + t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end - create_table 'active_storage_blobs', force: :cascade do |t| - t.string 'key', null: false - t.string 'filename', null: false - t.string 'content_type' - t.text 'metadata' - t.bigint 'byte_size', null: false - t.string 'checksum', null: false - t.datetime 'created_at', null: false - t.index ['key'], name: 'index_active_storage_blobs_on_key', unique: true + create_table "active_storage_blobs", force: :cascade do |t| + t.string "key", null: false + t.string "filename", null: false + t.string "content_type" + t.text "metadata" + t.bigint "byte_size", null: false + t.string "checksum", null: false + t.datetime "created_at", null: false + t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end - create_table 'attachments', id: :serial, force: :cascade do |t| - t.integer 'file_type', default: 0 - t.string 'external_url' - t.float 'coordinates_lat', default: 0.0 - t.float 'coordinates_long', default: 0.0 - t.integer 'message_id', null: false - t.integer 'account_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'fallback_title' - t.string 'extension' + create_table "attachments", id: :serial, force: :cascade do |t| + t.integer "file_type", default: 0 + t.string "external_url" + t.float "coordinates_lat", default: 0.0 + t.float "coordinates_long", default: 0.0 + t.integer "message_id", null: false + t.integer "account_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "fallback_title" + t.string "extension" end - create_table 'canned_responses', id: :serial, force: :cascade do |t| - t.integer 'account_id', null: false - t.string 'short_code' - t.text 'content' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false + create_table "canned_responses", id: :serial, force: :cascade do |t| + t.integer "account_id", null: false + t.string "short_code" + t.text "content" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end - create_table 'channel_facebook_pages', id: :serial, force: :cascade do |t| - t.string 'name', null: false - t.string 'page_id', null: false - t.string 'user_access_token', null: false - t.string 'page_access_token', null: false - t.integer 'account_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.index %w[page_id account_id], name: 'index_channel_facebook_pages_on_page_id_and_account_id', unique: true - t.index ['page_id'], name: 'index_channel_facebook_pages_on_page_id' + create_table "channel_facebook_pages", id: :serial, force: :cascade do |t| + t.string "name", null: false + t.string "page_id", null: false + t.string "user_access_token", null: false + t.string "page_access_token", null: false + t.integer "account_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["page_id", "account_id"], name: "index_channel_facebook_pages_on_page_id_and_account_id", unique: true + t.index ["page_id"], name: "index_channel_facebook_pages_on_page_id" end - create_table 'channel_twitter_profiles', force: :cascade do |t| - t.string 'name' - t.string 'profile_id', null: false - t.string 'twitter_access_token', null: false - t.string 'twitter_access_token_secret', null: false - t.integer 'account_id', null: false - t.datetime 'created_at', precision: 6, null: false - t.datetime 'updated_at', precision: 6, null: false + create_table "channel_twitter_profiles", force: :cascade do |t| + t.string "name" + t.string "profile_id", null: false + t.string "twitter_access_token", null: false + t.string "twitter_access_token_secret", null: false + t.integer "account_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false end - create_table 'channel_web_widgets', id: :serial, force: :cascade do |t| - t.string 'website_name' - t.string 'website_url' - t.integer 'account_id' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'website_token' - t.string 'widget_color', default: '#1f93ff' - t.index ['website_token'], name: 'index_channel_web_widgets_on_website_token', unique: true + create_table "channel_web_widgets", id: :serial, force: :cascade do |t| + t.string "website_name" + t.string "website_url" + t.integer "account_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "website_token" + t.string "widget_color", default: "#1f93ff" + t.index ["website_token"], name: "index_channel_web_widgets_on_website_token", unique: true end - create_table 'contact_inboxes', force: :cascade do |t| - t.bigint 'contact_id' - t.bigint 'inbox_id' - t.string 'source_id', null: false - t.datetime 'created_at', precision: 6, null: false - t.datetime 'updated_at', precision: 6, null: false - t.index ['contact_id'], name: 'index_contact_inboxes_on_contact_id' - t.index %w[inbox_id source_id], name: 'index_contact_inboxes_on_inbox_id_and_source_id', unique: true - t.index ['inbox_id'], name: 'index_contact_inboxes_on_inbox_id' - t.index ['source_id'], name: 'index_contact_inboxes_on_source_id' + create_table "contact_inboxes", force: :cascade do |t| + t.bigint "contact_id" + t.bigint "inbox_id" + t.string "source_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["contact_id"], name: "index_contact_inboxes_on_contact_id" + t.index ["inbox_id", "source_id"], name: "index_contact_inboxes_on_inbox_id_and_source_id", unique: true + t.index ["inbox_id"], name: "index_contact_inboxes_on_inbox_id" + t.index ["source_id"], name: "index_contact_inboxes_on_source_id" end - create_table 'contacts', id: :serial, force: :cascade do |t| - t.string 'name' - t.string 'email' - t.string 'phone_number' - t.integer 'account_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'pubsub_token' - t.jsonb 'additional_attributes' - t.index ['account_id'], name: 'index_contacts_on_account_id' - t.index ['pubsub_token'], name: 'index_contacts_on_pubsub_token', unique: true + create_table "contacts", id: :serial, force: :cascade do |t| + t.string "name" + t.string "email" + t.string "phone_number" + t.integer "account_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "pubsub_token" + t.jsonb "additional_attributes" + t.index ["account_id"], name: "index_contacts_on_account_id" + t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true end - create_table 'conversations', id: :serial, force: :cascade do |t| - t.integer 'account_id', null: false - t.integer 'inbox_id', null: false - t.integer 'status', default: 0, null: false - t.integer 'assignee_id' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.bigint 'contact_id' - t.integer 'display_id', null: false - t.datetime 'user_last_seen_at' - t.datetime 'agent_last_seen_at' - t.boolean 'locked', default: false - t.jsonb 'additional_attributes' - t.bigint 'contact_inbox_id' - t.index %w[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 ['contact_inbox_id'], name: 'index_conversations_on_contact_inbox_id' + create_table "conversations", id: :serial, force: :cascade do |t| + t.integer "account_id", null: false + t.integer "inbox_id", null: false + t.integer "status", default: 0, null: false + t.integer "assignee_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "contact_id" + t.integer "display_id", null: false + t.datetime "user_last_seen_at" + t.datetime "agent_last_seen_at" + t.boolean "locked", default: false + t.jsonb "additional_attributes" + t.bigint "contact_inbox_id" + 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 ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id" end - create_table 'inbox_members', id: :serial, force: :cascade do |t| - t.integer 'user_id', null: false - t.integer 'inbox_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.index ['inbox_id'], name: 'index_inbox_members_on_inbox_id' + create_table "inbox_members", id: :serial, force: :cascade do |t| + t.integer "user_id", null: false + t.integer "inbox_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["inbox_id"], name: "index_inbox_members_on_inbox_id" end - create_table 'inboxes', id: :serial, force: :cascade do |t| - t.integer 'channel_id', null: false - t.integer 'account_id', null: false - t.string 'name', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'channel_type' - t.index ['account_id'], name: 'index_inboxes_on_account_id' + create_table "inboxes", id: :serial, force: :cascade do |t| + t.integer "channel_id", null: false + t.integer "account_id", null: false + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "channel_type" + t.boolean "enable_auto_assignment", default: true + t.index ["account_id"], name: "index_inboxes_on_account_id" end - create_table 'messages', id: :serial, force: :cascade do |t| - t.text 'content' - t.integer 'account_id', null: false - t.integer 'inbox_id', null: false - t.integer 'conversation_id', null: false - t.integer 'message_type', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.boolean 'private', default: false - t.integer 'user_id' - t.integer 'status', default: 0 - t.string 'source_id' - t.integer 'content_type', default: 0 - t.json 'content_attributes', default: {} - t.bigint 'contact_id' - t.index ['contact_id'], name: 'index_messages_on_contact_id' - t.index ['conversation_id'], name: 'index_messages_on_conversation_id' - t.index ['source_id'], name: 'index_messages_on_source_id' + create_table "messages", id: :serial, force: :cascade do |t| + t.text "content" + t.integer "account_id", null: false + t.integer "inbox_id", null: false + t.integer "conversation_id", null: false + t.integer "message_type", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "private", default: false + t.integer "user_id" + t.integer "status", default: 0 + t.string "source_id" + t.integer "content_type", default: 0 + t.json "content_attributes", default: {} + t.bigint "contact_id" + t.index ["contact_id"], name: "index_messages_on_contact_id" + t.index ["conversation_id"], name: "index_messages_on_conversation_id" + t.index ["source_id"], name: "index_messages_on_source_id" end - create_table 'subscriptions', id: :serial, force: :cascade do |t| - t.string 'pricing_version' - t.integer 'account_id' - t.datetime 'expiry' - t.string 'billing_plan', default: 'trial' - t.string 'stripe_customer_id' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.integer 'state', default: 0 - t.boolean 'payment_source_added', default: false + create_table "subscriptions", id: :serial, force: :cascade do |t| + t.string "pricing_version" + t.integer "account_id" + t.datetime "expiry" + t.string "billing_plan", default: "trial" + t.string "stripe_customer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "state", default: 0 + t.boolean "payment_source_added", default: false end - create_table 'taggings', id: :serial, force: :cascade do |t| - t.integer 'tag_id' - t.string 'taggable_type' - t.integer 'taggable_id' - t.string 'tagger_type' - t.integer 'tagger_id' - t.string 'context', limit: 128 - t.datetime 'created_at' - t.index ['context'], name: 'index_taggings_on_context' - t.index %w[tag_id taggable_id taggable_type context tagger_id tagger_type], name: 'taggings_idx', unique: true - t.index ['tag_id'], name: 'index_taggings_on_tag_id' - t.index %w[taggable_id taggable_type context], name: 'index_taggings_on_taggable_id_and_taggable_type_and_context' - t.index %w[taggable_id taggable_type tagger_id context], name: 'taggings_idy' - t.index ['taggable_id'], name: 'index_taggings_on_taggable_id' - t.index %w[taggable_type taggable_id], name: 'index_taggings_on_taggable_type_and_taggable_id' - t.index ['taggable_type'], name: 'index_taggings_on_taggable_type' - t.index %w[tagger_id tagger_type], name: 'index_taggings_on_tagger_id_and_tagger_type' - t.index ['tagger_id'], name: 'index_taggings_on_tagger_id' - t.index %w[tagger_type tagger_id], name: 'index_taggings_on_tagger_type_and_tagger_id' + create_table "taggings", id: :serial, force: :cascade do |t| + t.integer "tag_id" + t.string "taggable_type" + t.integer "taggable_id" + t.string "tagger_type" + t.integer "tagger_id" + t.string "context", limit: 128 + t.datetime "created_at" + t.index ["context"], name: "index_taggings_on_context" + t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true + t.index ["tag_id"], name: "index_taggings_on_tag_id" + t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" + t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy" + t.index ["taggable_id"], name: "index_taggings_on_taggable_id" + t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id" + t.index ["taggable_type"], name: "index_taggings_on_taggable_type" + t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type" + t.index ["tagger_id"], name: "index_taggings_on_tagger_id" + t.index ["tagger_type", "tagger_id"], name: "index_taggings_on_tagger_type_and_tagger_id" end - create_table 'tags', id: :serial, force: :cascade do |t| - t.string 'name' - t.integer 'taggings_count', default: 0 - t.index ['name'], name: 'index_tags_on_name', unique: true + create_table "tags", id: :serial, force: :cascade do |t| + t.string "name" + t.integer "taggings_count", default: 0 + t.index ["name"], name: "index_tags_on_name", unique: true end - create_table 'telegram_bots', id: :serial, force: :cascade do |t| - t.string 'name' - t.string 'auth_key' - t.integer 'account_id' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false + create_table "telegram_bots", id: :serial, force: :cascade do |t| + t.string "name" + t.string "auth_key" + t.integer "account_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end - create_table 'users', id: :serial, force: :cascade do |t| - t.string 'provider', default: 'email', null: false - t.string 'uid', default: '', null: false - t.string 'encrypted_password', default: '', null: false - t.string 'reset_password_token' - t.datetime 'reset_password_sent_at' - t.datetime 'remember_created_at' - t.integer 'sign_in_count', default: 0, null: false - t.datetime 'current_sign_in_at' - t.datetime 'last_sign_in_at' - t.string 'current_sign_in_ip' - t.string 'last_sign_in_ip' - t.string 'confirmation_token' - t.datetime 'confirmed_at' - t.datetime 'confirmation_sent_at' - t.string 'unconfirmed_email' - t.string 'name', null: false - t.string 'nickname' - t.string 'email' - t.json 'tokens' - t.integer 'account_id', null: false - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.string 'pubsub_token' - t.integer 'role', default: 0 - t.bigint 'inviter_id' - t.index ['email'], name: 'index_users_on_email' - t.index ['inviter_id'], name: 'index_users_on_inviter_id' - t.index ['pubsub_token'], name: 'index_users_on_pubsub_token', unique: true - t.index ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true - t.index %w[uid provider], name: 'index_users_on_uid_and_provider', unique: true + create_table "users", id: :serial, force: :cascade do |t| + t.string "provider", default: "email", null: false + t.string "uid", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.integer "sign_in_count", default: 0, null: false + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "unconfirmed_email" + t.string "name", null: false + t.string "nickname" + t.string "email" + t.json "tokens" + t.integer "account_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "pubsub_token" + t.integer "role", default: 0 + t.bigint "inviter_id" + t.index ["email"], name: "index_users_on_email" + t.index ["inviter_id"], name: "index_users_on_inviter_id" + t.index ["pubsub_token"], name: "index_users_on_pubsub_token", unique: true + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true end - create_table 'webhooks', force: :cascade do |t| - t.integer 'account_id' - t.integer 'inbox_id' - t.string 'urls' - t.datetime 'created_at', precision: 6, null: false - t.datetime 'updated_at', precision: 6, null: false + create_table "webhooks", force: :cascade do |t| + t.integer "account_id" + t.integer "inbox_id" + t.string "urls" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false end - add_foreign_key 'active_storage_attachments', 'active_storage_blobs', column: 'blob_id' - add_foreign_key 'contact_inboxes', 'contacts' - add_foreign_key 'contact_inboxes', 'inboxes' - add_foreign_key 'conversations', 'contact_inboxes' - add_foreign_key 'messages', 'contacts' - add_foreign_key 'users', 'users', column: 'inviter_id', on_delete: :nullify + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" + add_foreign_key "contact_inboxes", "contacts" + add_foreign_key "contact_inboxes", "inboxes" + add_foreign_key "conversations", "contact_inboxes" + add_foreign_key "messages", "contacts" + add_foreign_key "users", "users", column: "inviter_id", on_delete: :nullify end diff --git a/spec/controllers/api/v1/inboxes_controller_spec.rb b/spec/controllers/api/v1/inboxes_controller_spec.rb index 8bd72188b..7553f45ff 100644 --- a/spec/controllers/api/v1/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/inboxes_controller_spec.rb @@ -62,7 +62,7 @@ RSpec.describe 'Inboxes API', type: :request do as: :json expect(response).to have_http_status(:success) - expect(Inbox.count).to eq(0) + expect { inbox.reload }.to raise_exception(ActiveRecord::RecordNotFound) end it 'is unable to delete inbox of another account' do @@ -87,4 +87,42 @@ RSpec.describe 'Inboxes API', type: :request do end end end + + describe 'PATCH /api/v1/inboxes/:id' do + let(:inbox) { create(:inbox, account: account) } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + patch "/api/v1/inboxes/#{inbox.id}" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + let(:valid_params) { { inbox: { enable_auto_assignment: false } } } + + it 'updates inbox' do + patch "/api/v1/inboxes/#{inbox.id}", + headers: admin.create_new_auth_token, + params: valid_params, + as: :json + + expect(response).to have_http_status(:success) + expect(inbox.reload.enable_auto_assignment).to be_falsey + end + + it 'will not update inbox for agent' do + agent = create(:user, account: account, role: :agent) + + patch "/api/v1/inboxes/#{inbox.id}", + headers: agent.create_new_auth_token, + params: valid_params, + as: :json + + expect(response).to have_http_status(:unauthorized) + end + end + end end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index e2d44668d..a74b766d7 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -97,6 +97,17 @@ RSpec.describe Conversation, type: :model do # run_round_robin expect(conversation.reload.assignee).to eq(agent) end + + it 'will not auto assign agent if enable_auto_assignment is false' do + inbox.update(enable_auto_assignment: false) + + # send_events + expect(Rails.configuration.dispatcher).to have_received(:dispatch) + .with(described_class::CONVERSATION_CREATED, kind_of(Time), conversation: conversation) + + # run_round_robin + expect(conversation.reload.assignee).to eq(nil) + end end describe '#update_assignee' do