From 6f93d4b72a432ddcb085be7edf3d2577ee28e7dd Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 4 Apr 2025 10:04:39 +0200 Subject: [PATCH] Add system to auto reconsider inactive bubbles after 30 days --- app/jobs/{ => bubble}/auto_pop_all_due_job.rb | 2 +- .../bubble/auto_reconsider_all_stagnated_job.rb | 9 +++++++++ app/models/bubble/engageable.rb | 16 ++++++++++++++++ app/views/bubbles/_pop_toggle.html.erb | 2 +- config/recurring.yml | 5 ++++- test/models/bubble/engageable_test.rb | 15 +++++++++++++++ 6 files changed, 46 insertions(+), 3 deletions(-) rename app/jobs/{ => bubble}/auto_pop_all_due_job.rb (73%) create mode 100644 app/jobs/bubble/auto_reconsider_all_stagnated_job.rb diff --git a/app/jobs/auto_pop_all_due_job.rb b/app/jobs/bubble/auto_pop_all_due_job.rb similarity index 73% rename from app/jobs/auto_pop_all_due_job.rb rename to app/jobs/bubble/auto_pop_all_due_job.rb index d9462934d..845c0889e 100644 --- a/app/jobs/auto_pop_all_due_job.rb +++ b/app/jobs/bubble/auto_pop_all_due_job.rb @@ -1,4 +1,4 @@ -class AutoPopAllDueJob < ApplicationJob +class Bubble::AutoPopAllDueJob < ApplicationJob queue_as :default def perform diff --git a/app/jobs/bubble/auto_reconsider_all_stagnated_job.rb b/app/jobs/bubble/auto_reconsider_all_stagnated_job.rb new file mode 100644 index 000000000..1b08f4371 --- /dev/null +++ b/app/jobs/bubble/auto_reconsider_all_stagnated_job.rb @@ -0,0 +1,9 @@ +class Bubble::AutoReconsiderAllStagnatedJob < ApplicationJob + queue_as :default + + def perform + ApplicationRecord.with_each_tenant do |tenant| + Bubble.auto_reconsider_all_stagnated + end + end +end diff --git a/app/models/bubble/engageable.rb b/app/models/bubble/engageable.rb index 514b7ba98..cd72947b2 100644 --- a/app/models/bubble/engageable.rb +++ b/app/models/bubble/engageable.rb @@ -1,11 +1,27 @@ module Bubble::Engageable extend ActiveSupport::Concern + STAGNATED_AFTER = 30.days + included do has_one :engagement, dependent: :destroy, class_name: "Bubble::Engagement" scope :doing, -> { active.joins(:engagement) } scope :considering, -> { active.where.missing(:engagement) } + + scope :stagnated, -> { doing.where(last_active_at: ..STAGNATED_AFTER.ago) } + end + + class_methods do + def auto_reconsider_all_stagnated + stagnated.find_each do |bubble| + bubble.reconsider + end + end + end + + def auto_reconsider_at + last_active_at + STAGNATED_AFTER if last_active_at end def doing? diff --git a/app/views/bubbles/_pop_toggle.html.erb b/app/views/bubbles/_pop_toggle.html.erb index 60dfc4993..0bcb1b818 100644 --- a/app/views/bubbles/_pop_toggle.html.erb +++ b/app/views/bubbles/_pop_toggle.html.erb @@ -37,7 +37,7 @@ <% if bubble.doing? %> - Returns to Considering if no activity <%= local_datetime_tag(bubble.auto_pop_at, style: :indays) -%>. + Returns to Considering if no activity <%= local_datetime_tag(bubble.auto_reconsider_at, style: :indays) -%>. <% else %> Auto-closes if no activity <%= local_datetime_tag(bubble.auto_pop_at, style: :indays) -%>. <% end %> diff --git a/config/recurring.yml b/config/recurring.yml index c05571f2e..7ace77fd4 100644 --- a/config/recurring.yml +++ b/config/recurring.yml @@ -1,6 +1,9 @@ production: auto_pop_all_due: - class: AutoPopAllDueJob + class: Bubble::AutoPopAllDueJob + schedule: every hour + auto_reconsider_all_inactive: + class: Bubble::AutoReconsiderAllStagnatedJob schedule: every hour remove_abandoned_creations: class: RemoveAbandonedCreationsJob diff --git a/test/models/bubble/engageable_test.rb b/test/models/bubble/engageable_test.rb index 2e716d165..57a605149 100644 --- a/test/models/bubble/engageable_test.rb +++ b/test/models/bubble/engageable_test.rb @@ -46,4 +46,19 @@ class Bubble::EngageableTest < ActiveSupport::TestCase assert_includes Bubble.considering, bubbles(:text) assert_not_includes Bubble.considering, bubbles(:logo) end + + test "auto_reconsider_all_stagnated" do + bubbles(:logo).update!(last_active_at: 1.day.ago - Bubble::Engageable::STAGNATED_AFTER) + bubbles(:logo).engage + + bubbles(:shipping).update!(last_active_at: 1.day.from_now - Bubble::Engageable::STAGNATED_AFTER) + bubbles(:shipping).engage + + assert_difference -> { Bubble.considering.count }, +1 do + Bubble.auto_reconsider_all_stagnated + end + + assert bubbles(:shipping).reload.doing? + assert bubbles(:logo).reload.considering? + end end