20210128131507_init_core_workflow.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class InitCoreWorkflow < ActiveRecord::Migration[5.2]
  3. def change
  4. return if !Setting.exists?(name: 'system_init_done')
  5. add_table
  6. add_setting_ajax
  7. fix_invalid_screens
  8. fix_pending_time
  9. fix_organization_screens
  10. fix_user_screens
  11. add_workflows
  12. end
  13. def add_table # rubocop:disable Metrics/AbcSize
  14. create_table :core_workflows do |t|
  15. t.string :name, limit: 100, null: false
  16. t.string :object, limit: 100, null: true
  17. t.text :preferences, limit: 500.kilobytes + 1, null: true
  18. t.text :condition_saved, limit: 500.kilobytes + 1, null: true
  19. t.text :condition_selected, limit: 500.kilobytes + 1, null: true
  20. t.text :perform, limit: 500.kilobytes + 1, null: true
  21. t.boolean :active, null: false, default: true
  22. t.boolean :stop_after_match, null: false, default: false
  23. t.boolean :changeable, null: false, default: true
  24. t.integer :priority, null: false, default: 0
  25. t.integer :updated_by_id, null: false
  26. t.integer :created_by_id, null: false
  27. t.timestamps limit: 3, null: false
  28. end
  29. add_index :core_workflows, [:name], unique: true
  30. add_foreign_key :core_workflows, :users, column: :created_by_id
  31. add_foreign_key :core_workflows, :users, column: :updated_by_id
  32. end
  33. def add_setting_ajax
  34. Setting.create_if_not_exists(
  35. title: 'Core Workflow Ajax Mode',
  36. name: 'core_workflow_ajax_mode',
  37. area: 'System::UI',
  38. description: 'Defines if the core workflow communication should run over AJAX instead of websockets.',
  39. options: {
  40. form: [
  41. {
  42. display: '',
  43. null: true,
  44. name: 'core_workflow_ajax_mode',
  45. tag: 'boolean',
  46. options: {
  47. true => 'yes',
  48. false => 'no',
  49. },
  50. },
  51. ],
  52. },
  53. state: false,
  54. preferences: {
  55. prio: 3,
  56. permission: ['admin.system'],
  57. },
  58. frontend: true
  59. )
  60. end
  61. def fix_invalid_screens
  62. ObjectManager::Attribute.where(object_lookup_id: [ ObjectLookup.by_name('User'), ObjectLookup.by_name('Organization') ], editable: false).each do |attribute|
  63. next if attribute.screens[:edit].blank?
  64. next if attribute.screens[:create].present?
  65. attribute.screens[:create] = attribute.screens[:edit]
  66. attribute.save
  67. end
  68. end
  69. def fix_pending_time
  70. pending_time = ObjectManager::Attribute.find_by(name: 'pending_time', object_lookup: ObjectLookup.find_by(name: 'Ticket'))
  71. return if pending_time.blank?
  72. pending_time.data_option.delete('required_if')
  73. pending_time.data_option.delete('shown_if')
  74. pending_time.save
  75. end
  76. def fix_organization_screens
  77. %w[domain note].each do |name|
  78. field = ObjectManager::Attribute.find_by(name: name, object_lookup: ObjectLookup.find_by(name: 'Organization'))
  79. next if field.blank?
  80. field.screens['create'] ||= {}
  81. field.screens['create']['-all-'] ||= {}
  82. field.screens['create']['-all-']['null'] = true
  83. field.save
  84. end
  85. end
  86. def fix_user_screens
  87. %w[email web phone mobile organization_id fax department street zip city country address password vip note role_ids].each do |name|
  88. field = ObjectManager::Attribute.find_by(name: name, object_lookup: ObjectLookup.find_by(name: 'User'))
  89. next if field.blank?
  90. field.screens['create'] ||= {}
  91. field.screens['create']['-all-'] ||= {}
  92. field.screens['create']['-all-']['null'] = true
  93. field.save
  94. end
  95. end
  96. def add_workflows
  97. CoreWorkflow.create_if_not_exists(
  98. name: 'base - hide pending time on non pending states',
  99. object: 'Ticket',
  100. condition_saved: {
  101. 'custom.module': {
  102. operator: 'match all modules',
  103. value: [
  104. 'CoreWorkflow::Custom::PendingTime',
  105. ],
  106. },
  107. },
  108. perform: {
  109. 'custom.module': {
  110. execute: ['CoreWorkflow::Custom::PendingTime']
  111. },
  112. },
  113. changeable: false,
  114. created_by_id: 1,
  115. updated_by_id: 1,
  116. )
  117. CoreWorkflow.create_if_not_exists(
  118. name: 'base - admin sla options',
  119. object: 'Sla',
  120. condition_saved: {
  121. 'custom.module': {
  122. operator: 'match all modules',
  123. value: [
  124. 'CoreWorkflow::Custom::AdminSla',
  125. ],
  126. },
  127. },
  128. perform: {
  129. 'custom.module': {
  130. execute: ['CoreWorkflow::Custom::AdminSla']
  131. },
  132. },
  133. changeable: false,
  134. created_by_id: 1,
  135. updated_by_id: 1,
  136. )
  137. CoreWorkflow.create_if_not_exists(
  138. name: 'base - core workflow',
  139. object: 'CoreWorkflow',
  140. condition_saved: {
  141. 'custom.module': {
  142. operator: 'match all modules',
  143. value: [
  144. 'CoreWorkflow::Custom::AdminCoreWorkflow',
  145. ],
  146. },
  147. },
  148. perform: {
  149. 'custom.module': {
  150. execute: ['CoreWorkflow::Custom::AdminCoreWorkflow']
  151. },
  152. },
  153. changeable: false,
  154. created_by_id: 1,
  155. updated_by_id: 1,
  156. )
  157. end
  158. end