diff --git a/app/controllers/api/service_dialogs_controller.rb b/app/controllers/api/service_dialogs_controller.rb index d3da77feb4c..d17e64286ca 100644 --- a/app/controllers/api/service_dialogs_controller.rb +++ b/app/controllers/api/service_dialogs_controller.rb @@ -26,8 +26,9 @@ def create_resource(_type, _id, data) def edit_resource(type, id, data) service_dialog = resource_search(id, type, Dialog) begin - service_dialog.update!(data.except('content')) - service_dialog.update_tabs(data['content']['dialog_tabs']) if data['content'] + $api_log.warn("Both 'dialog_tabs':[...] and 'content':{'dialog_tabs':[...]} were specified. 'content':{'dialog_tabs':[...]} will be ignored.") if data.key?('dialog_tabs') && data['content'].try(:key?, 'dialog_tabs') + service_dialog.update_tabs(data['dialog_tabs'] || data['content']['dialog_tabs']) if data['dialog_tabs'] || data['content'] + service_dialog.update!(data.except('dialog_tabs', 'content')) rescue => err raise BadRequestError, "Failed to update service dialog - #{err}" end diff --git a/spec/requests/api/service_dialogs_spec.rb b/spec/requests/api/service_dialogs_spec.rb index 816272ae5e1..0e8ef6722b9 100644 --- a/spec/requests/api/service_dialogs_spec.rb +++ b/spec/requests/api/service_dialogs_spec.rb @@ -154,6 +154,84 @@ expect(response.parsed_body).to include(expected) end + context 'using call with :content key' do + it 'POST /api/service_dialogs/:id updates a service dialog' do + api_basic_authorize collection_action_identifier(:service_dialogs, :edit) + dialog_tab = dialog.dialog_tabs.first + dialog_group = dialog_tab.dialog_groups.first + dialog_field = dialog_group.dialog_fields.first + + updated_dialog = { + 'label' => 'updated label', + 'content' => { + 'dialog_tabs' => [ + 'id' => dialog_tab.id.to_s, + 'label' => 'updated tab label', + 'dialog_groups' => [ + { + 'id' => dialog_group.id.to_s, + 'dialog_fields' => [ + { 'id' => dialog_field.id.to_s } + ] + } + ] + ] + } + } + + expected = { + 'href' => a_string_including(service_dialogs_url(dialog.id)), + 'id' => dialog.id, + 'label' => 'updated label' + } + + expect do + run_post(service_dialogs_url(dialog.id), gen_request(:edit, updated_dialog)) + dialog.reload + end.to change(dialog, :content) + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + end + + context 'using call without :content key' do + it 'POST /api/service_dialogs/:id updates a service dialog' do + api_basic_authorize collection_action_identifier(:service_dialogs, :edit) + dialog_tab = dialog.dialog_tabs.first + dialog_group = dialog_tab.dialog_groups.first + dialog_field = dialog_group.dialog_fields.first + + updated_dialog = { + 'label' => 'updated label', + 'dialog_tabs' => [ + 'id' => dialog_tab.id.to_s, + 'label' => 'updated tab label', + 'dialog_groups' => [ + { + 'id' => dialog_group.id.to_s, + 'dialog_fields' => [ + { 'id' => dialog_field.id.to_s } + ] + } + ] + ] + } + + expected = { + 'href' => a_string_including(service_dialogs_url(dialog.id)), + 'id' => dialog.id, + 'label' => 'updated label' + } + + expect do + run_post(service_dialogs_url(dialog.id), gen_request(:edit, updated_dialog)) + dialog.reload + end.to change(dialog, :content) + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + end + it 'POST /api/service_dialogs updates multiple service dialog' do dialog2 = FactoryGirl.create(:dialog_with_tab_and_group_and_field)