Browse Source

Fixes #1103 - 'time_unit' attribute is not propagated over ticket articles API.

Co-authored-by: Florian Liebe <fl@zammad.com>
Co-authored-by: Tobias Schäfer <ts@zammad.com>
Florian Liebe 1 year ago
parent
commit
99c1694015
2 changed files with 41 additions and 0 deletions
  1. 6 0
      app/models/ticket/article.rb
  2. 35 0
      spec/requests/ticket/article/time_accounting_spec.rb

+ 6 - 0
app/models/ticket/article.rb

@@ -284,6 +284,7 @@ returns
   def attributes_with_association_names(empty_keys: false)
     attributes = super
     add_attachments_to_attributes(attributes)
+    add_time_unit_to_attributes(attributes)
     Ticket::Article.insert_urls(attributes)
   end
 
@@ -318,6 +319,11 @@ returns
     attributes
   end
 
+  def add_time_unit_to_attributes(attributes)
+    attributes['time_unit'] = ticket_time_accounting&.time_unit.presence || nil
+    attributes
+  end
+
   # strip not wanted chars
   def check_subject
     return true if subject.blank?

+ 35 - 0
spec/requests/ticket/article/time_accounting_spec.rb

@@ -0,0 +1,35 @@
+# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe 'Ticket::Article API > Time Accounting', :aggregate_failures, type: :request do
+  describe 'GET /api/v1/ticket_articles' do
+    let(:agent)          { create(:agent, groups: Group.all) }
+    let(:ticket)         { create(:ticket) }
+    let(:article)        { create(:ticket_article, ticket: ticket) }
+    let(:accounted_time) { create(:ticket_time_accounting, ticket: ticket, ticket_article: article, time_unit: 42) }
+
+    before do
+      article && accounted_time
+
+      authenticated_as(agent)
+      get "/api/v1/ticket_articles/#{article.id}?expand=true"
+    end
+
+    context 'when no time was accounted' do
+      let(:accounted_time) { nil }
+
+      it 'returns nil' do
+        expect(response).to have_http_status(:ok)
+        expect(json_response['time_unit']).to be_nil
+      end
+    end
+
+    context 'when time was accounted' do
+      it 'returns the accounted time' do
+        expect(response).to have_http_status(:ok)
+        expect(json_response['time_unit']).to eq(accounted_time.time_unit.to_s)
+      end
+    end
+  end
+end