Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Rewrite SQL queries to be more performant

Damian Legawiecb9ae97745280
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 2c2f9220fb..8ef06f2bb6 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -817,14 +817,22 @@ module Spree
Spree::CouponCode.find_by(order: self, promotion: promotions).try(:code) || promotions.pluck(:code).compact.first
end
+ # Returns the valid promotions for the order
+ # @return [Array<Spree::OrderPromotion>]
def valid_promotions
- order_promotions.where(promotion_id: valid_promotion_ids).uniq(&:promotion_id)
+ order_promotions.includes(:promotion).where(promotion_id: valid_promotion_ids).uniq(&:promotion_id)
end
+ # Returns the IDs of the valid promotions for the order
+ # @return [Array<Integer>]
def valid_promotion_ids
- all_adjustments.eligible.nonzero.promotion.map { |a| a.source.promotion_id }.uniq
+ all_adjustments.eligible.nonzero.promotion.promotion.eligible.nonzero.promotion.
+ joins("INNER JOIN #{Spree::PromotionAction.table_name} ON #{Spree::PromotionAction.table_name}.id = #{Spree::Adjustment.table_name}.source_id").
+ pluck("#{Spree::PromotionAction.table_name}.promotion_id").compact.uniq
end
+ # Returns the valid coupon promotions for the order
+ # @return [Array<Spree::Promotion>]
def valid_coupon_promotions
promotions.
where(id: valid_promotion_ids).
@@ -832,7 +840,7 @@ module Spree
end
# Returns item and whole order discount amount for Order
- # without Shipment disccounts (eg. Free Shipping)
+ # without Shipment discounts (eg. Free Shipping)
# @return [BigDecimal]
def cart_promo_total
all_adjustments.eligible.nonzero.promotion.
diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rb
index 215e2b5b7f..ea8629eee7 100644
--- a/core/app/models/spree/promotion_handler/coupon.rb
+++ b/core/app/models/spree/promotion_handler/coupon.rb
@@ -209,7 +209,7 @@ module Spree
end
def handle_coupon_code(discount, coupon_code)
- discount.source.promotion.coupon_codes.unused.find_by(code: coupon_code)&.apply_order!(order)
+ Spree::CouponCode.unused.find_by(promotion_id: discount.cached_source.promotion_id, code: coupon_code)&.apply_order!(order)
end
def load_gift_card_code
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index cf5ce7156d..c62dfbcd05 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -209,7 +209,8 @@ module Spree
#
# @return [Boolean]
def with_free_shipping_promotion?
- adjustments.promotion.includes(:source).any? { |p| p.source.respond_to?(:free_shipping?) && p.source.free_shipping? }
+ adjustments.promotion.joins("INNER JOIN #{Spree::PromotionAction.table_name} ON #{Spree::PromotionAction.table_name}.id = #{Spree::Adjustment.table_name}.source_id").
+ where("#{Spree::PromotionAction.table_name}.type = 'Spree::Promotion::Actions::FreeShipping'").exists?
end
def finalize!
diff --git a/core/app/services/spree/coupon_codes/coupon_codes_handler.rb b/core/app/services/spree/coupon_codes/coupon_codes_handler.rb
index cc449f1932..f2b3d212e1 100644
--- a/core/app/services/spree/coupon_codes/coupon_codes_handler.rb
+++ b/core/app/services/spree/coupon_codes/coupon_codes_handler.rb
@@ -19,7 +19,8 @@ module Spree
private
def find_current_promotions_ids
- order.all_adjustments.promotion.eligible.map { |a| a.source.try(:promotion_id) }.compact
+ order.all_adjustments.promotion.eligible.joins("INNER JOIN #{Spree::PromotionAction.table_name} ON #{Spree::PromotionAction.table_name}.id = #{Spree::Adjustment.table_name}.source_id").
+ pluck("#{Spree::PromotionAction.table_name}.promotion_id").compact.uniq
end
def use_all_current(promotion_ids)