mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-11-09 16:01:30 +08:00
Add automation observers for the task [SCI-11930]
This commit is contained in:
parent
f4bf6fcf6d
commit
0f5cf347ef
14 changed files with 159 additions and 2 deletions
|
|
@ -8,6 +8,7 @@ class Asset < ApplicationRecord
|
|||
include ActiveStorageConcerns
|
||||
include ActiveStorageHelper
|
||||
include VersionedAttachments
|
||||
include ObservableModel
|
||||
|
||||
require 'tempfile'
|
||||
# Lock duration set to 30 minutes
|
||||
|
|
@ -475,4 +476,8 @@ class Asset < ApplicationRecord
|
|||
def reset_file_processing
|
||||
self.file_processing = false
|
||||
end
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(step, last_modified_by || created_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
class Checklist < ApplicationRecord
|
||||
include SearchableModel
|
||||
include ObservableModel
|
||||
|
||||
auto_strip_attributes :name, nullify: false
|
||||
validates :name,
|
||||
|
|
@ -57,4 +58,10 @@ class Checklist < ApplicationRecord
|
|||
new_checklist
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(step, last_modified_by || created_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
class ChecklistItem < ApplicationRecord
|
||||
include ObservableModel
|
||||
|
||||
attr_accessor :with_paragraphs
|
||||
|
||||
|
|
@ -22,6 +23,7 @@ class ChecklistItem < ApplicationRecord
|
|||
class_name: 'User',
|
||||
optional: true
|
||||
|
||||
after_create :run_observers
|
||||
# conditional touch excluding checked updates
|
||||
after_destroy :touch_checklist
|
||||
after_save :touch_checklist
|
||||
|
|
@ -76,4 +78,8 @@ class ChecklistItem < ApplicationRecord
|
|||
checklist.touch
|
||||
# rubocop:enable Rails/SkipsModelValidations
|
||||
end
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(checklist.step, last_modified_by || created_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class FormFieldValue < ApplicationRecord
|
||||
include ObservableModel
|
||||
|
||||
belongs_to :form_response
|
||||
belongs_to :form_field
|
||||
belongs_to :created_by, class_name: 'User'
|
||||
|
|
@ -44,4 +46,8 @@ class FormFieldValue < ApplicationRecord
|
|||
|
||||
errors.add(:value, :not_unique_latest)
|
||||
end
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(form_response.step, created_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ class Result < ApplicationRecord
|
|||
CleanupUserSettingsJob.perform_later('result_states', id)
|
||||
end
|
||||
|
||||
after_create :run_observers
|
||||
|
||||
def self.search(user,
|
||||
include_archived,
|
||||
query = nil,
|
||||
|
|
@ -196,4 +198,10 @@ class Result < ApplicationRecord
|
|||
def delete_step_results
|
||||
step_results.destroy_all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ResultCreateAutomationObserver.new(my_module, user).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ class Step < ApplicationRecord
|
|||
|
||||
before_validation :set_completed_on, if: :completed_changed?
|
||||
before_save :set_last_modified_by
|
||||
after_create :run_observers
|
||||
before_destroy :cascade_before_destroy
|
||||
after_destroy :adjust_positions_after_destroy, unless: -> { skip_position_adjust }
|
||||
|
||||
|
|
@ -187,7 +188,14 @@ class Step < ApplicationRecord
|
|||
private
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::AllCheckedStepsAutomationObserver.new(my_module, last_modified_by).call if saved_change_to_completed? && completed
|
||||
return unless protocol.in_module?
|
||||
|
||||
if saved_change_to_completed?
|
||||
AutomationObservers::CompletedStepChangeAutomationObserver.new(my_module, last_modified_by).call if completed
|
||||
AutomationObservers::AllCompletedStepsAutomationObserver.new(my_module, last_modified_by).call
|
||||
end
|
||||
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(self, last_modified_by).call
|
||||
end
|
||||
|
||||
def duplicate_table(new_step, user, table)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class StepComment < Comment
|
||||
include ObservableModel
|
||||
|
||||
before_create :fill_unseen_by
|
||||
after_create :run_observers
|
||||
|
||||
belongs_to :step, foreign_key: :associated_id, inverse_of: :step_comments
|
||||
|
||||
|
|
@ -16,4 +19,8 @@ class StepComment < Comment
|
|||
def fill_unseen_by
|
||||
self.unseen_by += step.protocol.my_module.experiment.project.users.where.not(id: user.id).pluck(:id)
|
||||
end
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(step, last_modified_by || user).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ class StepOrderableElement < ApplicationRecord
|
|||
validates :position, uniqueness: { scope: :step }
|
||||
validate :check_step_relations
|
||||
|
||||
after_create :run_observers
|
||||
around_destroy :decrement_following_elements_positions
|
||||
|
||||
belongs_to :step, inverse_of: :step_orderable_elements, touch: true
|
||||
|
|
@ -26,4 +27,8 @@ class StepOrderableElement < ApplicationRecord
|
|||
step.normalize_elements_position
|
||||
end
|
||||
end
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(step, step.last_modified_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
class StepText < ApplicationRecord
|
||||
include TinyMceImages
|
||||
include ActionView::Helpers::TextHelper
|
||||
include ObservableModel
|
||||
|
||||
auto_strip_attributes :name, nullify: false
|
||||
validates :name, length: { maximum: Constants::NAME_MAX_LENGTH }
|
||||
|
|
@ -38,4 +39,10 @@ class StepText < ApplicationRecord
|
|||
new_step_text
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(step, step.last_modified_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
class Table < ApplicationRecord
|
||||
include SearchableModel
|
||||
include TableHelper
|
||||
include ObservableModel
|
||||
|
||||
auto_strip_attributes :name, nullify: false
|
||||
validates :name,
|
||||
|
|
@ -102,4 +103,8 @@ class Table < ApplicationRecord
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def run_observers
|
||||
AutomationObservers::ProtocolContentChangedAutomationObserver.new(step, step&.last_modified_by).call
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module AutomationObservers
|
||||
class AllCheckedStepsAutomationObserver
|
||||
class AllCompletedStepsAutomationObserver
|
||||
def initialize(my_module, user)
|
||||
@my_module = my_module
|
||||
@user = user
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module AutomationObservers
|
||||
class CompletedStepChangeAutomationObserver
|
||||
def initialize(my_module, user)
|
||||
@my_module = my_module
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
return unless @my_module.team.settings.dig('team_automation_settings', 'step_marked_as_completed')
|
||||
return unless @my_module.my_module_status.initial_status?
|
||||
|
||||
previous_status_id = @my_module.my_module_status.id
|
||||
@my_module.update!(my_module_status: @my_module.my_module_status.next_status)
|
||||
|
||||
Activities::CreateActivityService
|
||||
.call(activity_type: :automation_task_status_changed,
|
||||
owner: @user,
|
||||
team: @my_module.team,
|
||||
project: @my_module.project,
|
||||
subject: @my_module,
|
||||
message_items: {
|
||||
my_module: @my_module.id,
|
||||
my_module_status_old: previous_status_id,
|
||||
my_module_status_new: @my_module.my_module_status.id
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module AutomationObservers
|
||||
class ProtocolContentChangedAutomationObserver
|
||||
def initialize(step, user)
|
||||
@step = step
|
||||
@my_module = step&.my_module
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
return if @step.blank?
|
||||
return unless @step.protocol.in_module?
|
||||
return unless @my_module.team.settings.dig('team_automation_settings', 'protocol_content_added')
|
||||
return unless @my_module.my_module_status.initial_status?
|
||||
|
||||
previous_status_id = @my_module.my_module_status.id
|
||||
@my_module.update!(my_module_status: @my_module.my_module_status.next_status)
|
||||
|
||||
Activities::CreateActivityService
|
||||
.call(activity_type: :automation_task_status_changed,
|
||||
owner: @user,
|
||||
team: @my_module.team,
|
||||
project: @my_module.project,
|
||||
subject: @my_module,
|
||||
message_items: {
|
||||
my_module: @my_module.id,
|
||||
my_module_status_old: previous_status_id,
|
||||
my_module_status_new: @my_module.my_module_status.id
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module AutomationObservers
|
||||
class ResultCreateAutomationObserver
|
||||
def initialize(my_module, user)
|
||||
@my_module = my_module
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
return @my_module.team.settings.dig('team_automation_settings', 'task_result_added') unless @my_module.team.settings.dig('team_automation_settings', 'task_result_added')
|
||||
return unless @my_module.my_module_status.initial_status?
|
||||
|
||||
previous_status_id = @my_module.my_module_status.id
|
||||
@my_module.update!(my_module_status: @my_module.my_module_status.next_status)
|
||||
|
||||
Activities::CreateActivityService
|
||||
.call(activity_type: :automation_task_status_changed,
|
||||
owner: @user,
|
||||
team: @my_module.team,
|
||||
project: @my_module.project,
|
||||
subject: @my_module,
|
||||
message_items: {
|
||||
my_module: @my_module.id,
|
||||
my_module_status_old: previous_status_id,
|
||||
my_module_status_new: @my_module.my_module_status.id
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue