Skip to content

Commit

Permalink
Fixes 422 error due to bad sql building
Browse files Browse the repository at this point in the history
- first part of query use supplier_properties parameter, but not
  second part, that can leads to mismatch between the 2 parts.
- spec tests now with an enterprise property.
  • Loading branch information
cyrillefr committed Jul 23, 2024
1 parent dfea0cd commit 91f804f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 15 additions & 7 deletions app/services/order_cycles/distributed_products_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def products_relation_incl_supplier_properties

query = supplier_property_join(query)

query.order(Arel.sql(order))
query.order(Arel.sql(order(supplier_properties: true)))
end

def variants_relation
Expand Down Expand Up @@ -71,24 +71,32 @@ def sorting
distributor.preferred_shopfront_product_sorting_method
end

def sorting_by_producer?
sorting == "by_producer" &&
distributor.preferred_shopfront_producer_order.present?
end

def sorting_by_category?
sorting == "by_category" &&
distributor.preferred_shopfront_taxon_order.present?
end

def supplier_property_join(query)
query.joins("
JOIN enterprises ON enterprises.id = first_variant.supplier_id
LEFT OUTER JOIN producer_properties ON producer_properties.producer_id = enterprises.id
")
end

def order
if distributor.preferred_shopfront_product_sorting_method == "by_producer" &&
distributor.preferred_shopfront_producer_order.present?
order_by_producer = distributor
def order(supplier_properties: false)
if sorting_by_producer?
order_by_producer = distributor || supplier_properties
.preferred_shopfront_producer_order
.split(",").map { |id| "first_variant.supplier_id=#{id} DESC" }
.join(", ")

"#{order_by_producer}, spree_products.name ASC, spree_products.id ASC"
elsif distributor.preferred_shopfront_product_sorting_method == "by_category" &&
distributor.preferred_shopfront_taxon_order.present?
elsif sorting_by_category?
order_by_category = distributor
.preferred_shopfront_taxon_order
.split(",").map { |id| "first_variant.primary_taxon_id=#{id} DESC" }
Expand Down
1 change: 1 addition & 0 deletions spec/system/consumer/shopping/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@

context "product taxonomies" do
before do
supplier.set_producer_property("certified", "yes")
distributor.preferred_shopfront_product_sorting_method = "by_category"
distributor.preferred_shopfront_taxon_order = taxon.id.to_s
visit shop_path
Expand Down

0 comments on commit 91f804f

Please sign in to comment.