Skip to content

Commit

Permalink
Remove carts older than 6 months
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Yorkley committed Jan 25, 2021
1 parent 624caad commit 930f0e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/tasks/data/remove_transient_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ class Session < ActiveRecord::Base
def call
Rails.logger.info("#{self.class.name}: processing")

Spree::Order.where("state = 'cart' AND created_at < ?", RETENTION_PERIOD).delete_all
Spree::StateChange.where("created_at < ?", RETENTION_PERIOD).delete_all
Spree::LogEntry.where("created_at < ?", RETENTION_PERIOD).delete_all
Session.where("updated_at < ?", RETENTION_PERIOD).delete_all

# Clear old carts and associated records
old_carts = Spree::Order.where("state = 'cart' AND updated_at < ?", RETENTION_PERIOD)
old_cart_line_items = Spree::LineItem.where(order_id: old_carts)
old_cart_adjustments = Spree::Adjustment.where(order_id: old_carts)

old_cart_adjustments.delete_all
old_cart_line_items.delete_all
old_carts.delete_all
end
end
25 changes: 25 additions & 0 deletions spec/lib/tasks/data/remove_transient_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,30 @@

expect(RemoveTransientData::Session.all).to be_empty
end

describe "deleting old carts" do
let(:product) { create(:product) }
let(:variant) { product.variants.first }

let!(:cart) { create(:order, state: 'cart') }
let!(:line_item) { create(:line_item, order: cart, variant: variant) }
let!(:adjustment) { create(:adjustment, order: cart) }

let!(:old_cart) { create(:order, state: 'cart', updated_at: retention_period - 1.day) }
let!(:old_line_item) { create(:line_item, order: old_cart, variant: variant) }
let!(:old_adjustment) { create(:adjustment, order: old_cart) }

it 'deletes cart orders and related objects older than retention_period' do
RemoveTransientData.new.call

expect{ cart.reload }.to_not raise_error
expect{ line_item.reload }.to_not raise_error
expect{ adjustment.reload }.to_not raise_error

expect{ old_cart.reload }.to raise_error ActiveRecord::RecordNotFound
expect{ old_line_item.reload }.to raise_error ActiveRecord::RecordNotFound
expect{ old_adjustment.reload }.to raise_error ActiveRecord::RecordNotFound
end
end
end
end

0 comments on commit 930f0e7

Please sign in to comment.