Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: bounded claims by the units earlier claims already cover
Damian Legawiecfd7bea32b6e0
diff --git a/spree/core/app/workflows/spree/claims/create.rb b/spree/core/app/workflows/spree/claims/create.rbindex 11363277b2..9186f92e96 100644--- a/spree/core/app/workflows/spree/claims/create.rb+++ b/spree/core/app/workflows/spree/claims/create.rb@@ -39,19 +39,35 @@ module Spreefailure(order, :no_items_to_claim) if items.blank?end+ # Bounded by what earlier claims left: each claim can pay out what was+ # paid for its units, so claiming the same units again would pay twice.+ # A denied or canceled claim settled nothing and frees its units.def normalize_items+ requested = Hash.new(0)+@normalized_items = items.map do |item|line_item = item[:line_item]quantity = item[:quantity].to_ifailure(order, :invalid_quantity) unless quantity.positive?failure(order, :item_not_on_order) unless line_item&.order_id == order.id- failure(order, :invalid_quantity) if quantity > line_item.quantity.to_i+ failure(order, :invalid_quantity) if quantity > claimable_quantity_for(line_item) - requested[line_item.id]+ requested[line_item.id] += quantityitem.merge(line_item: line_item, quantity: quantity)endend+ def claimable_quantity_for(line_item)+ claimed = Spree::ClaimLineItem.+ joins(:claim).+ where(line_item_id: line_item.id).+ where.not(Spree::Claim.table_name => { status: %w[denied canceled] }).+ sum(:quantity)++ line_item.quantity.to_i - claimed+ end+def build_claim@claim = order.claims.new(store: order.store,diff --git a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rbindex 63712bd748..b33a308dd2 100644--- a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb@@ -39,6 +39,21 @@ RSpec.describe 'Spree::Claims workflows' doexpect(result).to be_failureend+ # Each claim can pay out what its units cost, so claiming the same units+ # again would pay for them twice.+ it 'refuses units an earlier claim already covers' do+ Spree::Claims::Create.call(order: order, items: [{ line_item: line_item, quantity: line_item.quantity }])++ expect(create_claim).to be_failure+ end++ it 'frees the units of a denied claim' do+ earlier = Spree::Claims::Create.call(order: order, items: [{ line_item: line_item, quantity: line_item.quantity }]).value+ earlier.update!(status: 'denied')++ expect(create_claim).to be_success+ end+it 'lets a validate handler gate self-service claims' doSpree.hooks.register('claims.create.validate') { |flow| flow.reject!('claims disabled') }