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
Rewrite SQL queries to be more performant
Damian Legawiecb9ae97745280
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 2c2f9220fb..8ef06f2bb6 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -817,14 +817,22 @@ module SpreeSpree::CouponCode.find_by(order: self, promotion: promotions).try(:code) || promotions.pluck(:code).compact.firstend+ # 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.uniqend+ # Returns the valid coupon promotions for the order+ # @return [Array<Spree::Promotion>]def valid_coupon_promotionspromotions.where(id: valid_promotion_ids).@@ -832,7 +840,7 @@ module Spreeend# 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_totalall_adjustments.eligible.nonzero.promotion.diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rbindex 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 Spreeenddef 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)enddef load_gift_card_codediff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex 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?enddef finalize!diff --git a/core/app/services/spree/coupon_codes/coupon_codes_handler.rb b/core/app/services/spree/coupon_codes/coupon_codes_handler.rbindex 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 Spreeprivatedef 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.uniqenddef use_all_current(promotion_ids)