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.

Address review on the seller orders and products surface

Damian Legawiec7a7348c986e2
diff --git a/packages/seller-dashboard/src/pages/order.tsx b/packages/seller-dashboard/src/pages/order.tsx
index 1c9016755f..c1585a02b0 100644
--- a/packages/seller-dashboard/src/pages/order.tsx
+++ b/packages/seller-dashboard/src/pages/order.tsx
@@ -1,6 +1,5 @@
import {
AddressBlock,
- Badge,
Button,
Card,
CardAction,
@@ -30,7 +29,12 @@ export function OrderPage() {
const confirm = useConfirm()
const orderKey = ['seller', sellerId, 'order', orderId]
- const { data: order, isLoading } = useQuery({
+ const {
+ data: order,
+ isLoading,
+ isError,
+ refetch,
+ } = useQuery({
queryKey: orderKey,
queryFn: () => sellerClient().orders.get(orderId),
})
@@ -50,6 +54,18 @@ export function OrderPage() {
})
if (isLoading) return <CenteredMessage>{t('common.loading')}</CenteredMessage>
+ // A request that failed is not the same as an order that is not there —
+ // reporting the latter sends the seller looking for the wrong cause.
+ if (isError) {
+ return (
+ <CenteredMessage>
+ {t('common.error')}{' '}
+ <Button variant="outline" onClick={() => refetch()}>
+ {t('common.retry')}
+ </Button>
+ </CenteredMessage>
+ )
+ }
if (!order) return <CenteredMessage>{t('orders.not_found')}</CenteredMessage>
async function handleCancel() {
@@ -152,14 +168,18 @@ function FulfillmentCard({ orderId, fulfillment }: { orderId: string; fulfillmen
}),
})
- const shipped = fulfillment.status !== 'unfulfilled'
+ // A canceled parcel never went out and never will, so it is neither
+ // shipped nor still fulfillable. Reading that off "not unfulfilled"
+ // badged it as a success and offered a tracking number for a parcel
+ // nobody is going to send.
+ const fulfillable = fulfillment.status === 'unfulfilled'
return (
<Card>
<CardHeader>
<CardTitle>{fulfillment.number}</CardTitle>
<CardAction>
- <Badge variant={shipped ? 'success' : 'secondary'}>{fulfillment.status}</Badge>
+ <StatusBadge status={fulfillment.status} />
</CardAction>
</CardHeader>
<CardContent className="flex flex-col gap-3">
@@ -176,7 +196,7 @@ function FulfillmentCard({ orderId, fulfillment }: { orderId: string; fulfillmen
</div>
))}
- {shipped ? (
+ {!fulfillable ? (
fulfillment.tracking && (
<p className="text-sm">{t('orders.tracking', { number: fulfillment.tracking })}</p>
)
diff --git a/packages/seller-dashboard/src/pages/product.tsx b/packages/seller-dashboard/src/pages/product.tsx
index 9196192f61..3fca1cdb04 100644
--- a/packages/seller-dashboard/src/pages/product.tsx
+++ b/packages/seller-dashboard/src/pages/product.tsx
@@ -50,7 +50,12 @@ export function ProductPage({ mode }: { mode: 'new' | 'edit' }) {
const navigate = useNavigate()
const queryClient = useQueryClient()
- const { data: product, isLoading } = useQuery({
+ const {
+ data: product,
+ isLoading,
+ isError,
+ refetch,
+ } = useQuery({
queryKey: ['seller', sellerId, 'product', productId],
queryFn: () => sellerClient().products.get(productId as string),
enabled: !!productId,
@@ -83,9 +88,11 @@ export function ProductPage({ mode }: { mode: 'new' | 'edit' }) {
// Only sent when the seller typed one: an empty box means "leave the
// price alone", not "price this at nothing".
if (values.amount.trim()) {
- payload.prices = [
- { amount: values.amount.trim(), currency: product?.price?.currency ?? 'USD' },
- ]
+ // No currency on a new listing: the server prices it in the store's,
+ // which is the only place that knows it. Guessing here can persist a
+ // price in the wrong currency and drop the right one.
+ const currency = product?.price?.currency
+ payload.prices = [{ amount: values.amount.trim(), ...(currency ? { currency } : {}) }]
}
return productId
@@ -119,6 +126,18 @@ export function ProductPage({ mode }: { mode: 'new' | 'edit' }) {
}
if (mode === 'edit' && isLoading) return <CenteredMessage>{t('common.loading')}</CenteredMessage>
+ // Same distinction as the order page: a failed request is not a missing
+ // product, and only one of the two is worth retrying.
+ if (mode === 'edit' && isError) {
+ return (
+ <CenteredMessage>
+ {t('common.error')}{' '}
+ <Button variant="outline" onClick={() => refetch()}>
+ {t('common.retry')}
+ </Button>
+ </CenteredMessage>
+ )
+ }
if (mode === 'edit' && !product)
return <CenteredMessage>{t('products.not_found')}</CenteredMessage>
diff --git a/packages/seller-sdk/src/seller-client.ts b/packages/seller-sdk/src/seller-client.ts
index 39d82ecf25..47b167790f 100644
--- a/packages/seller-sdk/src/seller-client.ts
+++ b/packages/seller-sdk/src/seller-client.ts
@@ -389,7 +389,15 @@ export interface ProductParams {
tags?: string[]
category_ids?: string[]
metadata?: Record<string, unknown>
- prices?: Array<{ amount: string | number; compare_at_amount?: string | number; currency: string }>
+ /**
+ * Omit `currency` and the price is recorded in the store's, which is the
+ * only place that knows it. A seller has no currency of their own to read.
+ */
+ prices?: Array<{
+ amount: string | number
+ compare_at_amount?: string | number
+ currency?: string
+ }>
}
/** What `/seller/me` answers. */
diff --git a/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
index b3118d4fc9..f50e478084 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/orders_controller.rb
@@ -56,12 +56,13 @@ module Spree
%w[index show]
end
- # A checkout still in flight is a draft carrying its cart; it is not
- # yet anybody's order and must not appear in a seller's list.
+ # Only orders that were actually placed. A draft is either a
+ # checkout still in flight or the operator's own working document —
+ # neither is something this seller has sold, and the operator's is
+ # not theirs to read or cancel. The admin branch admits the
+ # cart-less ones deliberately; this branch must not.
def scope
- base = super
-
- base.where(cart_id: nil).or(base.where.not(status: 'draft'))
+ super.where.not(status: 'draft')
end
def collection_includes
diff --git a/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb
index d014a9c399..ec7c2151f0 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb
@@ -32,7 +32,7 @@ module Spree
end
def permitted_params
- params.permit(
+ attrs = params.permit(
:name, :description, :slug, :status,
:meta_title, :meta_description, :meta_keywords,
:product_type_id,
@@ -41,6 +41,23 @@ module Spree
metadata: {},
prices: [:amount, :compare_at_amount, :currency]
)
+
+ default_price_currency(attrs)
+ end
+
+ # A price with no currency is priced in the store's, not in whatever
+ # the client happened to assume. `Variant#prices=` passes the value
+ # through untouched and drops base prices for currencies absent from
+ # the payload, so a wrong guess does not merely add a stray price —
+ # it removes the right one.
+ def default_price_currency(attrs)
+ return attrs if attrs[:prices].blank?
+
+ attrs[:prices] = attrs[:prices].map do |price|
+ price[:currency].present? ? price : price.merge(currency: Spree::Current.currency)
+ end
+
+ attrs
end
def collection_includes
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb
index 0df5b2b6f2..141e96ef81 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders/fulfillments_controller_spec.rb
@@ -50,6 +50,28 @@ RSpec.describe Spree::Api::V3::Seller::Orders::FulfillmentsController, type: :co
expect(fulfillment.tracking).to eq('TRACK-1')
end
+ # A line the parcel does not hold cannot be shipped out of it, even when
+ # another parcel on the same order does hold it — the request is checked
+ # against this fulfillment alone, before anything is split or shipped.
+ it 'refuses a line this parcel does not hold' do
+ other = order.fulfillments.create!(stock_location: fulfillment.stock_location)
+ moved = fulfillment.fulfillment_items.first
+ moved.update!(fulfillment: other)
+
+ patch :fulfill,
+ params: {
+ order_id: order.prefixed_id,
+ id: fulfillment.prefixed_id,
+ items: [{ item_id: moved.line_item.prefixed_id, quantity: moved.quantity }]
+ },
+ as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(fulfillment.reload).not_to be_fulfilled
+ expect(other.reload).not_to be_fulfilled
+ expect(moved.reload.fulfillment_id).to eq(other.id)
+ end
+
# Shipping through another seller's order must be unreachable whatever ids
# are sent, since the fulfillment is only ever found through the order.
it "404s on a fulfillment reached through someone else's order" do
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
index 72bd77c298..979f7f9720 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
@@ -45,6 +45,33 @@ RSpec.describe Spree::Api::V3::Seller::OrdersController, type: :controller do
end
end
+ # A backoffice draft has no cart, so it passes the in-flight filter — but it
+ # is the operator's working document, not something the seller has sold.
+ describe 'operator drafts' do
+ let!(:backoffice_draft) do
+ create(:order, store: store, seller: seller, status: 'draft', cart: nil)
+ end
+
+ it 'are left out of the list' do
+ get :index, as: :json
+
+ expect(json_response['data'].map { |row| row['id'] }).
+ not_to include(backoffice_draft.prefixed_id)
+ end
+
+ it 'cannot be read directly' do
+ get :show, params: { id: backoffice_draft.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+
+ it 'cannot be canceled' do
+ patch :cancel, params: { id: backoffice_draft.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
describe 'GET #show' do
it 'renders what the seller needs to pack the parcel' do
get :show, params: { id: mine.prefixed_id }, as: :json
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb
index 1af1441470..6a07450afc 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb
@@ -77,6 +77,34 @@ RSpec.describe Spree::Api::V3::Seller::ProductsController, type: :controller do
expect(product.seller).to eq(seller)
expect(product.store).to eq(store)
end
+
+ # A seller has no currency of their own to send, and `Variant#prices=`
+ # drops base prices for currencies absent from the payload — so guessing
+ # here would not merely add a stray price, it would remove the right one.
+ it "prices in the store's currency when the payload names none" do
+ # Deliberately not the store factory's USD: a spec asserting USD here
+ # would pass just as well against a hardcoded fallback.
+ allow(Spree::Current).to receive(:currency).and_return('GBP')
+
+ post :create,
+ params: { name: 'Unpriced Lamp', prices: [{ amount: '19.99' }] },
+ as: :json
+
+ expect(response).to have_http_status(:created)
+ price = Spree::Product.find_by(name: 'Unpriced Lamp').default_variant.prices.first
+ expect(price.currency).to eq('GBP')
+ expect(price.amount).to eq(19.99)
+ end
+
+ it 'honours a currency the payload does name' do
+ post :create,
+ params: { name: 'Euro Lamp', prices: [{ amount: '19.99', currency: 'EUR' }] },
+ as: :json
+
+ expect(response).to have_http_status(:created)
+ currencies = Spree::Product.find_by(name: 'Euro Lamp').default_variant.prices.map(&:currency)
+ expect(currencies).to eq(['EUR'])
+ end
end
describe 'PATCH #update' do