Class: Activity::AutoChainCloser
- Inherits:
-
BaseService
- Object
- BaseService
- Activity::AutoChainCloser
- Defined in:
- app/services/activity/auto_chain_closer.rb
Overview
Responsible for auto closing activities which have an auto close rule setup
in their activity chain result types. This functionality aids in creating
escalation or email drip campaigns type workflows when paired with email templates.
Defined Under Namespace
Classes: Result
Instance Attribute Summary
Attributes inherited from BaseService
Instance Method Summary collapse
-
#load_activities_to_auto_close ⇒ ActiveRecord::Relation<Activity>
Overdue activities whose activity type participates in a dated chain.
-
#process(_activities: nil) ⇒ Result
Auto-closes every overdue activity whose chain type has an auto-close rule.
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#load_activities_to_auto_close ⇒ ActiveRecord::Relation<Activity>
Overdue activities whose activity type participates in a dated chain.
40 41 42 |
# File 'app/services/activity/auto_chain_closer.rb', line 40 def load_activities_to_auto_close Activity.overdue_activities.joins(activity_type: :activity_chain_types).merge(ActivityChainType.due_date) end |
#process(_activities: nil) ⇒ Result
Auto-closes every overdue activity whose chain type has an auto-close rule.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/services/activity/auto_chain_closer.rb', line 17 def process(_activities: nil) # Load activities eligible for an auto close failed_activities = [] auto_closed_activities = [] load_activities_to_auto_close.find_each(batch_size: 100) do |activity| result = activity.auto_close_if_applicable if result == :overdue_auto_close auto_closed_activities << { activity_id: activity.id, result: activity.activity_result_type&.result_code } else # Our query should have filtered out these, if they end up here they are to be considered failed failed_activities << { activity_id: activity.id, error: result } end rescue StandardError => e = activity.errors_to_s failed_activities << { activity_id: activity.id, error: } ErrorReporting.error(e, "Cannot auto close activity id #{activity.id}, errors: #{}") end Result.new(auto_closed_activities: auto_closed_activities, failed_activities: failed_activities) end |