Skip to content
Permalink
Browse files Browse the repository at this point in the history
Maintenance: Sync Article REST API capabilities with the ones of the …
…frontend.
  • Loading branch information
rolfschmidt authored and thorsteneckel committed Nov 16, 2020
1 parent f0462d4 commit 28944de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
17 changes: 12 additions & 5 deletions app/controllers/ticket_articles_controller.rb
Expand Up @@ -101,11 +101,18 @@ def update
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)

Expand Down
12 changes: 10 additions & 2 deletions spec/requests/ticket/article_spec.rb
Expand Up @@ -118,37 +118,45 @@
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
expect(response).to have_http_status(:created)
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

Expand Down
4 changes: 2 additions & 2 deletions spec/requests/ticket_spec.rb
Expand Up @@ -902,7 +902,7 @@
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)
Expand Down Expand Up @@ -991,7 +991,7 @@
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)
Expand Down

0 comments on commit 28944de

Please sign in to comment.