spree_cm_commissioner 2.3.0.pre.pre18 → 2.3.0.pre.pre20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31d13343c99ba15786dc8ea2706e64fd79ada05ce505f06fea9dfdffd24f1313
4
- data.tar.gz: 2638b1befd6df8abbc2ed8bcabfd447bb15bb2006336e2ff894662c58c07e933
3
+ metadata.gz: f73314825dc2db092e1255e49203ceb8e1071e0dac1c11803d8343b8dc37fafa
4
+ data.tar.gz: 32c25a52977a2f20591b8650ece48a69e517845b3431b79e3c57da63cf8ddb50
5
5
  SHA512:
6
- metadata.gz: 487aa62c3894d83cd38036169a677ab79633f9792654e4d87e59c61fdbbe6a6bb60cd303f69727e093fc37fcf41c361ad43fbb2f60e79f84e5cd6e830aa05ed2
7
- data.tar.gz: 608a5d18e523889840e11a31f2686d357636de604fb360abb0c5074c1f3eda129c33012e779f5dc5ce025aed2842eb605cb051014217fccf4fa36361b8315fb5
6
+ metadata.gz: 1bd90e765ad3bdd9f28b6199900a7ea4f587dd11732af3a03c8bd3b13d234eb83765740de6619223dd03cbe77cb4b865c82c12c8a1845bc05157977463bf17d6
7
+ data.tar.gz: ef7380007df4d1ee55da2fea5d2d9cb2b3b5d931c44d17a955b9925c338f8a6285df1df59459b643152cd03fda819bcdd264075bc87eaaff5961e93d08b84606
data/Gemfile.lock CHANGED
@@ -34,7 +34,7 @@ GIT
34
34
  PATH
35
35
  remote: .
36
36
  specs:
37
- spree_cm_commissioner (2.3.0.pre.pre18)
37
+ spree_cm_commissioner (2.3.0.pre.pre20)
38
38
  activerecord-multi-tenant
39
39
  activerecord_json_validator (~> 2.1, >= 2.1.3)
40
40
  aws-sdk-cloudfront
@@ -0,0 +1,13 @@
1
+ module SpreeCmCommissioner
2
+ module Orders
3
+ class BulkArchiveInactiveOrdersJob < ApplicationJob
4
+ # Manual job that archives ALL incomplete orders inactive for 14+ days.
5
+ # Thin wrapper that calls BulkArchiveInactiveOrders service.
6
+ # ApplicationJob handles error logging via around_perform hook.
7
+ # Triggered manually via Sidekiq (not scheduled).
8
+ def perform
9
+ SpreeCmCommissioner::Orders::BulkArchiveInactiveOrders.new.call
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module SpreeCmCommissioner
2
+ module Orders
3
+ class DailyArchiveInactiveOrdersJob < ApplicationJob
4
+ # Scheduled job that runs daily to archive incomplete orders inactive for 14+ days.
5
+ # Thin wrapper that calls DailyArchiveInactiveOrders service.
6
+ # ApplicationJob handles error logging via around_perform hook.
7
+ def perform
8
+ SpreeCmCommissioner::Orders::DailyArchiveInactiveOrders.new.call
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ module SpreeCmCommissioner
2
+ module OrderScopes
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # Archiving scopes
7
+ scope :archived, -> { where.not(archived_at: nil) }
8
+ scope :not_archived, -> { where(archived_at: nil) }
9
+
10
+ # Payment and state scopes
11
+ scope :subscription, -> { where.not(subscription_id: nil) }
12
+ scope :paid, -> { where(payment_state: :paid) }
13
+ scope :complete_or_canceled, -> { complete.or(where(state: 'canceled')) }
14
+ scope :payment, -> { incomplete.where(state: 'payment') }
15
+ scope :without_user, -> { where(user_id: nil) }
16
+
17
+ # Inactive orders scopes with parameterized dates
18
+ # Usage: Spree::Order.inactive_incomplete_all(threshold: 14.days.ago)
19
+ scope :inactive_incomplete_all, lambda { |threshold: 14.days.ago|
20
+ where(archived_at: nil, completed_at: nil)
21
+ .where('updated_at < ?', threshold)
22
+ }
23
+
24
+ # Usage: Spree::Order.inactive_incomplete(threshold: 14.days.ago, window: 7.days)
25
+ # Returns orders updated between (threshold - window) and threshold
26
+ scope :inactive_incomplete, lambda { |threshold: 14.days.ago, window: 7.days|
27
+ where(archived_at: nil, completed_at: nil)
28
+ .where('updated_at >= ?', threshold - window)
29
+ .where('updated_at < ?', threshold)
30
+ }
31
+
32
+ # Filter scopes
33
+ scope :filter_by_match_user_contact, lambda { |user|
34
+ complete.where(
35
+ '(email = :email OR intel_phone_number = :intel_phone_number) AND user_id IS NULL',
36
+ email: user.email,
37
+ intel_phone_number: user.intel_phone_number
38
+ )
39
+ }
40
+
41
+ scope :filter_by_vendor, lambda { |vendor|
42
+ joins(:line_items).where(spree_line_items: { vendor_id: vendor }).distinct
43
+ }
44
+ end
45
+ end
46
+ end
@@ -1,31 +1,12 @@
1
1
  module SpreeCmCommissioner
2
2
  module OrderDecorator
3
- def self.prepended(base) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
3
+ def self.prepended(base) # rubocop:disable Metrics/MethodLength
4
4
  base.include SpreeCmCommissioner::StoreMetadata
5
5
  base.include SpreeCmCommissioner::PhoneNumberSanitizer
6
6
  base.include SpreeCmCommissioner::OrderSeatable
7
7
  base.include SpreeCmCommissioner::OrderStateMachine
8
8
  base.include SpreeCmCommissioner::RouteOrderCountable
9
-
10
- base.scope :subscription, -> { where.not(subscription_id: nil) }
11
- base.scope :paid, -> { where(payment_state: :paid) }
12
- base.scope :complete_or_canceled, -> { complete.or(where(state: 'canceled')) }
13
- base.scope :payment, -> { incomplete.where(state: 'payment') }
14
- base.scope :archived, -> { where.not(archived_at: nil) }
15
- base.scope :not_archived, -> { where(archived_at: nil) }
16
- base.scope :without_user, -> { where(user_id: nil) }
17
-
18
- base.scope :filter_by_match_user_contact, lambda { |user|
19
- complete.where(
20
- '(email = :email OR intel_phone_number = :intel_phone_number) AND user_id IS NULL',
21
- email: user.email,
22
- intel_phone_number: user.intel_phone_number
23
- )
24
- }
25
-
26
- base.scope :filter_by_vendor, lambda { |vendor|
27
- joins(:line_items).where(spree_line_items: { vendor_id: vendor }).distinct
28
- }
9
+ base.include SpreeCmCommissioner::OrderScopes
29
10
 
30
11
  base.before_create :link_by_phone_number
31
12
  base.before_create :associate_customer
@@ -88,6 +69,7 @@ module SpreeCmCommissioner
88
69
  def restart_checkout_flow
89
70
  ActiveRecord::Base.transaction do
90
71
  cancel_blocks! if should_manage_blocks?
72
+ log_state_changes(state_name: 'cart', old_state: state, new_state: 'cart')
91
73
  update_columns( # rubocop:disable Rails/SkipsModelValidations
92
74
  state: 'cart',
93
75
  updated_at: Time.current
@@ -0,0 +1,52 @@
1
+ module SpreeCmCommissioner
2
+ module Orders
3
+ class BulkArchiveInactiveOrders
4
+ prepend ::Spree::ServiceModule::Base
5
+
6
+ # Archives ALL incomplete orders inactive for 14+ days (no time range limit).
7
+ # This is a bulk operation for manual cleanup via Sidekiq.
8
+ # Archived orders are hidden from users (Orders::Find filters them out).
9
+ #
10
+ # Criteria for archiving:
11
+ # - archived_at IS NULL (not already archived)
12
+ # - completed_at IS NULL (incomplete/unfinished orders)
13
+ # - updated_at < 14 days ago (inactive for 2+ weeks)
14
+ #
15
+ # Difference from ArchiveInactiveOrders:
16
+ # - ArchiveInactiveOrders: Runs daily, archives only 1-week window (21-14 days)
17
+ # - BulkArchiveInactiveOrders: Manual job, archives ALL orders older than 14 days
18
+ #
19
+ # Use case: Bulk cleanup when needed, triggered manually via Sidekiq
20
+ def call
21
+ # Archives ALL orders inactive for 14+ days (no time range limit)
22
+ # Uses parameterized scope: threshold=14.days.ago
23
+ inactive_orders = Spree::Order.inactive_incomplete_all(threshold: 14.days.ago)
24
+
25
+ count = inactive_orders.count
26
+
27
+ # Archive all inactive orders with reason in internal_note.
28
+ # We use bulk update_all instead of find_each because:
29
+ # - We rarely use internal_note, so preserving history is not critical
30
+ # - Bulk update is significantly faster (1 query vs N queries)
31
+ # - For bulk operations, performance is critical
32
+ inactive_orders.update_all( # rubocop:disable Rails/SkipsModelValidations
33
+ archived_at: Time.current,
34
+ internal_note: 'Auto-archived: inactive for 14 days',
35
+ updated_at: Time.current
36
+ )
37
+
38
+ CmAppLogger.log(
39
+ label: 'SpreeCmCommissioner::Orders::BulkArchiveInactiveOrders#call completed',
40
+ data: {
41
+ archived_count: count,
42
+ threshold_days: 14
43
+ }
44
+ )
45
+
46
+ success(archived_count: count)
47
+ rescue StandardError => e
48
+ failure(nil, e.message)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,58 @@
1
+ module SpreeCmCommissioner
2
+ module Orders
3
+ class DailyArchiveInactiveOrders
4
+ prepend ::Spree::ServiceModule::Base
5
+
6
+ # Archives incomplete orders that haven't been updated for 14 days.
7
+ # Archived orders are hidden from users (Orders::Find filters them out).
8
+ # Users will see an empty cart and can start a new one.
9
+ #
10
+ # Criteria for archiving:
11
+ # - archived_at IS NULL (not already archived)
12
+ # - completed_at IS NULL (incomplete/unfinished orders)
13
+ # - updated_at < 14 days ago (inactive for 2+ weeks)
14
+ #
15
+ # Why 14 days?
16
+ # - Gives users time to notice discontinued products and clean up their cart
17
+ # - If product is discontinued during these 14 days, users can manually clear
18
+ # their cart when they see the "unavailable" status in the UI
19
+ # - Gives the team time to investigate payment errors or stuck orders
20
+ #
21
+ # Why archive all payment states?
22
+ # - Data is preserved (archived_at is just a flag, not deletion)
23
+ # - Team can still review archived orders in the database if needed
24
+ # - Keeps user-facing order history clean (hidden from Orders::Find queries)
25
+ # - Reduces clutter in active carts/orders
26
+ def call
27
+ # Archives orders from the 1-week window (21-14 days ago)
28
+ # Uses parameterized scope: threshold=14.days.ago, window=7.days
29
+ inactive_orders = Spree::Order.inactive_incomplete(threshold: 14.days.ago, window: 7.days)
30
+
31
+ count = inactive_orders.count
32
+
33
+ # Archive all inactive orders with reason in internal_note.
34
+ # We use bulk update_all instead of find_each because:
35
+ # - We rarely use internal_note, so preserving history is not critical
36
+ # - Bulk update is significantly faster (1 query vs N queries)
37
+ # - For typical 100-500 orders/day, bulk update is worth the performance gain
38
+ inactive_orders.update_all( # rubocop:disable Rails/SkipsModelValidations
39
+ archived_at: Time.current,
40
+ internal_note: 'Auto-archived: inactive for 14 days',
41
+ updated_at: Time.current
42
+ )
43
+
44
+ CmAppLogger.log(
45
+ label: 'SpreeCmCommissioner::Orders::DailyArchiveInactiveOrders#call completed',
46
+ data: {
47
+ archived_count: count,
48
+ threshold_days: 14
49
+ }
50
+ )
51
+
52
+ success(archived_count: count)
53
+ rescue StandardError => e
54
+ failure(nil, e.message)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ class AddIndexToSpreeOrdersUpdatedAt < ActiveRecord::Migration[7.0]
2
+ def change
3
+ unless index_exists?(:spree_orders, :updated_at)
4
+ add_index :spree_orders, :updated_at, name: 'index_spree_orders_on_updated_at'
5
+ end
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  module SpreeCmCommissioner
2
- VERSION = '2.3.0-pre18'.freeze
2
+ VERSION = '2.3.0-pre20'.freeze
3
3
 
4
4
  module_function
5
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_cm_commissioner
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0.pre.pre18
4
+ version: 2.3.0.pre.pre20
5
5
  platform: ruby
6
6
  authors:
7
7
  - You
@@ -1310,6 +1310,8 @@ files:
1310
1310
  - app/jobs/spree_cm_commissioner/option_type_variants_public_metadata_updater_job.rb
1311
1311
  - app/jobs/spree_cm_commissioner/option_value_variants_public_metadata_updater_job.rb
1312
1312
  - app/jobs/spree_cm_commissioner/order_complete_telegram_sender_job.rb
1313
+ - app/jobs/spree_cm_commissioner/orders/bulk_archive_inactive_orders_job.rb
1314
+ - app/jobs/spree_cm_commissioner/orders/daily_archive_inactive_orders_job.rb
1313
1315
  - app/jobs/spree_cm_commissioner/product_event_id_to_children_syncer_job.rb
1314
1316
  - app/jobs/spree_cm_commissioner/queue_order_webhooks_requests_job.rb
1315
1317
  - app/jobs/spree_cm_commissioner/reports_assigner_job.rb
@@ -1357,6 +1359,7 @@ files:
1357
1359
  - app/models/concerns/spree_cm_commissioner/metafield.rb
1358
1360
  - app/models/concerns/spree_cm_commissioner/option_type_attr_type.rb
1359
1361
  - app/models/concerns/spree_cm_commissioner/option_value_attr_type.rb
1362
+ - app/models/concerns/spree_cm_commissioner/order_scopes.rb
1360
1363
  - app/models/concerns/spree_cm_commissioner/order_seatable.rb
1361
1364
  - app/models/concerns/spree_cm_commissioner/order_state_machine.rb
1362
1365
  - app/models/concerns/spree_cm_commissioner/parameterize_name.rb
@@ -1952,6 +1955,8 @@ files:
1952
1955
  - app/services/spree_cm_commissioner/intercity_taxi_order/update.rb
1953
1956
  - app/services/spree_cm_commissioner/metafields/product_metadata_service.rb
1954
1957
  - app/services/spree_cm_commissioner/order_params_checker.rb
1958
+ - app/services/spree_cm_commissioner/orders/bulk_archive_inactive_orders.rb
1959
+ - app/services/spree_cm_commissioner/orders/daily_archive_inactive_orders.rb
1955
1960
  - app/services/spree_cm_commissioner/orders/generate_commissions_decorator.rb
1956
1961
  - app/services/spree_cm_commissioner/organizer/export_guest_csv_service.rb
1957
1962
  - app/services/spree_cm_commissioner/organizer/export_invite_guest_csv_service.rb
@@ -2842,6 +2847,7 @@ files:
2842
2847
  - db/migrate/20251009033331_add_registered_by_to_spree_users.rb
2843
2848
  - db/migrate/20251009073040_add_lock_version_to_cm_routes.rb
2844
2849
  - db/migrate/20251009073929_add_lock_version_to_cm_vendor_routes.rb
2850
+ - db/migrate/20251113081853_add_index_to_spree_orders_updated_at.rb
2845
2851
  - docker-compose.yml
2846
2852
  - docs/api/scoped-access-token-endpoints.md
2847
2853
  - docs/option_types/attr_types.md