Load names from line item where available.

I'm not sure why Arel complained about SQL in the SELECT clause (fields), but not the GROUP BY clause (key_fields). Maybe because it's not susceptible to user injection.

I'm also not sure why I couldn't use the aliases defined in SELECT in the GROUP BY clause, but hey this seems to work.
This commit is contained in:
David Cook
2026-01-05 15:38:06 +11:00
committed by Gaetan Craig-Riou
parent b98552003c
commit afdb044386
2 changed files with 14 additions and 12 deletions

View File

@@ -14,7 +14,7 @@ class AffiliateSalesQuery
},
)
.group(key_fields)
.pluck(fields)
.pluck(Arel.sql(fields))
end
# Create a hash with labels for an array of data points:
@@ -48,8 +48,8 @@ class AffiliateSalesQuery
def fields
<<~SQL.squish
spree_products.name AS product_name,
spree_variants.display_name AS unit_name,
COALESCE(spree_line_items.product_name, spree_products.name) AS product_name,
COALESCE(spree_line_items.variant_name, spree_variants.display_name) AS unit_name,
spree_variants.variant_unit AS unit_type,
spree_variants.unit_value AS units,
spree_variants.unit_presentation,
@@ -65,8 +65,8 @@ class AffiliateSalesQuery
def key_fields
<<~SQL.squish
product_name,
unit_name,
COALESCE(spree_line_items.product_name, spree_products.name),
COALESCE(spree_line_items.variant_name, spree_variants.display_name),
unit_type,
units,
spree_variants.unit_presentation,

View File

@@ -63,7 +63,7 @@ RSpec.describe AffiliateSalesQuery do
expect(labelled_row).to include(
product_name: "Tomatoes",
unit_name: "Tomatoes - Roma",
unit_name: "Tomatoes - Roma (1kg)",
unit_type: "weight",
units: 1000.to_f,
unit_presentation: "1kg",
@@ -78,20 +78,22 @@ RSpec.describe AffiliateSalesQuery do
it "returns data stored in line item at time of order" do
# Records are updated after the orders are created
product.update! name: "Tomatoes Updated"
variant1.update! display_name: "Tomatoes - Updated Roma", price: 11
product.update! name: "Tommy toes"
variant1.update! display_name: "Tommy toes - Roma", price: 11
labelled_row = query.label_row(query.data(order1.distributor).first)
pending "#13220 store product and variant names"
expect(labelled_row).to include(
product_name: "Tomatoes",
unit_name: "Tomatoes - Roma",
unit_name: "Tomatoes - Roma (1kg)",
price: 10.to_d, # this price is hardcoded in the line item factory.
)
end
it "returns data from variant if line item doesn't have it" do
# Old line item records (before migration 20250713110052) don't have these values stored
order1.line_items.first.update! product_name: nil, variant_name: nil
labelled_row = query.label_row(query.data(order1.distributor).first)
expect(labelled_row).to include(
@@ -131,12 +133,12 @@ RSpec.describe AffiliateSalesQuery do
expect(labelled_data).to include a_hash_including(
product_name: "Tomatoes",
unit_name: "Tomatoes - Roma",
unit_name: "Tomatoes - Roma (1kg)",
quantity_sold: 1,
)
expect(labelled_data).to include a_hash_including(
product_name: "Tomatoes",
unit_name: "Tomatoes - Cherry",
unit_name: "Tomatoes - Cherry (500g)",
quantity_sold: 1,
units: 500,
unit_presentation: "500g",