core_workflow.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class CoreWorkflow < ApplicationModel
  3. include ChecksClientNotification
  4. include CoreWorkflow::Assets
  5. include ChecksCoreWorkflow
  6. core_workflow_screens 'create', 'edit'
  7. default_scope { order(:priority, :id) }
  8. scope :active, -> { where(active: true) }
  9. scope :changeable, -> { where(changeable: true) }
  10. scope :object, ->(object) { where(object: [object, nil]) }
  11. store :preferences
  12. store :condition_saved
  13. store :condition_selected
  14. store :perform
  15. validates :name, presence: true, uniqueness: { case_sensitive: false }
  16. def self.classes
  17. Models.all.keys.select { |m| m.included_modules.include?(ChecksCoreWorkflow) }
  18. end
  19. def self.config
  20. classes.each_with_object({ configuration: {}, execution: {} }) do |config_class, result|
  21. if config_class.try(:core_workflow_screens).present?
  22. result[:execution][config_class.to_s] = config_class.try(:core_workflow_screens)
  23. end
  24. if config_class.try(:core_workflow_admin_screens).present?
  25. result[:configuration][config_class.to_s] = config_class.try(:core_workflow_admin_screens)
  26. end
  27. end
  28. end
  29. =begin
  30. Runs the core workflow engine based on the current state of the object.
  31. perform_result = CoreWorkflow.perform(payload: {
  32. 'event' => 'core_workflow',
  33. 'request_id' => 'ChecksCoreWorkflow.validate_workflows',
  34. 'class_name' => 'Ticket',
  35. 'screen' => 'edit',
  36. 'params' => Ticket.first.attributes,
  37. }, user: User.find(3), assets: false)
  38. =end
  39. def self.perform(payload:, user:, assets: {}, assets_in_result: true, result: {}, form_updater: false)
  40. CoreWorkflow::Result.new(payload: payload, user: user, assets: assets, assets_in_result: assets_in_result, result: result, form_updater: form_updater).run
  41. rescue => e
  42. return {} if e.is_a?(ArgumentError)
  43. raise e if !Rails.env.production?
  44. Rails.logger.error 'Error performing Core Workflow engine.'
  45. Rails.logger.error e
  46. {}
  47. end
  48. =begin
  49. Checks if the object matches a specific condition.
  50. CoreWorkflow.matches_selector?(
  51. id: Ticket.first.id,
  52. user: User.find(3),
  53. selector: {"ticket.state_id"=>{"operator"=>"is", "value"=>["4", "5", "1", "2", "7", "3"]}}
  54. )
  55. =end
  56. def self.matches_selector?(id:, user:, selector:, class_name: 'Ticket', params: {}, screen: 'edit', request_id: 'ChecksCoreWorkflow.validate_workflows', event: 'core_workflow', check: 'saved')
  57. if id.present?
  58. params['id'] = id
  59. end
  60. CoreWorkflow::Result.new(payload: {
  61. 'event' => event,
  62. 'request_id' => request_id,
  63. 'class_name' => class_name,
  64. 'screen' => screen,
  65. 'params' => params,
  66. }, user: user, assets: false, assets_in_result: false).matches_selector?(selector: selector, check: check)
  67. rescue => e
  68. return {} if e.is_a?(ArgumentError)
  69. raise e if !Rails.env.production?
  70. Rails.logger.error 'Error performing Core Workflow engine.'
  71. Rails.logger.error e
  72. false
  73. end
  74. end