diff --git a/app/models/card.rb b/app/models/card.rb index c3d27b956..94ee043d2 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -24,6 +24,8 @@ class Card < ApplicationRecord when "oldest" then chronologically when "latest" then latest when "stalled" then stalled.chronologically + when "closing_soon" then closing_soon.chronologically + when "falling_back_soon" then falling_back_soon.chronologically when "closed" then closed end end diff --git a/app/models/card/entropic.rb b/app/models/card/entropic.rb index d275d6d81..cd65b34cc 100644 --- a/app/models/card/entropic.rb +++ b/app/models/card/entropic.rb @@ -9,7 +9,23 @@ module Card::Entropic end scope :stagnated, -> { doing.entropic_by(:auto_reconsider_period) } + scope :due_to_be_closed, -> { considering.entropic_by(:auto_close_period) } + + scope :closing_soon, -> do + considering + .left_outer_joins(collection: :entropy_configuration) + .where("last_active_at > DATETIME('now', '-' || COALESCE(entropy_configurations.auto_close_period, ?) || ' seconds')", Entropy::Configuration.default.auto_close_period) + .where("last_active_at <= DATETIME('now', '-' || CAST(COALESCE(entropy_configurations.auto_close_period, ?) * 0.75 AS INTEGER) || ' seconds')", Entropy::Configuration.default.auto_close_period) + end + + scope :falling_back_soon, -> do + doing + .left_outer_joins(collection: :entropy_configuration) + .where("last_active_at > DATETIME('now', '-' || COALESCE(entropy_configurations.auto_reconsider_period, ?) || ' seconds')", Entropy::Configuration.default.auto_reconsider_period) + .where("last_active_at <= DATETIME('now', '-' || CAST(COALESCE(entropy_configurations.auto_reconsider_period, ?) * 0.75 AS INTEGER) || ' seconds')", Entropy::Configuration.default.auto_reconsider_period) + end + delegate :auto_close_period, :auto_reconsider_period, to: :collection end diff --git a/app/models/filter/fields.rb b/app/models/filter/fields.rb index 3cf3c2665..fcdde0346 100644 --- a/app/models/filter/fields.rb +++ b/app/models/filter/fields.rb @@ -1,7 +1,7 @@ module Filter::Fields extend ActiveSupport::Concern - INDEXES = %w[ newest oldest latest stalled ] + INDEXES = %w[ newest oldest latest stalled closing_soon falling_back_soon ] delegate :default_value?, to: :class diff --git a/test/models/card/entropic_test.rb b/test/models/card/entropic_test.rb index 2d4d604e3..eae2e1ae1 100644 --- a/test/models/card/entropic_test.rb +++ b/test/models/card/entropic_test.rb @@ -70,6 +70,16 @@ class Card::EntropicTest < ActiveSupport::TestCase assert_not cards(:shipping).reload.closed? end + test "closing soon scope" do + cards(:logo, :shipping).each(&:published!).each(&:reconsider) + + cards(:logo).update!(last_active_at: entropy_configurations(:writebook_collection).auto_close_period.seconds.ago + 2.days) + cards(:shipping).update!(last_active_at: entropy_configurations(:writebook_collection).auto_close_period.seconds.ago - 2.days) + + assert_includes Card.closing_soon, cards(:logo) + assert_not_includes Card.closing_soon, cards(:shipping) + end + test "auto consider all stagnated using the default account entropy configuration" do travel_to Time.current @@ -104,4 +114,16 @@ class Card::EntropicTest < ActiveSupport::TestCase assert cards(:logo).reload.considering? assert_equal Time.current, cards(:logo).last_active_at end + + test "falling back scope" do + travel_to Time.current + + cards(:logo, :shipping).each(&:engage) + + cards(:logo).update!(last_active_at: 1.day.ago - entropy_configurations("writebook_collection").auto_close_period) + cards(:shipping).update!(last_active_at: 1.day.from_now - entropy_configurations("writebook_collection").auto_close_period) + + assert_includes Card.falling_back_soon, cards(:shipping) + assert_not_includes Card.falling_back_soon, cards(:logo) + end end