Class: Spree::Commissions::ResolveRate
- Inherits:
-
Object
- Object
- Spree::Commissions::ResolveRate
- Includes:
- ServiceModule::Base
- Defined in:
- app/services/spree/commissions/resolve_rate.rb
Overview
Picks the commission rate that governs one line item.
Walks the store's enabled rates in list order and takes the first whose targeting admits the sale. Precedence is therefore the order an operator sees in the table, not a hardcoded product-beats-category-beats-seller ladder: seeded rates arrive in the conventional order, so a marketplace that never touches it still gets the expected answer, and one that wants a different answer drags a row.
Returns nil when nothing matches, which is a real answer — a marketplace with no rate for a sale charges no commission, rather than falling back to some default the operator never configured.
Swap through Spree.commissions_resolve_rate_service.
Class Method Summary collapse
-
.ancestors_of(categories) ⇒ Hash{Integer=>Array<Spree::Category>}
Every category above each of the given ones, in one query per store rather than one per category.
-
.candidates_for(store) ⇒ Array<Spree::CommissionRate>
The store's rates in resolution order, with rules preloaded — matching reads every rule of every candidate, so without this each rate costs a query per line item.
-
.categories_for(products) ⇒ Hash{Integer=>Array<Spree::Category>}
Each product's categories together with their ancestors, keyed by product id.
Instance Method Summary collapse
Methods included from ServiceModule::Base
Class Method Details
.ancestors_of(categories) ⇒ Hash{Integer=>Array<Spree::Category>}
Every category above each of the given ones, in one query per store rather than one per category. Nested sets make an ancestor a plain range comparison, so the whole set can be asked for at once.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/services/spree/commissions/resolve_rate.rb', line 90 def self.ancestors_of(categories) return {} if categories.empty? table = Spree::Category.arel_table # Kept per store rather than flattened into one pool: bounds are only # meaningful within the tree that assigned them, so a category from # another store can enclose these ones by coincidence — and a rate # targeting it would then match a sale it has nothing to do with. pool_by_store = categories.group_by(&:store_id).transform_values do |group| reaching = group.map { |c| table[:lft].lteq(c.lft).and(table[:rgt].gteq(c.rgt)) }.reduce(:or) Spree::Category.where(store_id: group.first.store_id).where(reaching).to_a end categories.to_h do |category| ancestors = pool_by_store.fetch(category.store_id, []).select do |node| node.lft <= category.lft && node.rgt >= category.rgt end [category.id, ancestors] end end |
.candidates_for(store) ⇒ Array<Spree::CommissionRate>
The store's rates in resolution order, with rules preloaded — matching reads every rule of every candidate, so without this each rate costs a query per line item.
58 59 60 |
# File 'app/services/spree/commissions/resolve_rate.rb', line 58 def self.candidates_for(store) store.commission_rates.enabled.ordered.includes(:commission_rules).to_a end |
.categories_for(products) ⇒ Hash{Integer=>Array<Spree::Category>}
Each product's categories together with their ancestors, keyed by product id.
Resolved for a whole set of products at once because walking a nested set is a query per category, and this runs inside checkout: asking per line item would cost a query for every category of every item in the basket, most of them the same ones over and over.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/services/spree/commissions/resolve_rate.rb', line 72 def self.categories_for(products) products = products.compact.uniq return {} if products.empty? by_product = products.to_h { |product| [product.id, product.categories.to_a] } ancestors = ancestors_of(by_product.values.flatten.uniq) by_product.transform_values do |categories| categories.flat_map { |category| ancestors.fetch(category.id, [category]) }.uniq end end |
Instance Method Details
#call(line_item:, seller:, store:, currency: nil, rates: nil, categories: nil) ⇒ Spree::CommissionRate?
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/spree/commissions/resolve_rate.rb', line 33 def call(line_item:, seller:, store:, currency: nil, rates: nil, categories: nil) currency ||= line_item.currency candidates = rates || self.class.candidates_for(store) context = Spree::Commissions::Context.new( seller: seller, order: line_item.order, line_item: line_item, currency: currency, categories: categories && line_item.variant&.product ? categories.fetch(line_item.variant.product.id, []) : nil ) success( candidates.find do |rate| rate.applies_to_currency?(currency) && rate.matches?(context) end ) end |