Class: SpreeCmCommissioner::Orders::DailyArchiveInactiveOrders
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Orders::DailyArchiveInactiveOrders
- Includes:
- Spree::ServiceModule::Base
- Defined in:
- app/services/spree_cm_commissioner/orders/daily_archive_inactive_orders.rb
Instance Method Summary collapse
-
#call ⇒ Object
Archives incomplete orders that haven’t been updated for 14 days.
Instance Method Details
#call ⇒ Object
Archives incomplete orders that haven’t been updated for 14 days. Archived orders are hidden from users (Orders::Find filters them out). Users will see an empty cart and can start a new one.
Criteria for archiving:
-
archived_at IS NULL (not already archived)
-
completed_at IS NULL (incomplete/unfinished orders)
-
updated_at < 14 days ago (inactive for 2+ weeks)
Why 14 days?
-
Gives users time to notice discontinued products and clean up their cart
-
If product is discontinued during these 14 days, users can manually clear their cart when they see the “unavailable” status in the UI
-
Gives the team time to investigate payment errors or stuck orders
Why archive all payment states?
-
Data is preserved (archived_at is just a flag, not deletion)
-
Team can still review archived orders in the database if needed
-
Keeps user-facing order history clean (hidden from Orders::Find queries)
-
Reduces clutter in active carts/orders
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/spree_cm_commissioner/orders/daily_archive_inactive_orders.rb', line 26 def call # Archives orders from the 1-week window (21-14 days ago) # Uses parameterized scope: threshold=14.days.ago, window=7.days inactive_orders = Spree::Order.inactive_incomplete(threshold: 14.days.ago, window: 7.days) count = inactive_orders.count # Archive all inactive orders with reason in internal_note. # We use bulk update_all instead of find_each because: # - We rarely use internal_note, so preserving history is not critical # - Bulk update is significantly faster (1 query vs N queries) # - For typical 100-500 orders/day, bulk update is worth the performance gain inactive_orders.update_all( # rubocop:disable Rails/SkipsModelValidations archived_at: Time.current, internal_note: 'Auto-archived: inactive for 14 days', updated_at: Time.current ) CmAppLogger.log( label: 'SpreeCmCommissioner::Orders::DailyArchiveInactiveOrders#call completed', data: { archived_count: count, threshold_days: 14 } ) success(archived_count: count) rescue StandardError => e CmAppLogger.error( label: 'SpreeCmCommissioner::Orders::DailyArchiveInactiveOrders#call failed', data: { error_class: e.class.name, error_message: e., backtrace: e.backtrace&.first(5)&.join("\n") } ) failure(nil, e.) end |