Class: SpreeCmCommissioner::Orders::BulkArchiveInactiveOrders

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/orders/bulk_archive_inactive_orders.rb

Instance Method Summary collapse

Instance Method Details

#callObject

Archives ALL incomplete orders inactive for 14+ days (no time range limit). This is a bulk operation for manual cleanup via Sidekiq. Archived orders are hidden from users (Orders::Find filters them out).

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)

Difference from DailyArchiveInactiveOrders:

  • DailyArchiveInactiveOrders: Runs daily, archives only 1-week window (21-14 days)

  • BulkArchiveInactiveOrders: Manual job, archives ALL orders older than 14 days

Use case: Bulk cleanup when needed, triggered manually via Sidekiq



20
21
22
23
24
25
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
# File 'app/services/spree_cm_commissioner/orders/bulk_archive_inactive_orders.rb', line 20

def call
  # Archives ALL orders inactive for 14+ days (no time range limit)
  # Uses parameterized scope: threshold=14.days.ago
  inactive_orders = Spree::Order.inactive_incomplete_all(threshold: 14.days.ago)

  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 bulk operations, performance is critical
  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::BulkArchiveInactiveOrders#call completed',
    data: {
      archived_count: count,
      threshold_days: 14
    }
  )

  success(archived_count: count)
rescue StandardError => e
  CmAppLogger.error(
    label: 'SpreeCmCommissioner::Orders::BulkArchiveInactiveOrders#call failed',
    data: {
      error_class: e.class.name,
      error_message: e.message,
      backtrace: e.backtrace&.first(5)&.join("\n")
    }
  )
  failure(nil, e.message)
end