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.

2.9 KB
Raw Blame
import type { ChannelCreateParams, ChannelUpdateParams } from '@spree/admin-sdk'
import { STOREFRONT_ACCESSES } from '@spree/core'
import { requiredMessage } from '@spree/dashboard-ui'
import i18n from 'i18next'
import { z } from 'zod/v4'

// Empty string clears the channel-level override → falls back to store.
export const RULES_ORDER_ROUTING_STRATEGY = 'rules'

export const ORDER_ROUTING_STRATEGY_VALUES = ['', RULES_ORDER_ROUTING_STRATEGY] as const

export type OrderRoutingStrategyValue = (typeof ORDER_ROUTING_STRATEGY_VALUES)[number]

// Empty string clears the channel-level override → falls back to store.
export const STOREFRONT_ACCESS_VALUES = ['', ...STOREFRONT_ACCESSES] as const

// Tri-state form representation of the channel's boolean guest_checkout
// override: '' inherits the store value, 'true'/'false' set an explicit value.
export const GUEST_CHECKOUT_VALUES = ['', 'true', 'false'] as const

export const channelFormSchema = z.object({
  name: z.string().min(1, { error: requiredMessage('name') }),
  code: z
    .string()
    .regex(/^[a-z0-9_-]*$/, {
      error: () => i18n.t('admin.pages.channels.validation.code_format'),
    })
    .optional(),
  active: z.boolean(),
  default: z.boolean(),
  order_routing_strategy: z.string(),
  storefront_access: z.enum(STOREFRONT_ACCESS_VALUES),
  guest_checkout: z.string(),
  // Empty means shoppers outside any assigned catalog see every product
  // published on the channel.
  default_catalog_id: z.string(),
  // Empty means every location of the store serves this channel, which is how
  // the API reads an empty array.
  stock_location_ids: z.array(z.string()),
})

export type ChannelFormValues = z.infer<typeof channelFormSchema>

export const CHANNEL_DEFAULTS: ChannelFormValues = {
  name: '',
  code: '',
  active: true,
  default: false,
  order_routing_strategy: '',
  storefront_access: '',
  guest_checkout: '',
  default_catalog_id: '',
  stock_location_ids: [],
}

/**
 * Maps form values to the Admin API payload. Every blank override is sent as
 * null rather than omitted: clearing a field on an existing channel has to
 * restore the inherited behavior, which an omitted key would not do.
 */
export function channelValuesToParams(
  v: ChannelFormValues,
): ChannelCreateParams & ChannelUpdateParams {
  return {
    name: v.name,
    ...(v.code ? { code: v.code } : {}),
    active: v.active,
    default: v.default,
    order_routing_strategy: v.order_routing_strategy || null,
    storefront_access: v.storefront_access || null,
    // '' → inherit (null); otherwise an explicit boolean.
    guest_checkout: v.guest_checkout === '' ? null : v.guest_checkout === 'true',
    // '' → no default catalog: shoppers outside any assigned catalog see
    // every product published on the channel.
    default_catalog_id: v.default_catalog_id || null,
    // Replace-set: an empty array clears the narrowing, so every location
    // serves the channel again.
    stock_location_ids: v.stock_location_ids,
  }
}