Browse Source

Fixes #4750 - New ticket organization condition in core workflow not working for is specific usage.

Co-authored-by: Dominik Klein <dk@zammad.com>
Rolf Schmidt 1 year ago
parent
commit
e0ff740d4d
2 changed files with 53 additions and 8 deletions
  1. 20 8
      app/models/ticket.rb
  2. 33 0
      spec/models/core_workflow/conditions_spec.rb

+ 20 - 8
app/models/ticket.rb

@@ -30,7 +30,8 @@ class Ticket < ApplicationModel
   include ::Ticket::Search
   include ::Ticket::MergeHistory
 
-  store          :preferences
+  store :preferences
+  after_initialize :check_defaults, if: :new_record?
   before_create  :check_generate, :check_defaults, :check_title, :set_default_state, :set_default_priority
   before_update  :check_defaults, :check_title, :reset_pending_time, :check_owner_active
 
@@ -1026,17 +1027,28 @@ returns a hex color code
   end
 
   def check_defaults
-    if !owner_id
-      self.owner_id = 1
-    end
-    return true if !customer_id
+    check_default_owner
+    check_default_organization
+    true
+  end
+
+  def check_default_owner
+    return if !has_attribute?(:owner_id)
+    return if owner_id || owner
+
+    self.owner_id = 1
+  end
+
+  def check_default_organization
+    return if !has_attribute?(:organization_id)
+    return if !customer_id
 
     customer = User.find_by(id: customer_id)
-    return true if !customer
-    return true if organization_id.present? && customer.organization_id?(organization_id)
+    return if !customer
+    return if organization_id.present? && customer.organization_id?(organization_id)
+    return if organization.present? && customer.organization_id?(organization.id)
 
     self.organization_id = customer.organization_id
-    true
   end
 
   def reset_pending_time

+ 33 - 0
spec/models/core_workflow/conditions_spec.rb

@@ -1794,4 +1794,37 @@ RSpec.describe 'CoreWorkflow > Conditions', mariadb: true, type: :model do
       end
     end
   end
+
+  describe 'New ticket organization condition in core workflow not working for is specific usage #4750' do
+    let(:ticket_customer) { create(:customer, :with_org) }
+
+    let!(:workflow) do
+      create(:core_workflow,
+             object:             'Ticket',
+             condition_selected: {
+               'ticket.organization_id': {
+                 operator: 'is',
+                 value:    [ticket.customer.organization_id.to_s],
+               },
+             })
+    end
+
+    context 'when agent' do
+      let(:payload) do
+        base_payload.merge('params' => { 'customer_id' => ticket.customer_id })
+      end
+
+      it 'does match' do
+        expect(result[:matched_workflows]).to include(workflow.id)
+      end
+    end
+
+    context 'when customer' do
+      let!(:action_user) { ticket.customer } # rubocop:disable RSpec/LetSetup
+
+      it 'does match' do
+        expect(result[:matched_workflows]).to include(workflow.id)
+      end
+    end
+  end
 end