Browse Source

Fixes #1553: Calendar as filter condition for Trigger/Automatization.

Rolf Schmidt 5 years ago
parent
commit
ec23915bd0

+ 2 - 2
.rubocop_todo.yml

@@ -37,7 +37,7 @@ Metrics/BlockNesting:
 
 # Offense count: 340
 Metrics/CyclomaticComplexity:
-  Max: 97
+  Max: 107
 
 # Offense count: 27
 # Configuration parameters: CountComments.
@@ -46,7 +46,7 @@ Metrics/ModuleLength:
 
 # Offense count: 274
 Metrics/PerceivedComplexity:
-  Max: 115
+  Max: 125
 
 # Offense count: 2
 # Cop supports --auto-correct.

+ 34 - 18
app/assets/javascripts/app/controllers/_ui_element/ticket_selector.coffee

@@ -17,6 +17,10 @@ class App.UiElement.ticket_selector
         name: 'Organization'
         model: 'Organization'
 
+    if attribute.executionTime
+      groups.execution_time =
+        name: 'Execution Time'
+
     operators_type =
       '^datetime$': ['before (absolute)', 'after (absolute)', 'before (relative)', 'after (relative)', 'within next (relative)', 'within last (relative)']
       '^timestamp$': ['before (absolute)', 'after (absolute)', 'before (relative)', 'after (relative)', 'within next (relative)', 'within last (relative)']
@@ -71,24 +75,36 @@ class App.UiElement.ticket_selector
         operator: ['is', 'is not']
 
     for groupKey, groupMeta of groups
-      for row in App[groupMeta.model].configure_attributes
-
-        # ignore passwords and relations
-        if row.type isnt 'password' && row.name.substr(row.name.length-4,4) isnt '_ids' && row.searchable isnt false
-          config = _.clone(row)
-          for operatorRegEx, operator of operators_type
-            myRegExp = new RegExp(operatorRegEx, 'i')
-            if config.tag && config.tag.match(myRegExp)
-              config.operator = operator
-            elements["#{groupKey}.#{config.name}"] = config
-          for operatorRegEx, operator of operators_name
-            myRegExp = new RegExp(operatorRegEx, 'i')
-            if config.name && config.name.match(myRegExp)
-              config.operator = operator
-            elements["#{groupKey}.#{config.name}"] = config
-
-          if config.tag == 'select'
-            config.multiple = true
+      if groupKey is 'execution_time'
+        if attribute.executionTime
+          elements['execution_time.calendar_id'] =
+            name: 'calendar_id'
+            display: 'Calendar'
+            tag: 'select'
+            relation: 'Calendar'
+            null: false
+            translate: false
+            operator: ['is in working time', 'is not in working time']
+
+      else
+        for row in App[groupMeta.model].configure_attributes
+
+          # ignore passwords and relations
+          if row.type isnt 'password' && row.name.substr(row.name.length-4,4) isnt '_ids' && row.searchable isnt false
+            config = _.clone(row)
+            for operatorRegEx, operator of operators_type
+              myRegExp = new RegExp(operatorRegEx, 'i')
+              if config.tag && config.tag.match(myRegExp)
+                config.operator = operator
+              elements["#{groupKey}.#{config.name}"] = config
+            for operatorRegEx, operator of operators_name
+              myRegExp = new RegExp(operatorRegEx, 'i')
+              if config.name && config.name.match(myRegExp)
+                config.operator = operator
+              elements["#{groupKey}.#{config.name}"] = config
+
+            if config.tag == 'select'
+              config.multiple = true
 
     if attribute.out_of_office
       elements['ticket.out_of_office_replacement_id'] =

+ 1 - 1
app/assets/javascripts/app/models/job.coffee

@@ -5,7 +5,7 @@ class App.Job extends App.Model
   @configure_attributes = [
     { name: 'name',                 display: 'Name',                            tag: 'input',    type: 'text', limit: 100, null: false },
     { name: 'timeplan',             display: 'When should the job run?',        tag: 'timer', null: true },
-    { name: 'condition',            display: 'Conditions for effected objects', tag: 'ticket_selector', null: true },
+    { name: 'condition',            display: 'Conditions for effected objects', tag: 'ticket_selector', null: true, executionTime: true },
     { name: 'perform',              display: 'Execute changes on objects',      tag: 'ticket_perform_action', null: true, notification: true, ticket_delete: true },
     { name: 'disable_notification', display: 'Disable Notifications',           tag: 'boolean', default: true },
     { name: 'note',                 display: 'Note',                            tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, null: true },

+ 1 - 1
app/assets/javascripts/app/models/trigger.coffee

@@ -4,7 +4,7 @@ class App.Trigger extends App.Model
   @url: @apiPath + '/triggers'
   @configure_attributes = [
     { name: 'name',       display: 'Name',          tag: 'input',     type: 'text', limit: 100, null: false },
-    { name: 'condition',  display: 'Conditions for effected objects', tag: 'ticket_selector', null: false, preview: false, action: true, hasChanged: true },
+    { name: 'condition',  display: 'Conditions for effected objects', tag: 'ticket_selector', null: false, preview: false, action: true, hasChanged: true, executionTime: true },
     { name: 'perform',    display: 'Execute changes on objects',      tag: 'ticket_perform_action', null: true, notification: true, trigger: true },
     { name: 'active',     display: 'Active',        tag: 'active',    default: true },
     { name: 'updated_at', display: 'Updated',       tag: 'datetime',  readonly: 1 },

+ 1 - 1
app/controllers/tickets_controller.rb

@@ -498,7 +498,7 @@ class TicketsController < ApplicationController
   def selector
     permission_check('admin.*')
 
-    ticket_count, tickets = Ticket.selectors(params[:condition], limit: 6)
+    ticket_count, tickets = Ticket.selectors(params[:condition], limit: 6, execution_time: true)
 
     assets = {}
     ticket_ids = []

+ 1 - 0
app/models/application_model/can_assets.rb

@@ -68,6 +68,7 @@ get assets and record_ids of selector
         attribute_class = attribute[0].to_classname.constantize
       rescue => e
         next if attribute[0] == 'article'
+        next if attribute[0] == 'execution_time'
 
         logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
         next

+ 15 - 0
app/models/calendar.rb

@@ -327,6 +327,21 @@ returns
     holidays
   end
 
+  def biz
+    Biz::Schedule.new do |config|
+
+      # get business hours
+      hours = business_hours_to_hash
+      raise "No configured hours found in calendar #{inspect}" if hours.blank?
+
+      config.hours = hours
+
+      # get holidays
+      config.holidays = public_holidays_to_array
+      config.time_zone = timezone
+    end
+  end
+
   private
 
   # if changed calendar is default, set all others default to false

+ 1 - 0
app/models/concerns/checks_condition_validation.rb

@@ -13,6 +13,7 @@ module ChecksConditionValidation
 
     # check if a valid condition got inserted.
     validate_condition.delete('ticket.action')
+    validate_condition.delete('execution_time.calendar_id')
     validate_condition.each do |key, value|
       next if !value
       next if !value['operator']

+ 2 - 2
app/models/job.rb

@@ -74,7 +74,7 @@ job.run(true)
         return
       end
 
-      ticket_count, tickets = Ticket.selectors(condition, limit: 2_000)
+      ticket_count, tickets = Ticket.selectors(condition, limit: 2_000, execution_time: true)
 
       logger.debug { "Job #{name} with #{ticket_count} tickets" }
 
@@ -140,7 +140,7 @@ job.run(true)
   end
 
   def matching_count
-    ticket_count, _tickets = Ticket.selectors(condition, limit: 1)
+    ticket_count, _tickets = Ticket.selectors(condition, limit: 1, execution_time: true)
     ticket_count || 0
   end
 

+ 7 - 6
app/models/job/assets.rb

@@ -23,21 +23,22 @@ returns
 =end
 
     def assets(data)
-
       app_model = Job.to_app_model
 
-      if !data[ app_model ]
-        data[ app_model ] = {}
-      end
+      data[ app_model ] ||= {}
       return data if data[ app_model ][ id ]
 
       data[ app_model ][ id ] = attributes_with_association_ids
       data = assets_of_selector('condition', data)
       data = assets_of_selector('perform', data)
 
-      if !data[ User.to_app_model ]
-        data[ User.to_app_model ] = {}
+      app_model_calendar = Calendar.to_app_model
+      data[ app_model_calendar ] ||= {}
+      Calendar.find_each do |calendar|
+        data = calendar.assets(data)
       end
+
+      data[ User.to_app_model ] ||= {}
       %w[created_by_id updated_by_id].each do |local_user_id|
         next if !self[ local_user_id ]
         next if data[ User.to_app_model ][ self[ local_user_id ] ]

Some files were not shown because too many files changed in this diff