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.

Add cross-order listing endpoints for returns, exchanges and claims

Damian Legawieccfe5108cc9c0
diff --git a/packages/admin-sdk/src/admin-client.ts b/packages/admin-sdk/src/admin-client.ts
index 7b4f08be56..b2e6eadfeb 100644
--- a/packages/admin-sdk/src/admin-client.ts
+++ b/packages/admin-sdk/src/admin-client.ts
@@ -2056,6 +2056,66 @@ export class AdminClient {
this.request<void>('DELETE', `/gift_cards/${id}`, options),
}
+ // ============================================
+ // Post-sale, across all orders (read-only — creating any of these needs an
+ // order, so writes live on client.orders.{returns,exchanges,claims})
+ // ============================================
+
+ readonly returns = {
+ list: (
+ params?: ListParams & Record<string, unknown>,
+ options?: RequestOptions,
+ ): Promise<PaginatedResponse<Return>> =>
+ this.request<PaginatedResponse<Return>>('GET', '/returns', {
+ ...options,
+ params: params ? transformListParams(params) : undefined,
+ }),
+
+ get: (id: string, params?: { expand?: string[] }, options?: RequestOptions): Promise<Return> =>
+ this.request<Return>('GET', `/returns/${id}`, {
+ ...options,
+ params: getParams(params),
+ }),
+ }
+
+ readonly exchanges = {
+ list: (
+ params?: ListParams & Record<string, unknown>,
+ options?: RequestOptions,
+ ): Promise<PaginatedResponse<Exchange>> =>
+ this.request<PaginatedResponse<Exchange>>('GET', '/exchanges', {
+ ...options,
+ params: params ? transformListParams(params) : undefined,
+ }),
+
+ get: (
+ id: string,
+ params?: { expand?: string[] },
+ options?: RequestOptions,
+ ): Promise<Exchange> =>
+ this.request<Exchange>('GET', `/exchanges/${id}`, {
+ ...options,
+ params: getParams(params),
+ }),
+ }
+
+ readonly claims = {
+ list: (
+ params?: ListParams & Record<string, unknown>,
+ options?: RequestOptions,
+ ): Promise<PaginatedResponse<Claim>> =>
+ this.request<PaginatedResponse<Claim>>('GET', '/claims', {
+ ...options,
+ params: params ? transformListParams(params) : undefined,
+ }),
+
+ get: (id: string, params?: { expand?: string[] }, options?: RequestOptions): Promise<Claim> =>
+ this.request<Claim>('GET', `/claims/${id}`, {
+ ...options,
+ params: getParams(params),
+ }),
+ }
+
// ============================================
// Gift card batches (bulk issuance)
// ============================================
diff --git a/spree/api/app/controllers/spree/api/v3/admin/claims_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/claims_controller.rb
new file mode 100644
index 0000000000..60224643a6
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/claims_controller.rb
@@ -0,0 +1,31 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Cross-order listing of claims — "what is awaiting resolution across
+ # the whole store". Read-only by route: opening a claim needs an
+ # order, so creation and the status transitions stay on the nested
+ # Orders::ClaimsController.
+ class ClaimsController < ResourceController
+ # Post-sale records are order data: a key that may read orders may
+ # read these, and nothing here writes.
+ scoped_resource :orders
+
+ protected
+
+ def model_class
+ Spree::Claim
+ end
+
+ def serializer_class
+ Spree.api.admin_claim_serializer
+ end
+
+ def collection_includes
+ [:order, :reason, { claim_line_items: :variant }]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/exchanges_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/exchanges_controller.rb
new file mode 100644
index 0000000000..2bfe03769c
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/exchanges_controller.rb
@@ -0,0 +1,31 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Cross-order listing of exchanges — "what is awaiting fulfillment
+ # across the whole store". Read-only by route: opening an exchange
+ # needs an order, so creation and the status transitions stay on the
+ # nested Orders::ExchangesController.
+ class ExchangesController < ResourceController
+ # Post-sale records are order data: a key that may read orders may
+ # read these, and nothing here writes.
+ scoped_resource :orders
+
+ protected
+
+ def model_class
+ Spree::Exchange
+ end
+
+ def serializer_class
+ Spree.api.admin_exchange_serializer
+ end
+
+ def collection_includes
+ [:order, :reason, :stock_location, { exchange_line_items: [:original_variant, :new_variant] }]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/returns_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/returns_controller.rb
new file mode 100644
index 0000000000..6e07e281b9
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/returns_controller.rb
@@ -0,0 +1,31 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Cross-order listing of returns — "what is awaiting receipt across
+ # the whole store". Read-only by route: opening a return needs an
+ # order, so creation and the status transitions stay on the nested
+ # Orders::ReturnsController.
+ class ReturnsController < ResourceController
+ # Post-sale records are order data: a key that may read orders may
+ # read these, and nothing here writes.
+ scoped_resource :orders
+
+ protected
+
+ def model_class
+ Spree::Return
+ end
+
+ def serializer_class
+ Spree.api.admin_return_serializer
+ end
+
+ def collection_includes
+ [:order, :reason, :stock_location, { return_line_items: :variant }]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rb
index 257d8ac678..85e1be324d 100644
--- a/spree/api/config/routes.rb
+++ b/spree/api/config/routes.rb
@@ -385,6 +385,12 @@ Spree::Core::Engine.add_routes do
resources :gift_cards
resources :gift_card_batches, only: [:index, :show, :create]
+ # Post-sale, across all orders. Read-only — creating any of these
+ # needs an order, so writes live under /orders/:order_id/...
+ resources :returns, only: [:index, :show]
+ resources :exchanges, only: [:index, :show]
+ resources :claims, only: [:index, :show]
+
# Channels (per-store distribution surfaces)
resources :channels do
member do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/returns_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/returns_controller_spec.rb
new file mode 100644
index 0000000000..2e3e88594f
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/returns_controller_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::ReturnsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:order) { create(:shipped_order, store: store) }
+ let!(:pending_return) { create(:return, store: store, order: order) }
+ let!(:received) { create(:received_return, store: store) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #index' do
+ it 'lists returns across every order in the store' do
+ get :index, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].map { |r| r['id'] }).
+ to contain_exactly(pending_return.prefixed_id, received.prefixed_id)
+ end
+
+ # The whole point of the cross-order list: "what is awaiting receipt".
+ it 'filters by status' do
+ get :index, params: { q: { status_eq: 'received' } }, as: :json
+
+ expect(json_response['data'].map { |r| r['id'] }).to eq([received.prefixed_id])
+ end
+
+ it 'does not leak another store' do
+ other = create(:return, store: create(:store))
+
+ get :index, as: :json
+
+ expect(json_response['data'].map { |r| r['id'] }).not_to include(other.prefixed_id)
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns one record' do
+ get :show, params: { id: pending_return.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['number']).to eq(pending_return.number)
+ end
+ end
+
+ it 'does not expose a create route' do
+ expect { post :create, params: {}, as: :json }.to raise_error(ActionController::UrlGenerationError)
+ end
+end