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 feedback on the delivery provider PR

Damian Legawiec6e932ed06797
diff --git a/docs/plans/6.0-delivery-rate-provider.md b/docs/plans/6.0-delivery-rate-provider.md
index 49aa90926d..38776d7ada 100644
--- a/docs/plans/6.0-delivery-rate-provider.md
+++ b/docs/plans/6.0-delivery-rate-provider.md
@@ -262,6 +262,15 @@ Multiple DeliveryMethods can share one external provider, and a single API call
## Design Details
+> **The snippets below are the original design sketch, kept for the reasoning
+> that produced it.** Where they disagree with what shipped, the
+> "Implementation status" section above is authoritative: the value object is
+> `Spree::DeliveryRateProvider::Estimate` (not `DeliveryRateEstimate`), it
+> carries no `estimated_transit_days`, and `DeliveryRate` uses a plain
+> `metadata` column rather than the `Spree::Metadata` concern. Provider
+> authors should follow
+> `docs/v6/developer/how-to/custom-delivery-rate-provider.mdx`.
+
### Provider base class
```ruby
diff --git a/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx b/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx
index 31be6ed454..8857f98637 100644
--- a/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx
+++ b/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx
@@ -73,6 +73,10 @@ module SpreeAcmeCarrier
estimated_delivery_date: quote.delivery_date,
metadata: { quote_id: quote.id }
)
+ rescue AcmeCarrier::Error => e
+ # Never raise into checkout: report and hide this method instead.
+ Rails.error.report(e, context: { delivery_method_id: delivery_method.id }, source: 'acme.rating')
+ nil
end
private
diff --git a/packages/admin-sdk/src/types/index.ts b/packages/admin-sdk/src/types/index.ts
index fc5d999ced..0c44ede5dd 100644
--- a/packages/admin-sdk/src/types/index.ts
+++ b/packages/admin-sdk/src/types/index.ts
@@ -7,7 +7,8 @@ export type {
PaginatedResponse,
PaginationMeta,
} from '@spree/sdk-core'
-// Hand-written discovery-endpoint types (controller-shaped, not generated)
+// Hand-written discovery-endpoint types (controller-shaped, not generated):
+// entries returned by the delivery-method provider discovery endpoints.
export type { DeliveryRateProviderOption, FulfillmentProviderOption } from './fulfillment-providers'
// Admin-specific generated types
export type { default as Address } from './generated/Address'
diff --git a/packages/dashboard/src/hooks/use-delivery-methods.ts b/packages/dashboard/src/hooks/use-delivery-methods.ts
index 6e8f8eb195..4353d1669b 100644
--- a/packages/dashboard/src/hooks/use-delivery-methods.ts
+++ b/packages/dashboard/src/hooks/use-delivery-methods.ts
@@ -32,6 +32,7 @@ export function useFulfillmentProviders() {
})
}
+/** Rate providers available to this store (carrier providers appear once their integration is connected), plus the default. */
export function useDeliveryRateProviders() {
return useQuery({
queryKey: useResourceKey('delivery-methods', 'rate-providers'),
diff --git a/packages/dashboard/src/hooks/use-integrations.ts b/packages/dashboard/src/hooks/use-integrations.ts
index 25c5f2db06..e81e6954b9 100644
--- a/packages/dashboard/src/hooks/use-integrations.ts
+++ b/packages/dashboard/src/hooks/use-integrations.ts
@@ -7,6 +7,7 @@ import { adminClient, useResourceKey, useResourceMutation } from '@spree/dashboa
import { useMutation, useQuery } from '@tanstack/react-query'
import i18n from 'i18next'
+/** Integrations connected to the current store, with secrets masked. */
export function useIntegrations() {
return useQuery({
queryKey: useResourceKey('integrations'),
@@ -14,6 +15,7 @@ export function useIntegrations() {
})
}
+/** Registered integration types and their configuration schemas — static registry discovery, cached long. */
export function useIntegrationTypes() {
return useQuery({
queryKey: useResourceKey('integrations', 'types'),
@@ -22,6 +24,7 @@ export function useIntegrationTypes() {
})
}
+/** Connects an integration type to the store. Activating verifies the connection server-side. */
export function useCreateIntegration() {
return useResourceMutation<Integration, Error, IntegrationCreateParams>({
mutationFn: (params) => adminClient.integrations.create(params),
@@ -31,6 +34,7 @@ export function useCreateIntegration() {
})
}
+/** Updates credentials or activation. Masked secrets round-trip safely. */
export function useUpdateIntegration(id: string) {
return useResourceMutation<Integration, Error, IntegrationUpdateParams>({
mutationFn: (params) => adminClient.integrations.update(id, params),
@@ -40,6 +44,7 @@ export function useUpdateIntegration(id: string) {
})
}
+/** Disconnects an integration, deleting its stored credentials. */
export function useDeleteIntegration() {
return useResourceMutation<void, Error, string>({
mutationFn: (id) => adminClient.integrations.delete(id),
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/settings/delivery-methods.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/settings/delivery-methods.tsx
index aeb8596045..6278770f13 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/settings/delivery-methods.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/settings/delivery-methods.tsx
@@ -487,14 +487,12 @@ function ProviderSelectField({
label,
help,
options,
- fallbackValue,
}: {
form: UseFormReturn<DeliveryMethodFormValues>
name: 'fulfillment_provider' | 'rate_provider'
label: string
help: string
options: { value: string; label: string }[]
- fallbackValue?: string
}) {
return (
<Field>
@@ -503,11 +501,7 @@ function ProviderSelectField({
name={name}
control={form.control}
render={({ field }) => (
- <Select
- items={options}
- value={field.value || fallbackValue || ''}
- onValueChange={field.onChange}
- >
+ <Select items={options} value={field.value || ''} onValueChange={field.onChange}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
@@ -568,6 +562,18 @@ function DeliveryMethodFormFields({ form }: { form: UseFormReturn<DeliveryMethod
}))
const defaultRateProvider = rateProviders?.default ?? ''
+
+ // The select displayed the server default while submitting a blank value —
+ // harmless (blank resolves to Internal) but the shown and saved values
+ // disagreed. Seed it once the default is known, leaving a touched field or
+ // a loaded record alone.
+ const rateProvider = form.watch('rate_provider')
+ const rateProviderDirty = !!form.formState.dirtyFields.rate_provider
+ useEffect(() => {
+ if (!defaultRateProvider || rateProvider || rateProviderDirty) return
+
+ form.setValue('rate_provider', defaultRateProvider)
+ }, [defaultRateProvider, rateProvider, rateProviderDirty, form])
const rateProviderOptions = (rateProviders?.data ?? []).map((provider) => ({
value: provider.type,
label: provider.name,
@@ -678,7 +684,6 @@ function DeliveryMethodFormFields({ form }: { form: UseFormReturn<DeliveryMethod
label={t('admin.fields.delivery_method.rate_provider.label')}
help={t('admin.fields.delivery_method.rate_provider.help')}
options={rateProviderOptions}
- fallbackValue={defaultRateProvider}
/>
)}
diff --git a/spree/core/app/models/spree/delivery_method.rb b/spree/core/app/models/spree/delivery_method.rb
index 4df7529990..64cb460a52 100644
--- a/spree/core/app/models/spree/delivery_method.rb
+++ b/spree/core/app/models/spree/delivery_method.rb
@@ -183,9 +183,13 @@ module Spree
@rate_provider_instance ||= rate_provider_class.new(self)
end
+ # Falls back to the default when the stored provider no longer resolves —
+ # a gem can be uninstalled while its rows remain, and raising here would
+ # surface as a checkout error rather than a missing carrier option.
+ #
# @return [Class]
def rate_provider_class
- (rate_provider.presence || DEFAULT_RATE_PROVIDER).constantize
+ (rate_provider.presence || DEFAULT_RATE_PROVIDER).safe_constantize || DEFAULT_RATE_PROVIDER.constantize
end
diff --git a/spree/core/db/migrate/20260806130001_add_unique_store_type_index_to_spree_integrations.rb b/spree/core/db/migrate/20260806130001_add_unique_store_type_index_to_spree_integrations.rb
index 6aaf83d87c..873e5953c0 100644
--- a/spree/core/db/migrate/20260806130001_add_unique_store_type_index_to_spree_integrations.rb
+++ b/spree/core/db/migrate/20260806130001_add_unique_store_type_index_to_spree_integrations.rb
@@ -1,7 +1,24 @@
class AddUniqueStoreTypeIndexToSpreeIntegrations < ActiveRecord::Migration[7.2]
- def change
- # Backs the existing one-integration-per-(store, type) model validation.
+ def up
+ # The model has always validated one integration per (store, type), but
+ # without a backing index a race could have persisted duplicates. Keep the
+ # oldest of each group so the index can be created.
+ # Raw SQL, not the model — a migration must not depend on application
+ # code that may not load at this revision.
+ execute(<<~SQL)
+ DELETE FROM spree_integrations
+ WHERE id NOT IN (
+ SELECT keeper FROM (
+ SELECT MIN(id) AS keeper FROM spree_integrations GROUP BY store_id, type
+ ) AS keepers
+ )
+ SQL
+
add_index :spree_integrations, [:store_id, :type], unique: true,
name: 'index_spree_integrations_on_store_id_and_type', if_not_exists: true
end
+
+ def down
+ remove_index :spree_integrations, name: 'index_spree_integrations_on_store_id_and_type', if_exists: true
+ end
end
diff --git a/spree/core/spec/models/spree/delivery_method_spec.rb b/spree/core/spec/models/spree/delivery_method_spec.rb
index c9316cc0e2..c5684fd5c5 100644
--- a/spree/core/spec/models/spree/delivery_method_spec.rb
+++ b/spree/core/spec/models/spree/delivery_method_spec.rb
@@ -210,6 +210,14 @@ describe Spree::DeliveryMethod, type: :model do
expect(delivery_method.reload).to be_valid
end
+ # Uninstalling a provider gem leaves its name on existing rows; quoting
+ # must fall back rather than raise inside checkout.
+ it 'falls back to the default when the stored provider no longer resolves' do
+ delivery_method.update_columns(rate_provider: 'GoneAwayProvider')
+
+ expect(delivery_method.reload.rate_provider_instance).to be_a(Spree::DeliveryRateProvider::Internal)
+ end
+
# The admin picker filters on availability, but a direct API write must
# not save a provider whose integration isn't connected.
it 'rejects a registered provider that is unavailable for the store' do