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 type {
CompanyCreateParams,
TaxExemptionCertificateCreateParams,
TaxIdentifierCreateParams,
} from '@spree/admin-sdk'
import { COMPANY_KINDS } from '@spree/core'
import { blankToNull, countryCodeSchema, signedIdParam } from '@spree/dashboard-core'
import { requiredMessage } from '@spree/dashboard-ui'
import { z } from 'zod/v4'
export const companyFormSchema = z.object({
name: z.string().min(1, { error: requiredMessage('name') }),
// Whether this buyer's own procurement process demands a PO reference on
// every order they place.
po_number_required: z.boolean(),
})
export type CompanyFormValues = z.infer<typeof companyFormSchema>
export const COMPANY_DEFAULTS: CompanyFormValues = { name: '', po_number_required: false }
export function companyValuesToParams(values: CompanyFormValues): CompanyCreateParams {
return {
name: values.name,
po_number_required: values.po_number_required,
}
}
// A sub-unit is created in place under its parent: name + kind only — the
// rest lives on its own page.
export const companyChildFormSchema = z.object({
name: z.string().min(1, { error: requiredMessage('name') }),
kind: z.enum(COMPANY_KINDS),
})
export type CompanyChildFormValues = z.infer<typeof companyChildFormSchema>
export const COMPANY_CHILD_DEFAULTS: CompanyChildFormValues = { name: '', kind: 'division' }
export const taxIdentifierFormSchema = z.object({
kind: z.string().min(1, { error: requiredMessage('tax_identifier.kind') }),
value: z.string().min(1, { error: requiredMessage('tax_identifier.value') }),
})
export type TaxIdentifierFormValues = z.infer<typeof taxIdentifierFormSchema>
export const TAX_IDENTIFIER_DEFAULTS: TaxIdentifierFormValues = { kind: '', value: '' }
export function taxIdentifierValuesToParams(
values: TaxIdentifierFormValues,
): TaxIdentifierCreateParams {
return { kind: values.kind, value: values.value }
}
/**
* The registration kinds core ships strings for. Any string is accepted by the
* API — what a kind means is decided by whichever validator is registered for
* it — so the merchant can type their own.
*/
export const TAX_IDENTIFIER_KINDS = ['eu_vat', 'gb_vat', 'ch_vat', 'au_abn', 'us_ein'] as const
export const taxExemptionCertificateFormSchema = z.object({
certificate_number: z.string().min(1, { error: requiredMessage('certificate_number') }),
tax_exemption_reason_id: z.string().min(1, { error: requiredMessage('tax_exemption_reason_id') }),
country_code: countryCodeSchema.or(z.literal('')).optional(),
state_code: z.string().optional(),
issued_at: z.string().optional(),
expires_at: z.string().optional(),
issuing_authority: z.string().optional(),
document_signed_id: z.string().nullable().optional(),
})
export type TaxExemptionCertificateFormValues = z.infer<typeof taxExemptionCertificateFormSchema>
export const TAX_EXEMPTION_CERTIFICATE_DEFAULTS: TaxExemptionCertificateFormValues = {
certificate_number: '',
tax_exemption_reason_id: '',
country_code: '',
state_code: '',
issued_at: '',
expires_at: '',
issuing_authority: '',
document_signed_id: null,
}
export function taxExemptionCertificateValuesToParams(
values: TaxExemptionCertificateFormValues,
): TaxExemptionCertificateCreateParams {
return {
certificate_number: values.certificate_number,
tax_exemption_reason_id: values.tax_exemption_reason_id,
country_code: values.country_code || null,
state_code: blankToNull(values.state_code),
issued_at: blankToNull(values.issued_at),
expires_at: blankToNull(values.expires_at),
issuing_authority: blankToNull(values.issuing_authority),
...signedIdParam('document', values.document_signed_id),
}
}