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.

Order digital assets by their own table when listing

Damian Legawiece6780514381d
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
index 09d0bc36f9..e88c07ad61 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
@@ -45,6 +45,16 @@ module Spree
:digital_assets
end
+ # The parent association reaches assets through `variants`, which
+ # carries an ORDER BY on the variants table. Combined with the
+ # collection's DISTINCT, PostgreSQL rejects the query outright
+ # (ordering columns must appear in the select list), so order by
+ # the assets' own table instead. SQLite accepts either form, so
+ # this only fails on a real deployment.
+ def scope
+ super.reorder(created_at: :asc)
+ end
+
def scope_includes
[:variant, { attachment_attachment: :blob }]
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
index dd3f5e9c5b..54a6bf478c 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
@@ -36,6 +36,20 @@ RSpec.describe Spree::Api::V3::Admin::Products::DigitalAssetsController, type: :
expect(json_response['data'].map { |a| a['id'] }).not_to include(other.prefixed_id)
end
+
+ # Assets reach the product through `variants`, which orders by the variants
+ # table — combined with the collection's DISTINCT that is invalid on
+ # PostgreSQL, so the listing must order by the assets' own table. Asserting
+ # the order keeps this honest on SQLite too, where the bad query still runs.
+ it 'returns the assets oldest first, across variants' do
+ second_variant = create(:variant, product: product)
+ newer = create(:digital_asset, variant: second_variant, created_at: 1.hour.from_now)
+
+ get :index, params: { product_id: product.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].map { |a| a['id'] }).to eq([digital_asset.prefixed_id, newer.prefixed_id])
+ end
end
describe 'POST #create' do