From 28944de180a88698509a656f61558bf9d7f810f4 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Thu, 8 Oct 2020 09:19:37 +0200 Subject: [PATCH] Maintenance: Sync Article REST API capabilities with the ones of the frontend. --- app/controllers/ticket_articles_controller.rb | 17 ++++++++++++----- spec/requests/ticket/article_spec.rb | 12 ++++++++++-- spec/requests/ticket_spec.rb | 4 ++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/controllers/ticket_articles_controller.rb b/app/controllers/ticket_articles_controller.rb index 642d11771..ad2a16536 100644 --- a/app/controllers/ticket_articles_controller.rb +++ b/app/controllers/ticket_articles_controller.rb @@ -101,11 +101,18 @@ class TicketArticlesController < ApplicationController article = Ticket::Article.find(params[:id]) authorize!(article) - clean_params = Ticket::Article.association_name_to_id_convert(params) - clean_params = Ticket::Article.param_cleanup(clean_params, true) - - # only apply preferences changes (keep not updated keys/values) - clean_params = article.param_preferences_merge(clean_params) + # only update internal and highlight info + clean_params = {} + if !params[:internal].nil? + clean_params[:internal] = params[:internal] + end + if params.dig(:preferences, :highlight).present? + clean_params = article.param_preferences_merge(clean_params.merge( + preferences: { + highlight: params[:preferences][:highlight].to_s + } + )) + end article.update!(clean_params) diff --git a/spec/requests/ticket/article_spec.rb b/spec/requests/ticket/article_spec.rb index 531d5c9b4..8f98c4552 100644 --- a/spec/requests/ticket/article_spec.rb +++ b/spec/requests/ticket/article_spec.rb @@ -118,8 +118,10 @@ AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO content_type: 'text/plain', body: 'some body', type: 'note', + internal: false, preferences: { some_key1: 123, + highlight: '123', }, } post '/api/v1/ticket_articles', params: params, as: :json @@ -127,28 +129,34 @@ AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO expect(json_response).to be_a_kind_of(Hash) expect(json_response['subject']).to be_nil expect(json_response['body']).to eq('some body') + expect(json_response['internal']).to eq(false) expect(json_response['content_type']).to eq('text/plain') expect(json_response['updated_by_id']).to eq(agent.id) expect(json_response['created_by_id']).to eq(agent.id) expect(json_response['preferences']['some_key1']).to eq(123) + expect(json_response['preferences']['highlight']).to eq('123') expect(ticket.articles.count).to eq(5) params = { body: 'some body 2', + internal: true, preferences: { some_key2: 'abc', + highlight: '234', }, } put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json expect(response).to have_http_status(:ok) expect(json_response).to be_a_kind_of(Hash) expect(json_response['subject']).to be_nil - expect(json_response['body']).to eq('some body 2') + expect(json_response['body']).not_to eq('some body 2') + expect(json_response['internal']).to eq(true) expect(json_response['content_type']).to eq('text/plain') expect(json_response['updated_by_id']).to eq(agent.id) expect(json_response['created_by_id']).to eq(agent.id) expect(json_response['preferences']['some_key1']).to eq(123) - expect(json_response['preferences']['some_key2']).to eq('abc') + expect(json_response['preferences']['some_key2']).not_to eq('abc') + expect(json_response['preferences']['highlight']).to eq('234') end diff --git a/spec/requests/ticket_spec.rb b/spec/requests/ticket_spec.rb index ff36205ca..209ce996f 100644 --- a/spec/requests/ticket_spec.rb +++ b/spec/requests/ticket_spec.rb @@ -902,7 +902,7 @@ RSpec.describe 'Ticket', type: :request do expect(json_response).to be_a_kind_of(Hash) expect(json_response['ticket_id']).to eq(ticket.id) expect(json_response['from']).to eq(%("Tickets Agent via #{ticket_group.email_address.realname}" <#{ticket_group.email_address.email}>)) - expect(json_response['subject']).to eq('new subject') + expect(json_response['subject']).not_to eq('new subject') expect(json_response['body']).to eq('some body') expect(json_response['content_type']).to eq('text/plain') expect(json_response['internal']).to eq(true) @@ -991,7 +991,7 @@ RSpec.describe 'Ticket', type: :request do expect(json_response).to be_a_kind_of(Hash) expect(json_response['ticket_id']).to eq(ticket.id) expect(json_response['from']).to eq('Tickets Admin') - expect(json_response['subject']).to eq('new subject') + expect(json_response['subject']).not_to eq('new subject') expect(json_response['body']).to eq('some body') expect(json_response['content_type']).to eq('text/plain') expect(json_response['internal']).to eq(true)