mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Remove carts older than 6 months
This commit is contained in:
@@ -15,5 +15,14 @@ class RemoveTransientData
|
||||
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
|
||||
|
||||
@@ -35,5 +35,30 @@ describe RemoveTransientData do
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user