mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Fix soft-deletion in CartService and update spec
This commit is contained in:
@@ -29,11 +29,15 @@ class CartService
|
||||
|
||||
variants_data.each do |variant_data|
|
||||
loaded_variant = loaded_variants[variant_data[:variant_id].to_i]
|
||||
|
||||
if loaded_variant.deleted?
|
||||
remove_deleted_variant(loaded_variant)
|
||||
next
|
||||
end
|
||||
|
||||
next unless varies_from_cart(variant_data, loaded_variant)
|
||||
|
||||
attempt_cart_add(
|
||||
loaded_variant, variant_data[:quantity], variant_data[:max_quantity]
|
||||
)
|
||||
attempt_cart_add(loaded_variant, variant_data[:quantity], variant_data[:max_quantity])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -41,12 +45,16 @@ class CartService
|
||||
@indexed_variants ||= begin
|
||||
variant_ids_in_data = variants_data.map{ |v| v[:variant_id] }
|
||||
|
||||
Spree::Variant.where(id: variant_ids_in_data).
|
||||
Spree::Variant.with_deleted.where(id: variant_ids_in_data).
|
||||
includes(:default_price, :stock_items, :product).
|
||||
index_by(&:id)
|
||||
end
|
||||
end
|
||||
|
||||
def remove_deleted_variant(variant)
|
||||
line_item_for_variant(variant).andand.destroy
|
||||
end
|
||||
|
||||
def attempt_cart_add(variant, quantity, max_quantity = nil)
|
||||
quantity = quantity.to_i
|
||||
max_quantity = max_quantity.to_i if max_quantity
|
||||
|
||||
@@ -60,7 +60,7 @@ describe CartService do
|
||||
let(:relevant_line_item) { order.reload.find_line_item_by_variant(variant) }
|
||||
|
||||
describe "when the soft-deleted variant is not in the cart yet" do
|
||||
xit "doesn't fail, and does not add the deleted variant to the cart" do
|
||||
it "does not add the deleted variant to the cart" do
|
||||
variant.delete
|
||||
|
||||
cart_service.populate({ variants: { variant.id.to_s => { quantity: '2' } } }, true)
|
||||
@@ -70,17 +70,17 @@ describe CartService do
|
||||
end
|
||||
end
|
||||
|
||||
describe "when the soft-deleted variant already has a line_item in the cart" do
|
||||
describe "when the soft-deleted variant is already in the cart" do
|
||||
let!(:existing_line_item) {
|
||||
create(:line_item, variant: variant, quantity: 2, order: order)
|
||||
}
|
||||
|
||||
xit "doesn't fail, and removes the line_item from the cart" do
|
||||
it "removes the line_item from the cart" do
|
||||
variant.delete
|
||||
|
||||
cart_service.populate({ variants: { variant.id.to_s => { quantity: '2' } } }, true)
|
||||
cart_service.populate({ variants: { variant.id.to_s => { quantity: '3' } } }, true)
|
||||
|
||||
expect(relevant_line_item).to be_nil
|
||||
expect(Spree::LineItem.where(id: relevant_line_item).first).to be_nil
|
||||
expect(cart_service.errors.count).to be 0
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user