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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import { AMOUNT_BASED_CALCULATORS, hasCurrencyAmount, i18n } from '@spree/dashboard-core'
import { z } from 'zod/v4'
/**
* One eligibility rule as held in form state. `id` is absent for rules added
* in this editing session. `takes_products` comes from the rule-type discovery
* endpoint and decides whether `product_ids` is part of this rule's payload —
* it is display-only state, stripped before send.
*/
export const deliveryMethodRuleSchema = z.object({
id: z.string().optional(),
type: z.string(),
preferences: z.record(z.string(), z.unknown()),
product_ids: z.array(z.string()),
takes_products: z.boolean(),
})
export type DeliveryMethodRuleDraft = z.infer<typeof deliveryMethodRuleSchema>
/**
* One carrier service row: a service the method offers, with an optional
* customer-facing label and markup override. No rows on a provider-priced
* method means "offer everything the carrier returns".
*/
export const deliveryMethodServiceSchema = z.object({
id: z.string().optional(),
carrier: z.string().min(1),
service: z.string().min(1),
label: z.string().optional(),
markup_flat: z.string().optional(),
markup_percent: z.string().optional(),
})
export type DeliveryMethodServiceDraft = z.infer<typeof deliveryMethodServiceSchema>
const deliveryMethodFormObject = z.object({
name: z.string().min(1),
admin_name: z.string().optional(),
code: z.string().optional(),
fulfillment_provider: z.string(),
// Empty string means the built-in Internal provider (calculator-priced).
rate_provider: z.string(),
storefront_visible: z.boolean(),
// Marketplace only: whether a seller's packages may be quoted by this
// method (docs/plans/6.0-multi-vendor-marketplace.md, Decision 13).
available_to_sellers: z.boolean(),
tracking_url: z.string().optional(),
estimated_transit_business_days_min: z.string().optional(),
estimated_transit_business_days_max: z.string().optional(),
tax_category_id: z.string().optional(),
calculator: z.object({
type: z.string().optional(),
preferences: z.record(z.string(), z.unknown()).optional(),
}),
// Empty string means no destination restriction — the method serves
// everywhere its profile reaches.
delivery_zone_id: z.string(),
stock_location_ids: z.array(z.string()),
rules: z.array(deliveryMethodRuleSchema),
markup_flat: z.string().optional(),
markup_percent: z.string().optional(),
// Held as strings like the other decimals: what the merchant typed reaches
// the server intact, which is what parses it.
services: z.array(deliveryMethodServiceSchema),
})
export type DeliveryMethodFormValues = z.infer<typeof deliveryMethodFormObject>
/**
* The delivery method form's schema. `carrierPricedProviders` names the rate
* providers that quote their own rates (`''` when the default one does): their
* methods hide the calculator, so its amounts are not asked for.
*/
export function deliveryMethodFormSchema(carrierPricedProviders: readonly string[] = []) {
return deliveryMethodFormObject.superRefine((values, ctx) => {
if (carrierPricedProviders.includes(values.rate_provider)) return
// A calculator-priced method with no amount in any currency would never be offered.
const { type, preferences } = values.calculator
if (!AMOUNT_BASED_CALCULATORS.includes(type ?? '')) return
if (hasCurrencyAmount(preferences)) return
ctx.addIssue({
code: 'custom',
path: ['calculator', 'preferences'],
message: i18n.t('admin.currency_amounts.required'),
})
})
}
export const DELIVERY_METHOD_DEFAULTS: DeliveryMethodFormValues = {
name: '',
admin_name: '',
code: '',
fulfillment_provider: 'manual',
rate_provider: '',
storefront_visible: true,
available_to_sellers: false,
tracking_url: '',
estimated_transit_business_days_min: '',
estimated_transit_business_days_max: '',
tax_category_id: '',
calculator: { type: '', preferences: {} },
delivery_zone_id: '',
stock_location_ids: [],
rules: [],
markup_flat: '',
markup_percent: '',
services: [],
}
/** The `calculator` part of a write, or nothing when the form names neither a type nor a setting. */
function calculatorParams({ type, preferences }: DeliveryMethodFormValues['calculator']) {
const hasPreferences = !!preferences && Object.keys(preferences).length > 0
if (!type && !hasPreferences) return {}
return {
calculator: { type: type || undefined, preferences: hasPreferences ? preferences : undefined },
}
}
export function deliveryMethodValuesToParams(values: DeliveryMethodFormValues) {
return {
name: values.name,
admin_name: values.admin_name || null,
code: values.code || null,
fulfillment_provider: values.fulfillment_provider,
rate_provider: values.rate_provider || null,
storefront_visible: values.storefront_visible,
available_to_sellers: values.available_to_sellers,
tracking_url: values.tracking_url || null,
estimated_transit_business_days_min: values.estimated_transit_business_days_min
? Number(values.estimated_transit_business_days_min)
: null,
estimated_transit_business_days_max: values.estimated_transit_business_days_max
? Number(values.estimated_transit_business_days_max)
: null,
tax_category_id: values.tax_category_id || null,
...calculatorParams(values.calculator),
delivery_zone_id: values.delivery_zone_id || null,
stock_location_ids: values.stock_location_ids,
// Rules ride along with the method so one request saves the whole page.
// Omitting `id` marks a rule as new; dropping one from the array deletes
// it. `product_ids` is always sent for association-backed rules — omitting
// an emptied array would read as "leave unchanged" and silently keep the
// exclusions the merchant just removed.
rules: values.rules.map((rule) => ({
...(rule.id ? { id: rule.id } : {}),
type: rule.type,
preferences: rule.preferences,
...(rule.takes_products ? { product_ids: rule.product_ids } : {}),
})),
markup_flat: values.markup_flat || null,
markup_percent: values.markup_percent || null,
// Service rows follow the rules convention: the array replaces the full
// set, omitted rows are removed, and an empty array clears them (back to
// "offer every service").
services: values.services.map((row) => ({
...(row.id ? { id: row.id } : {}),
carrier: row.carrier,
service: row.service,
label: row.label || null,
markup_flat: row.markup_flat || null,
markup_percent: row.markup_percent || null,
})),
}
}