core_workflow.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Match saved data:
  51. CoreWorkflow.matches_selector?(
  52. id: Ticket.first.id,
  53. user: User.find_by(login: 'admin@example.com'),
  54. selector: { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => ['2'] } },
  55. )
  56. Match payload selected data:
  57. CoreWorkflow.matches_selector?(
  58. check: 'selected',
  59. user: User.find_by(login: 'admin@example.com'),
  60. params: {
  61. 'group_id' => '1',
  62. 'owner_id' => '',
  63. 'state_id' => '2',
  64. 'priority_id' => '2',
  65. 'article' => {
  66. 'body' => '',
  67. 'type' => 'note',
  68. 'internal' => true,
  69. 'form_id' => 'd8416050-0987-4ae4-b36f-c488b3b9b333',
  70. 'shared_draft_id' => '',
  71. 'subtype' => '',
  72. 'in_reply_to' => '',
  73. 'to' => '',
  74. 'cc' => '',
  75. 'subject' => ''
  76. },
  77. },
  78. selector: { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => ['2'] } },
  79. )
  80. =end
  81. def self.matches_selector?(user:, selector:, id: nil, class_name: 'Ticket', params: {}, screen: 'edit', request_id: 'ChecksCoreWorkflow.validate_workflows', event: 'core_workflow', check: 'saved')
  82. if id.present?
  83. params['id'] = id
  84. end
  85. CoreWorkflow::Result.new(payload: {
  86. 'event' => event,
  87. 'request_id' => request_id,
  88. 'class_name' => class_name,
  89. 'screen' => screen,
  90. 'params' => params,
  91. }, user: user, assets: false, assets_in_result: false).matches_selector?(selector: selector, check: check)
  92. rescue => e
  93. return {} if e.is_a?(ArgumentError)
  94. raise e if !Rails.env.production?
  95. Rails.logger.error 'Error performing Core Workflow engine.'
  96. Rails.logger.error e
  97. false
  98. end
  99. end