core_workflow.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class CoreWorkflow < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksCoreWorkflow
  5. include HasSearchIndexBackend
  6. include CanSelector
  7. include CanSearch
  8. include CoreWorkflow::Assets
  9. include CoreWorkflow::Search
  10. core_workflow_screens 'create', 'edit'
  11. default_scope { order(:priority, :id) }
  12. scope :active, -> { where(active: true) }
  13. scope :changeable, -> { where(changeable: true) }
  14. scope :object, ->(object) { where(object: [object, nil]) }
  15. store :preferences
  16. store :condition_saved
  17. store :condition_selected
  18. store :perform
  19. validates :name, presence: true, uniqueness: { case_sensitive: false }
  20. def self.classes
  21. Models.all.keys.select { |m| m.included_modules.include?(ChecksCoreWorkflow) }
  22. end
  23. def self.config
  24. classes.each_with_object({ configuration: {}, execution: {} }) do |config_class, result|
  25. if config_class.try(:core_workflow_screens).present?
  26. result[:execution][config_class.to_s] = config_class.try(:core_workflow_screens)
  27. end
  28. if config_class.try(:core_workflow_admin_screens).present?
  29. result[:configuration][config_class.to_s] = config_class.try(:core_workflow_admin_screens)
  30. end
  31. end
  32. end
  33. =begin
  34. Runs the core workflow engine based on the current state of the object.
  35. perform_result = CoreWorkflow.perform(payload: {
  36. 'event' => 'core_workflow',
  37. 'request_id' => 'ChecksCoreWorkflow.validate_workflows',
  38. 'class_name' => 'Ticket',
  39. 'screen' => 'edit',
  40. 'params' => Ticket.first.attributes,
  41. }, user: User.find(3), assets: false)
  42. =end
  43. def self.perform(payload:, user:, assets: {}, assets_in_result: true, result: {}, form_updater: false)
  44. CoreWorkflow::Result.new(payload: payload, user: user, assets: assets, assets_in_result: assets_in_result, result: result, form_updater: form_updater).run
  45. rescue => e
  46. return {} if e.is_a?(ArgumentError)
  47. raise e if !Rails.env.production?
  48. Rails.logger.error 'Error performing Core Workflow engine.'
  49. Rails.logger.error e
  50. {}
  51. end
  52. =begin
  53. Checks if the object matches a specific condition.
  54. Match saved data:
  55. CoreWorkflow.matches_selector?(
  56. id: Ticket.first.id,
  57. user: User.find_by(login: 'admin@example.com'),
  58. selector: { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => ['2'] } },
  59. )
  60. Match payload selected data:
  61. CoreWorkflow.matches_selector?(
  62. check: 'selected',
  63. user: User.find_by(login: 'admin@example.com'),
  64. params: {
  65. 'group_id' => '1',
  66. 'owner_id' => '',
  67. 'state_id' => '2',
  68. 'priority_id' => '2',
  69. 'article' => {
  70. 'body' => '',
  71. 'type' => 'note',
  72. 'internal' => true,
  73. 'form_id' => 'd8416050-0987-4ae4-b36f-c488b3b9b333',
  74. 'shared_draft_id' => '',
  75. 'subtype' => '',
  76. 'in_reply_to' => '',
  77. 'to' => '',
  78. 'cc' => '',
  79. 'subject' => ''
  80. },
  81. },
  82. selector: { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => ['2'] } },
  83. )
  84. =end
  85. def self.matches_selector?(user:, selector:, id: nil, class_name: 'Ticket', params: {}, screen: 'edit', request_id: 'ChecksCoreWorkflow.validate_workflows', event: 'core_workflow', check: 'saved')
  86. if id.present?
  87. params['id'] = id
  88. end
  89. CoreWorkflow::Result.new(payload: {
  90. 'event' => event,
  91. 'request_id' => request_id,
  92. 'class_name' => class_name,
  93. 'screen' => screen,
  94. 'params' => params,
  95. }, user: user, assets: false, assets_in_result: false).matches_selector?(selector: selector, check: check)
  96. rescue => e
  97. return {} if e.is_a?(ArgumentError)
  98. raise e if !Rails.env.production?
  99. Rails.logger.error 'Error performing Core Workflow engine.'
  100. Rails.logger.error e
  101. false
  102. end
  103. end