From afdb04438646fa88a35c7ad27abbe4ee47d10615 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 5 Jan 2026 15:38:06 +1100 Subject: [PATCH] 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. --- .../app/services/affiliate_sales_query.rb | 10 +++++----- .../spec/services/affiliate_sales_query_spec.rb | 16 +++++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/engines/dfc_provider/app/services/affiliate_sales_query.rb b/engines/dfc_provider/app/services/affiliate_sales_query.rb index 155243745a..e3dd760979 100644 --- a/engines/dfc_provider/app/services/affiliate_sales_query.rb +++ b/engines/dfc_provider/app/services/affiliate_sales_query.rb @@ -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, diff --git a/engines/dfc_provider/spec/services/affiliate_sales_query_spec.rb b/engines/dfc_provider/spec/services/affiliate_sales_query_spec.rb index e9598c1332..047cbcab9c 100644 --- a/engines/dfc_provider/spec/services/affiliate_sales_query_spec.rb +++ b/engines/dfc_provider/spec/services/affiliate_sales_query_spec.rb @@ -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",