@aglyn/plugins-commerce 1.0.0-beta.244 → 1.0.0-beta.247

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +4 -1
  2. package/package.json +13 -13
  3. package/src/lib/components/commerce-console-page.js +10 -0
  4. package/src/lib/components/commerce-console-page.js.map +1 -1
  5. package/src/lib/components/console/local-delivery-settings-card.component.js +10 -13
  6. package/src/lib/components/console/local-delivery-settings-card.component.js.map +1 -1
  7. package/src/lib/components/console/locations-card.component.js +5 -9
  8. package/src/lib/components/console/locations-card.component.js.map +1 -1
  9. package/src/lib/components/console/product-zones.d.ts +7 -1
  10. package/src/lib/components/console/product-zones.js.map +1 -1
  11. package/src/lib/components/console/products-hub-zone.component.d.ts +3 -2
  12. package/src/lib/components/console/products-hub-zone.component.js +9 -1
  13. package/src/lib/components/console/products-hub-zone.component.js.map +1 -1
  14. package/src/lib/components/console/review-settings-card.component.d.ts +14 -0
  15. package/src/lib/components/console/review-settings-card.component.js +168 -0
  16. package/src/lib/components/console/review-settings-card.component.js.map +1 -0
  17. package/src/lib/components/console/reviews-moderation-card.component.d.ts +14 -0
  18. package/src/lib/components/console/reviews-moderation-card.component.js +85 -11
  19. package/src/lib/components/console/reviews-moderation-card.component.js.map +1 -1
  20. package/src/lib/components/product-reviews.d.ts +29 -1
  21. package/src/lib/components/product-reviews.js +97 -12
  22. package/src/lib/components/product-reviews.js.map +1 -1
  23. package/src/lib/model/commerce.d.ts +11 -2
  24. package/src/lib/model/commerce.js +14 -2
  25. package/src/lib/model/commerce.js.map +1 -1
  26. package/src/lib/model/index.d.ts +1 -0
  27. package/src/lib/model/index.js +1 -0
  28. package/src/lib/model/index.js.map +1 -1
  29. package/src/lib/model/product-reviews.d.ts +124 -0
  30. package/src/lib/model/product-reviews.js +133 -0
  31. package/src/lib/model/product-reviews.js.map +1 -0
  32. package/src/lib/model/store-setup-content.d.ts +145 -0
  33. package/src/lib/model/store-setup-content.js +813 -0
  34. package/src/lib/model/store-setup-content.js.map +1 -0
  35. package/src/lib/server/membership-reset.js +8 -2
  36. package/src/lib/server/membership-reset.js.map +1 -1
  37. package/src/lib/server/order-event-triggers.d.ts +2 -0
  38. package/src/lib/server/order-event-triggers.js +18 -1
  39. package/src/lib/server/order-event-triggers.js.map +1 -1
  40. package/src/lib/server/review-requests.d.ts +102 -0
  41. package/src/lib/server/review-requests.js +346 -0
  42. package/src/lib/server/review-requests.js.map +1 -0
  43. package/src/lib/server/review-tokens.d.ts +82 -0
  44. package/src/lib/server/review-tokens.js +119 -0
  45. package/src/lib/server/review-tokens.js.map +1 -0
  46. package/src/lib/server/reviews.d.ts +36 -3
  47. package/src/lib/server/reviews.js +310 -52
  48. package/src/lib/server/reviews.js.map +1 -1
  49. package/src/lib/server/store-setup-ai-capability.d.ts +61 -0
  50. package/src/lib/server/store-setup-ai-capability.js +343 -0
  51. package/src/lib/server/store-setup-ai-capability.js.map +1 -0
  52. package/src/lib/server/store-setup-ai-writer.console.d.ts +27 -0
  53. package/src/lib/server/store-setup-ai-writer.console.js +44 -0
  54. package/src/lib/server/store-setup-ai-writer.console.js.map +1 -0
  55. package/src/lib/server/store-setup-ai-writer.d.ts +37 -0
  56. package/src/lib/server/store-setup-ai-writer.js +174 -0
  57. package/src/lib/server/store-setup-ai-writer.js.map +1 -0
  58. package/src/lib/server.js +27 -0
  59. package/src/lib/server.js.map +1 -1
  60. package/src/lib/tenant-emails.js +58 -0
  61. package/src/lib/tenant-emails.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/commerce.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { modifierGroupsProblem, type ProductModifierGroup } from './product-modifiers'\nimport { nameSearchFields } from '@aglyn/aglyn/app-utils/name-search'\nimport { draftProductChannelFacts, type ProductChannelFacts } from './product-channel'\nimport type { PickupLocationSettings } from './order-local-fulfillment'\n\n/**\n * Commerce catalog v1 (AGL-276): products with options/variants,\n * hierarchical categories, tags, and manual/smart collections. Pure\n * model + helpers — no I/O; the products hub and storefront APIs operate\n * over these shapes. Documents live under `hosts/{hostId}/products`,\n * `.../productCategories`, and `.../collections`; the legacy Commerce\n * Starter product doc (AGL-90: name/priceUsd/inventory) lifts into this\n * shape via `liftLegacyProduct`, so existing docs keep working.\n */\n\n/*\n * THERE IS NO STOREFRONT FEE CONSTANT, and that absence is load-bearing\n * (AGL-2295).\n *\n * `COMMERCE_PLATFORM_FEE_PERCENT = 2` lived here with ZERO call sites. It\n * asserted a flat 2% that agrees with the plan table for exactly one cell\n * (Starter, physical) and disagrees with the other fifteen — Starter digital\n * is 5%, Pro digital 3%, Business 2%, Scale 1%, and every physical rate above\n * Starter is 0%. Nothing was wrong while nothing read it; the cost was that\n * the next reader wiring it up would silently re-price every plan, and a\n * constant sitting in the model beside the types reads like the answer.\n *\n * The single source is `resolveTransactionFeePct(org, productType)` in\n * `plan-entitlements.ts`, resolved PER REQUEST from the org — because it\n * depends on the plan, on per-org staff overrides, and on whether the\n * subscription is still alive. A constant cannot express any of that.\n * `commerce-fee-single-source.spec.ts` fails the build if one comes back.\n */\n/** Product price ceiling (whole USD). */\nexport const COMMERCE_MAX_PRICE_USD = 10000\n\n/**\n * The price a new product starts with (AGL-3676), in the store's currency —\n * USD, the only one a catalog prices in (`priceUsd`). Zach, 2026-10-08:\n * \"product without price is ok, but default a price\". So every create path\n * (the editor's New product, an accepted AI proposal, a guided start's\n * products) fills this in, where nothing states one, for the owner to\n * change; an owner may still clear it on purpose, and the storefront then\n * says \"Price coming soon\" and sells it nowhere (`variantHasPrice`).\n *\n * $25: a plain round number in the middle of what small shops list a\n * typical item at, above every payment method's floor but Affirm's ($50),\n * and never $0 — a default of nothing would sell the product for free.\n */\nexport const COMMERCE_DEFAULT_PRICE_USD = 25\n\nexport type ProductType = 'physical' | 'digital' | 'service'\nexport type ProductStatus = 'draft' | 'active' | 'archived'\n\n/** One axis of variation, e.g. { name: 'Size', values: ['S','M','L'] }. */\nexport interface ProductOption {\n name: string\n values: string[]\n}\n\n/** A sellable configuration of a product (every product has ≥ 1). */\nexport interface ProductVariant {\n /** Stable id unique within the product (never reused after delete). */\n id: string\n /** Option selections keyed by option name; {} for the default variant. */\n options?: Record<string, string>\n sku?: string\n barcode?: string\n priceUsd: number\n /** Strike-through price; a sale badge shows when > priceUsd. */\n compareAtPriceUsd?: number\n weightGrams?: number\n /** null/undefined = untracked; 0 = sold out (matches AGL-96). */\n inventory?: number | null\n /**\n * Per-location stock (AGL-286); `inventory` stays the summed\n * denormalization. Absent = single default location.\n */\n inventoryByLocation?: Record<string, number>\n /** Media-library image URL shown when this variant is selected. */\n imageUrl?: string\n}\n\n/** A product's packed size and customs facts (AGL-3612). */\nexport interface ProductShippingFacts {\n lengthCm?: number | null\n widthCm?: number | null\n heightCm?: number | null\n /** Harmonized System tariff code. */\n hsCode?: string\n /** ISO-3166 alpha-2 country the product was made in. */\n originCountry?: string\n}\n\n/** A place's postal address, in parts (AGL-3612). Country is ISO-3166 alpha-2. */\nexport interface PostalAddress {\n line1?: string\n line2?: string\n city?: string\n state?: string\n postalCode?: string\n country?: string\n phone?: string\n}\n\n/** `hosts/{hostId}/locations/{id}` doc (AGL-286). */\nexport interface InventoryLocation {\n name: string\n isDefault?: boolean\n /** Free text, as the location was first described. */\n address?: string\n /**\n * The same place as a postal address (AGL-3612), so parcels can ship from\n * it. Optional: a location written before this has only `address`.\n */\n postalAddress?: PostalAddress\n /** Whether buyers may collect orders here, and when (AGL-3624). */\n pickup?: PickupLocationSettings\n}\n\n/** A product's packed size for one unit, when all three sides are known. */\nexport function productParcelDimensions(\n product: Pick<HostProduct, 'shipping'> | null | undefined,\n): { lengthCm: number; widthCm: number; heightCm: number } | undefined {\n const facts = product?.shipping\n const side = (value: unknown) => {\n const number = Number(value)\n return Number.isFinite(number) && number > 0 ? Math.min(number, 300) : 0\n }\n const lengthCm = side(facts?.lengthCm)\n const widthCm = side(facts?.widthCm)\n const heightCm = side(facts?.heightCm)\n return lengthCm && widthCm && heightCm ? { lengthCm, widthCm, heightCm } : undefined\n}\n\n/**\n * Shipping facts as the product editor stages them, keystroke by keystroke\n * (AGL-3647): {@link normalizeProductShippingFacts}, except that a country\n * of origin being typed keeps its first letter. The editor runs this on\n * every keystroke, and the strict form drops anything short of two letters,\n * so the field could never be typed into. Readers normalize what is stored.\n */\nexport function draftProductShippingFacts(value: unknown): ProductShippingFacts | undefined {\n const facts = normalizeProductShippingFacts(value)\n const typed = String((value as Record<string, unknown> | null)?.['originCountry'] ?? '')\n .trim()\n .toUpperCase()\n if (/^[A-Z]$/.test(typed)) return { ...facts, originCountry: typed }\n return facts\n}\n\n/** A stored product's shipping facts made safe (AGL-3612); never throws. */\nexport function normalizeProductShippingFacts(value: unknown): ProductShippingFacts | undefined {\n if (!value || typeof value !== 'object') return undefined\n const raw = value as Record<string, unknown>\n const facts: ProductShippingFacts = {}\n for (const side of ['lengthCm', 'widthCm', 'heightCm'] as const) {\n const number = Number(raw[side])\n if (raw[side] !== null && raw[side] !== undefined && Number.isFinite(number) && number > 0) {\n facts[side] = Math.min(Math.round(number * 10) / 10, 300)\n }\n }\n const hsCode = String(raw['hsCode'] ?? '').replace(/[^0-9.]/g, '').slice(0, 14)\n if (hsCode) facts.hsCode = hsCode\n const originCountry = String(raw['originCountry'] ?? '').trim().toUpperCase()\n if (/^[A-Z]{2}$/.test(originCountry)) facts.originCountry = originCountry\n return Object.keys(facts).length ? facts : undefined\n}\n\n/** A stored location's postal address made safe (AGL-3612); never throws. */\nexport function normalizePostalAddress(value: unknown): PostalAddress | undefined {\n if (!value || typeof value !== 'object') return undefined\n const raw = value as Record<string, unknown>\n const address: PostalAddress = {}\n for (const field of ['line1', 'line2', 'city', 'state', 'postalCode', 'phone'] as const) {\n const text = String(raw[field] ?? '').trim().slice(0, 120)\n if (text) address[field] = text\n }\n const country = String(raw['country'] ?? '').trim().toUpperCase()\n if (/^[A-Z]{2}$/.test(country)) address.country = country\n return Object.keys(address).length ? address : undefined\n}\n\n/**\n * `hosts/{hostId}/registers/{id}` doc (AGL-472): a named point-of-sale\n * register. Creation is capped by the plan's `posRegisters` quota (Pro 1,\n * Business 2, Advanced 5; raised by a per-org entitlement override for the\n * $89/mo add-on), enforced server-side by the resources route. Each POS\n * sale stamps its `registerId` so takings are attributable per register.\n */\nexport interface PosRegister {\n name: string\n /** Optional default inventory location this register sells from. */\n locationId?: string\n}\n\n/**\n * The register ids within the plan's `posRegisters` cap (AGL-482), ranked\n * by creation order — the same ordering `pos-order.ts` enforces at sale\n * time. Registers beyond the cap (e.g. after a Business→Pro downgrade) are\n * excluded so the console/POS can dim or hide them instead of surfacing a\n * checkout 403. `cap` of `Infinity` returns all.\n */\nexport function registersWithinCap(\n registers: Array<{ $id: string; createdAt?: any }>,\n cap: number,\n): Set<string> {\n const createdMs = (r: { createdAt?: any }) =>\n r.createdAt?.toMillis?.() ?? r.createdAt?.seconds ?? 0\n const ranked = [...registers].sort(\n (a, b) => createdMs(a) - createdMs(b) || a.$id.localeCompare(b.$id),\n )\n return new Set(ranked.slice(0, Math.max(0, cap)).map((r) => r.$id))\n}\n\n/**\n * `hosts/{hostId}/suppliers/{id}` doc (AGL-289): where dropshipped\n * order lines route on payment. Email and webhook are both optional but\n * one must be set for routing to do anything; webhook payloads are\n * HMAC-SHA256-signed with `webhookSecret`.\n */\nexport interface HostSupplier {\n name: string\n email?: string\n webhookUrl?: string\n webhookSecret?: string\n}\n\n/** `hosts/{hostId}/products/{id}` doc. */\nexport interface HostProduct {\n name: string\n /** Host-unique URL segment for /products/{slug}. */\n slug: string\n description?: string\n type: ProductType\n status: ProductStatus\n /** Ordered media-library image URLs (first = primary). */\n mediaUrls?: string[]\n categoryIds?: string[]\n /**\n * The SMART collections this product's rules answer (AGL-3321), written by\n * every writer — see `productCollectionIds`.\n */\n collectionIds?: string[]\n tags?: string[]\n options?: ProductOption[]\n variants: ProductVariant[]\n /** Per-product overrides for PDP meta tags (AGL-299 consumes). */\n seo?: { title?: string; description?: string; imageUrl?: string }\n /**\n * What shopping channels ask of the product (AGL-3637): brand, GTIN, MPN,\n * condition and Google category. Absent means nothing was entered; the\n * channel feeds fall back to the store's defaults.\n */\n channel?: ProductChannelFacts\n /** Supplier for dropship routing (AGL-289). */\n supplierId?: string\n /** Out-of-stock behavior (AGL-281): deny (default) or allow backorder. */\n oversellPolicy?: 'deny' | 'backorder'\n /** Never taxed regardless of tax settings (AGL-285). */\n taxExempt?: boolean\n /**\n * Digital delivery (AGL-302): downloadable files for `digital`\n * products. Buyers always download the CURRENT list, so uploading a\n * new version re-delivers to everyone.\n */\n digitalFiles?: Array<{ url: string; fileName: string; version?: string }>\n /** Max download attempts per order line; absent = unlimited. */\n downloadLimit?: number\n /**\n * Recurring billing (AGL-303): buyers subscribe instead of buying\n * once; an active subscription is the entitlement content gating\n * checks (AGL-309).\n */\n subscription?: { interval: 'month' | 'year'; trialDays?: number }\n /**\n * Buyer-chosen billing (AGL-545): with `subscription` set, true lets\n * the buyer pick one-time OR subscribe on the PDP (default one-time);\n * absent/false keeps the product subscription-only.\n */\n subscriptionOptional?: boolean\n /** Members-only videos (AGL-315), streamed via short-TTL links. */\n gatedVideos?: Array<{ url: string; title?: string }>\n /** Manual related products for the upsell block (AGL-325). */\n relatedProductIds?: string[]\n /**\n * What one unit ships as (AGL-3612): its packed size, for a carrier to\n * price, and its customs facts, for a parcel that crosses a border. Weight\n * stays per variant. Absent means unknown, and a label is priced in the\n * merchant's default box.\n */\n shipping?: ProductShippingFacts\n /** Buying this issues a gift-card code for its price (AGL-322). */\n giftCard?: boolean\n /** Tracked-total at/below this alerts host managers (AGL-281). */\n lowStockThreshold?: number\n /**\n * Choices added to the item at the register (AGL-3607) — \"Oat milk\",\n * \"Extra shot\" — priced by the server from this list. See\n * `product-modifiers.ts`.\n */\n modifierGroups?: ProductModifierGroup[]\n /** Shown under Quick keys on the register's first screen (AGL-3607). */\n posQuickKey?: boolean\n createdAtMs?: number\n updatedAtMs?: number\n deletedAt?: number | null\n /** Legacy Commerce Starter fields kept so old docs read back (AGL-90). */\n priceUsd?: number\n inventory?: number | null\n imageUrl?: string\n}\n\n/** `hosts/{hostId}/productCategories/{id}` doc (tree via parentId). */\nexport interface ProductCategory {\n name: string\n slug: string\n parentId?: string | null\n order?: number\n}\n\nexport type CollectionRuleField =\n | 'tag'\n | 'categoryId'\n | 'priceUsd'\n | 'name'\n | 'type'\nexport type CollectionRuleOp = 'eq' | 'neq' | 'lt' | 'gt' | 'contains'\n\nexport interface CollectionRule {\n field: CollectionRuleField\n op: CollectionRuleOp\n value: string | number\n}\n\n/** `hosts/{hostId}/collections/{id}` doc. */\nexport interface HostCollection {\n name: string\n slug: string\n description?: string\n mode: 'manual' | 'smart'\n /** Manual mode: explicit ordered membership. */\n productIds?: string[]\n /** Smart mode: rules evaluated over active products. */\n rules?: CollectionRule[]\n /** Smart mode: true = every rule must match (default), false = any. */\n matchAll?: boolean\n /** Media-library image URL for the collection card/landing hero. */\n imageUrl?: string\n order?: number\n}\n\n/**\n * Payment-provider seam (AGL-284): checkout/session creation goes\n * through a provider id so PayPal (etc.) can slot in without reworking\n * callers. Stripe is the only implementation today; store settings\n * (AGL-295) will carry the selection when a second provider exists.\n */\nexport type PaymentProviderId = 'stripe'\n\nexport interface PaymentCheckoutRequest {\n provider: PaymentProviderId\n hostId: string\n amountCents: number\n feeCents: number\n productName: string\n successUrl: string\n cancelUrl: string\n metadata?: Record<string, string>\n}\n\n/** Variant ceiling per product (Shopify parity). */\nexport const COMMERCE_MAX_VARIANTS = 100\nexport const COMMERCE_MAX_OPTIONS = 3\nexport const COMMERCE_MAX_OPTION_VALUES = 25\nexport const COMMERCE_SLUG_MAX_LENGTH = 80\n\n/** Lowercase-kebab slug from a product/category/collection name. */\nexport function commerceSlug(name: string): string {\n return String(name ?? '')\n .normalize('NFKD')\n .replace(/[̀-ͯ]/g, '')\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, COMMERCE_SLUG_MAX_LENGTH)\n}\n\n/**\n * The search keys a product write carries, so the catalog can be searched by\n * the QUERY rather than by the rows a listener happened to fetch (AGL-2501).\n *\n * The hub lists `limit(500)`, and the search compared what that returned — so\n * a product past the window answered \"no products match\", which reads as the\n * product not existing rather than as the search not reaching it. Filtering\n * server-side needs fields Firestore can index, and neither of the two things\n * a merchant searches by is one:\n *\n * - A NAME needs case-insensitive contains, which Firestore has no operator\n * for. `nameSearchFields` denormalizes it into the three shapes that are\n * indexable — an exact key, word-prefix tokens for `array-contains`, and\n * the key reversed so \"ends with\" becomes a prefix range.\n * - A SKU lives inside `variants`, an array of OBJECTS, and Firestore cannot\n * query a field inside one. Flattened here into a top-level array so a SKU\n * is one `array-contains`.\n *\n * ⛔ A SKU therefore matches WHOLE, not as a substring: \"abc-123\" finds it and\n * \"123\" does not. That is the right way round for a SKU, which is a value\n * somebody copies rather than half-remembers — unlike a name, where the tokens\n * above buy word-prefix matching precisely because names are half-remembered.\n *\n * `barcodes` is the same flattening for the same reason, and it is the one the\n * register depends on: a keyboard-wedge scanner types the code and presses\n * Enter, so the lookup has to be exact and has to reach the whole catalog. A\n * scan is never a half-remembered value.\n *\n * Lower-cased because the translator lower-cases the typed query before it\n * builds the `array-contains`; stored and typed have to be normalized the same\n * way or the two silently disagree.\n *\n * ⚠️ Each array is OMITTED when a product has none, rather than written as\n * `[]`. `isNotEmpty` is served as `!= null`, and an empty array is not null —\n * so a product with no SKUs at all would answer \"has a SKU\" for every row.\n *\n * `priceFromCents` is the storefront's price key (AGL-3321): the price a\n * product grid card shows — \"From $X\", the lowest variant price — in whole\n * cents, so the catalog's price slider and its price sorts are a range and an\n * `orderBy` on one stored number rather than a comparison over variants no\n * query can reach. Written only when the variants are, because it is derived\n * from them: a write that carries no variants must not stamp a price.\n *\n * ⚠️ Spread this at EVERY write that sets a product's name or variants. A\n * write that sets the name without it leaves the keys describing the previous\n * name, and the product becomes findable only by what it used to be called.\n */\nexport function productSearchFields(product: {\n name: string\n variants?: ProductVariant[]\n}): {\n name: string\n nameLower: string\n nameTokens: string[]\n nameReversed: string\n skus?: string[]\n barcodes?: string[]\n priceFromCents?: number\n} {\n const flatten = (read: (variant: ProductVariant) => string | undefined) => [\n ...new Set(\n (product.variants ?? [])\n .map((variant) => (read(variant) ?? '').trim().toLowerCase())\n .filter(Boolean),\n ),\n ]\n const skus = flatten((variant) => variant.sku)\n const barcodes = flatten((variant) => variant.barcode)\n return {\n ...nameSearchFields(product.name),\n ...(skus.length ? { skus } : {}),\n ...(barcodes.length ? { barcodes } : {}),\n ...(product.variants\n ? { priceFromCents: productPriceFromCents({ variants: product.variants }) }\n : {}),\n }\n}\n\n/**\n * The price a storefront card shows, in whole cents (AGL-3321): the low end\n * of the variant range, which is what \"From $X\" prints and what the catalog's\n * price filter and price sorts compare. Stored as `priceFromCents` by\n * {@link productSearchFields}; `0` for a product with no priced variant yet.\n */\nexport function productPriceFromCents(\n product: Pick<HostProduct, 'variants'>,\n): number {\n return Math.round(productPriceRange(product)[0] * 100)\n}\n\n/**\n * The stock keys a product write carries (AGL-3321): the flat `inventory`\n * total, and `soldOut` — whether a storefront card reads \"Sold out\", which is\n * what the product grid's In stock chip asks the catalog query for\n * (`soldOut == false`). A grid cannot ask a query about stock inside\n * `variants`, so the verdict is stored beside them.\n *\n * ⚠️ Spread this at EVERY write that changes `variants` or `oversellPolicy`:\n * a sale, a cancellation, a stock adjustment and the editor alike. A write\n * that moves stock without it leaves a sold-out product under In stock, or an\n * in-stock one missing from it. Stored by `backfill-products-list-fields.mjs`\n * on the products written before it.\n */\nexport function productStockFields(\n product: Pick<HostProduct, 'variants' | 'oversellPolicy'>,\n): { inventory: number | null; soldOut: boolean } {\n const inventory = productInventory(product)\n return {\n inventory,\n soldOut: inventory != null && inventory <= 0 && product.oversellPolicy !== 'backorder',\n }\n}\n\n/**\n * Cartesian product of option values → the variant option combinations\n * the products hub materializes into a variants matrix. Empty/absent\n * options yield a single default combination ({}).\n */\nexport function expandVariantMatrix(\n options: ProductOption[] | undefined,\n): Array<Record<string, string>> {\n const usable = (options ?? []).filter(\n (option) => option.name && (option.values?.length ?? 0) > 0,\n )\n if (usable.length === 0) return [{}]\n let combos: Array<Record<string, string>> = [{}]\n for (const option of usable) {\n const next: Array<Record<string, string>> = []\n for (const combo of combos) {\n for (const value of option.values) {\n next.push({ ...combo, [option.name]: value })\n }\n }\n combos = next\n if (combos.length > COMMERCE_MAX_VARIANTS) {\n return combos.slice(0, COMMERCE_MAX_VARIANTS)\n }\n }\n return combos\n}\n\n/**\n * The option axes renamed, with every variant's selection carried to the new\n * name in place (AGL-3066).\n *\n * A variant keeps its selections keyed by option NAME, so a rename has to move\n * each key rather than rebuild the matrix: a rebuild matches variants by name\n * and value, finds none under the new name, and replaces every variant with a\n * new id holding the first variant's price and stock and no SKU, barcode,\n * compare-at price or weight. Moving the key keeps the id every order line\n * and stock movement refers to, and every field a person set.\n *\n * `names` holds one entry per option, in order; an entry that is not a string\n * keeps that option's name. Each selection keeps its position among a\n * variant's selections, because a variant is labeled by them in order.\n *\n * Two options may share a name for a moment while a person types. A variant\n * cannot hold two selections under one name, so while the new names are not\n * all different nothing is moved, and `validateProduct` refuses the product.\n * The selection left under the old name is found again by its value once the\n * names differ, so typing through a duplicate loses nothing.\n */\nexport function renameProductOptions(\n product: Pick<HostProduct, 'options' | 'variants'>,\n names: ReadonlyArray<string | null | undefined>,\n): Pick<HostProduct, 'options' | 'variants'> {\n const current = product.options ?? []\n const options = current.map((option, index) => {\n const name = names[index]\n return typeof name === 'string' ? { ...option, name } : option\n })\n const next = options.map((option) => option.name)\n if (new Set(next).size !== next.length) return { options, variants: product.variants }\n const variants = (product.variants ?? []).map((variant) => {\n const selections = variant.options\n if (!selections) return variant\n const axisOf = optionAxesOf(current, selections)\n const moved: Record<string, string> = {}\n let changed = false\n for (const [key, value] of Object.entries(selections)) {\n const axis = axisOf.get(key)\n const name = axis === undefined ? key : next[axis]\n if (name !== key) changed = true\n moved[name] = value\n }\n return changed ? { ...variant, options: moved } : variant\n })\n return { options, variants }\n}\n\n/**\n * Which option each of a variant's selections belongs to: the option of the\n * same name first, and then, for a selection under a name no option has any\n * longer, the one remaining option whose values hold its value. A selection\n * that fits neither belongs to no option and is left as it is.\n */\nfunction optionAxesOf(\n options: readonly ProductOption[],\n selections: Readonly<Record<string, string>>,\n): Map<string, number> {\n const axisOf = new Map<string, number>()\n const taken = new Set<number>()\n options.forEach((option, index) => {\n if (Object.prototype.hasOwnProperty.call(selections, option.name) && !axisOf.has(option.name)) {\n axisOf.set(option.name, index)\n taken.add(index)\n }\n })\n for (const [key, value] of Object.entries(selections)) {\n if (axisOf.has(key)) continue\n const axis = options.findIndex(\n (option, index) => !taken.has(index) && (option.values ?? []).includes(value),\n )\n if (axis === -1) continue\n axisOf.set(key, axis)\n taken.add(axis)\n }\n return axisOf\n}\n\n/**\n * Whether a variant has a price a sale can charge (AGL-2916).\n *\n * A product proposed by AI is created with every price left empty for the\n * merchant to set, so a stored variant can lack one. `Number(undefined)` is\n * no price, and neither is `null`, which `Number` reads as zero: a sale door\n * that priced either would charge nothing, or fail at the processor.\n */\nexport function variantHasPrice(variant: object | null | undefined): boolean {\n const price = (variant as { priceUsd?: unknown } | null | undefined)?.priceUsd\n return typeof price === 'number' && Number.isFinite(price) && price >= 0\n}\n\n/** Whether any variant of a product still needs its price set (AGL-2916). */\nexport function productPriceMissing(product: { variants?: readonly object[] }): boolean {\n return (product.variants ?? []).some((variant) => !variantHasPrice(variant))\n}\n\n/** Copy proposed for a product, by field (AGL-2916). A field left out is left alone. */\nexport interface ProductCopyValues {\n description?: string\n tags?: string[]\n categoryIds?: string[]\n /** One name per option the product has, in order. */\n optionNames?: string[]\n seoTitle?: string\n seoDescription?: string\n /**\n * Packed size and customs facts (AGL-3612), merged over what the product\n * has; a side set to `null` is cleared.\n */\n shipping?: Partial<ProductShippingFacts>\n /**\n * Shopping-channel facts (AGL-3637), merged over what the product has; a\n * field set to `''` is cleared.\n */\n channel?: { [Field in keyof ProductChannelFacts]?: string }\n}\n\n/**\n * The fields proposed copy changes on a product (AGL-2916), in the shape the\n * editor stores them: only the fields `values` names. Tags and categories are\n * each kept once; the search listing keeps its share image; and option names\n * rename the product's options through `renameProductOptions`, keeping every\n * variant, only when they name each option once with a name of its own.\n */\nexport function productCopyPatch(\n product: Pick<HostProduct, 'options' | 'variants' | 'seo' | 'shipping'> &\n Pick<Partial<HostProduct>, 'channel'>,\n values: ProductCopyValues,\n): Partial<\n Pick<HostProduct, 'description' | 'tags' | 'categoryIds' | 'seo' | 'options' | 'variants' | 'shipping' | 'channel'>\n> {\n const patch: Partial<\n Pick<HostProduct, 'description' | 'tags' | 'categoryIds' | 'seo' | 'options' | 'variants' | 'shipping' | 'channel'>\n > = {}\n if (values.shipping && typeof values.shipping === 'object') {\n // Stored whole, never `undefined`: the editor's save is a full `setDoc`,\n // which refuses an undefined field. Nothing known is an empty map.\n patch.shipping = draftProductShippingFacts({ ...product.shipping, ...values.shipping }) ?? {}\n }\n if (values.channel && typeof values.channel === 'object') {\n // Stored whole, never `undefined`, for the same reason. Nothing entered\n // is an empty map. Kept as typed: this runs on every keystroke.\n patch.channel = draftProductChannelFacts({ ...product.channel, ...values.channel }) ?? {}\n }\n if (typeof values.description === 'string') patch.description = values.description\n if (Array.isArray(values.tags)) {\n patch.tags = [...new Set(values.tags.map((tag) => tag.trim()).filter(Boolean))]\n }\n if (Array.isArray(values.categoryIds)) {\n patch.categoryIds = [...new Set(values.categoryIds.filter(Boolean))]\n }\n if (typeof values.seoTitle === 'string' || typeof values.seoDescription === 'string') {\n patch.seo = {\n ...product.seo,\n ...(typeof values.seoTitle === 'string' ? { title: values.seoTitle } : {}),\n ...(typeof values.seoDescription === 'string' ? { description: values.seoDescription } : {}),\n }\n }\n const options = product.options ?? []\n const names = values.optionNames?.map((name) => name.trim())\n if (\n names &&\n options.length > 0 &&\n names.length === options.length &&\n names.every(Boolean) &&\n new Set(names).size === names.length &&\n names.some((name, index) => name !== options[index].name)\n ) {\n Object.assign(patch, renameProductOptions(product, names))\n }\n return patch\n}\n\n/** A product proposed for a store, before anyone has priced it (AGL-2916). */\nexport interface ProposedProduct {\n name: string\n type: ProductType\n description: string\n tags: string[]\n options: ProductOption[]\n seoTitle: string\n seoDescription: string\n}\n\n/** A new product whose variants carry no price yet. */\nexport type UnpricedProductDraft = Omit<HostProduct, 'variants'> & {\n variants: Array<Omit<ProductVariant, 'priceUsd'>>\n}\n\n/**\n * The document a proposed product is created as (AGL-2916): a `draft`, under\n * a slug no product in `takenSlugs` has, with one variant for each\n * combination of its options and NO price on any of them. Prices are the\n * merchant's to set: the editor refuses to save the product until every\n * variant has one, and no sale door sells a variant without one\n * (`variantHasPrice`). The search keys travel with the name, as on every\n * create path.\n */\nexport function unpricedProductDraft(\n proposal: ProposedProduct,\n takenSlugs: ReadonlySet<string>,\n nowMs: number,\n): UnpricedProductDraft {\n const name = proposal.name.trim().slice(0, 120)\n const base = commerceSlug(name) || 'product'\n let slug = base\n for (let suffix = 2; takenSlugs.has(slug); suffix += 1) slug = `${base}-${suffix}`\n const seen = new Set<string>()\n const options = proposal.options\n .map((option) => ({\n name: option.name.trim(),\n values: [...new Set(option.values.map((value) => value.trim()).filter(Boolean))].slice(\n 0,\n COMMERCE_MAX_OPTION_VALUES,\n ),\n }))\n .filter((option) => {\n if (!option.name || !option.values.length || seen.has(option.name)) return false\n seen.add(option.name)\n return true\n })\n .slice(0, COMMERCE_MAX_OPTIONS)\n const variants = options.length\n ? expandVariantMatrix(options).map((combo, index) => ({\n id: `v${nowMs.toString(36)}${index}`,\n options: combo,\n }))\n : [{ id: 'default' }]\n const seoTitle = proposal.seoTitle.trim()\n const seoDescription = proposal.seoDescription.trim()\n return {\n // The keys for the variants as created: no price is `priceFromCents: 0`\n // and no tracked stock is never sold out, so the draft is ordered and\n // filtered like every other product once it is priced and activated.\n ...productSearchFields({ name, variants: variants as ProductVariant[] }),\n ...productStockFields({ variants: variants as ProductVariant[] }),\n slug,\n type: proposal.type,\n status: 'draft',\n description: proposal.description,\n tags: [...new Set(proposal.tags.map((tag) => tag.trim()).filter(Boolean))],\n ...(options.length ? { options } : {}),\n variants,\n ...(seoTitle || seoDescription\n ? { seo: { ...(seoTitle ? { title: seoTitle } : {}), ...(seoDescription ? { description: seoDescription } : {}) } }\n : {}),\n createdAtMs: nowMs,\n updatedAtMs: nowMs,\n }\n}\n\n/**\n * A proposed product as a new product is created from it (AGL-3676): the\n * draft `unpricedProductDraft` builds, with `priceUsd` on every variant —\n * the default price unless the caller states one — and the search keys\n * worked out again for that price. `null` leaves it unpriced on purpose.\n */\nexport function defaultPricedProductDraft(\n proposal: ProposedProduct,\n takenSlugs: ReadonlySet<string>,\n nowMs: number,\n priceUsd: number | null = COMMERCE_DEFAULT_PRICE_USD,\n): UnpricedProductDraft | HostProduct {\n const draft = unpricedProductDraft(proposal, takenSlugs, nowMs)\n if (priceUsd === null) return draft\n const variants = draft.variants.map((variant) => ({ ...variant, priceUsd })) as ProductVariant[]\n return { ...draft, ...productSearchFields({ name: draft.name, variants }), variants }\n}\n\n/** Variant whose option selections match exactly; undefined if none. */\nexport function findVariant(\n product: Pick<HostProduct, 'variants'>,\n selections: Record<string, string>,\n): ProductVariant | undefined {\n return product.variants?.find((variant) => {\n const options = variant.options ?? {}\n const keys = new Set([...Object.keys(options), ...Object.keys(selections)])\n for (const key of keys) {\n if (options[key] !== selections[key]) return false\n }\n return true\n })\n}\n\n/** [min, max] price across variants; [0, 0] when there are none. */\nexport function productPriceRange(\n product: Pick<HostProduct, 'variants'>,\n): [number, number] {\n const prices = (product.variants ?? [])\n .map((variant) => Number(variant.priceUsd))\n .filter((price) => Number.isFinite(price) && price >= 0)\n if (prices.length === 0) return [0, 0]\n return [Math.min(...prices), Math.max(...prices)]\n}\n\n/** Sum of tracked inventory; null when every variant is untracked. */\nexport function productInventory(\n product: Pick<HostProduct, 'variants'>,\n): number | null {\n let total: number | null = null\n for (const variant of product.variants ?? []) {\n if (variant.inventory == null) continue\n total = (total ?? 0) + Number(variant.inventory)\n }\n return total\n}\n\n/**\n * Reasons an inventory adjustment doc may carry (AGL-281).\n *\n * `cancellation` is the only one written automatically in the PLUS direction\n * (AGL-1808): cancelling an order the transition rules prove was never\n * fulfilled puts back exactly what the sale took. It is deliberately not\n * `refund` — no money moved — and not `correction`, which is what a merchant\n * picks when the count disagreed with the shelf for a reason nobody recorded.\n * Kept out of the \"Adjust stock\" dialog's menu on purpose: a merchant reaching\n * for it by hand means something else happened.\n *\n * `sync` is a count another warehouse reported (AGL-3634): a fulfillment\n * network holding the goods said how many it can ship, and the count was set\n * to that. Written only through core's `core.stock-levels`, with `source`\n * naming who counted, and kept out of the dialog's menu for the same reason.\n */\nexport type InventoryAdjustmentReason =\n 'sale' | 'refund' | 'restock' | 'correction' | 'damage' | 'cancellation' | 'sync'\n\n/** `hosts/{hostId}/inventoryAdjustments/{id}` doc. */\nexport interface InventoryAdjustment {\n productId: string\n variantId: string\n /** Positive = stock added, negative = removed. */\n delta: number\n /**\n * What the count ACTUALLY moved by, when the floor in\n * `adjustVariantInventory` absorbed part of `delta` (AGL-2149). Written only\n * when the two differ, which for a sale means a backorder product that sold\n * past zero: `delta` stays the units sold — that is the merchant's stock\n * history and it must keep saying \"3 went out the door\" — while this says how\n * many of them the count could give up.\n *\n * A REVERSAL MUST READ THIS, NOT `delta`. Restoring `delta` on a backorder\n * sale that the floor swallowed invents inventory that never existed: 0 sells\n * 3, the count stays 0, and a `+3` on cancellation leaves 3 units nobody has.\n * Absent on every row written before AGL-2149 and on every row where nothing\n * was clamped, so `appliedDelta ?? delta` is the reader.\n */\n appliedDelta?: number\n reason: InventoryAdjustmentReason\n /** Order id for sale/refund adjustments. */\n orderId?: string\n /** Location for multi-location stock (AGL-286); absent = default. */\n locationId?: string\n /**\n * Who counted, for a `sync` row: the warehouse that reported it (AGL-3634);\n * for a `sale` another channel made, that channel (AGL-3638).\n */\n source?: string\n atMs: number\n}\n\n/**\n * Whether `quantity` of a variant is purchasable (AGL-281): untracked\n * stock always is; tracked stock honors the product's oversell policy.\n */\nexport function canPurchase(\n product: Pick<HostProduct, 'variants' | 'oversellPolicy'>,\n variantId: string | undefined,\n quantity = 1,\n): boolean {\n const variant = variantId\n ? product.variants?.find((item) => item.id === variantId)\n : product.variants?.[0]\n if (!variant) return false\n if (variant.inventory == null) return true\n if (product.oversellPolicy === 'backorder') return true\n return Number(variant.inventory) >= quantity\n}\n\n/**\n * What the register has to SAY about a line it is about to sell (AGL-2357).\n *\n * ## The defect\n *\n * `pos-order.ts` contains no `canPurchase` call. Every storefront door gates on\n * it; the register does not, so a merchant who set `oversellPolicy: 'deny'` in\n * the product editor silently got `backorder` at the counter — the count floors\n * at zero underneath and nothing anywhere says so.\n *\n * ## The decision, and why it is a shortfall and not a refusal\n *\n * Warn, never block. A till is the wrong place for a stale number to stop a\n * real sale: the cashier is holding the goods, the physical shelf is the truth,\n * and refusing loses a sale over data that is behind. But \"deny\" must at least\n * SAY something. So this reports the shortfall and the caller sells anyway.\n * Honouring the policy with a manager-level override is the post-launch shape\n * (AGL-2372) and wants an audit trail this deliberately does not build.\n *\n * ## Why the test is `canPurchase`\n *\n * Exactly the gate the register was missing, and no wider. An untracked variant\n * has no number to be short against, and a `backorder` product's merchant chose\n * to sell past zero — warning them would be noise about a setting they set on\n * purpose. So a shortfall is reported only where a purchase would have been\n * refused anywhere else in the product.\n */\nexport interface StockShortfall {\n productId: string\n variantId?: string\n /** Product name at the time of sale, for a message the cashier can read. */\n name: string\n variantLabel?: string\n /** Units the cashier is ringing up. */\n requested: number\n /** Units the count says are on the shelf; never negative. */\n available: number\n}\n\nexport function stockShortfall(\n product: Pick<HostProduct, 'variants' | 'oversellPolicy'>,\n variantId: string | undefined,\n quantity = 1,\n): { available: number } | null {\n if (canPurchase(product, variantId, quantity)) return null\n const variant = variantId\n ? product.variants?.find((item) => item.id === variantId)\n : product.variants?.[0]\n // `canPurchase` answers false for a variant that does not exist at all. That\n // is not a stock shortfall and must not be reported as \"0 in stock\" — the\n // register's own line builder falls back to `variants[0]`, so the case is\n // unreachable from the counter, and inventing a count for it would be the\n // one failure mode this feature must not have.\n if (!variant || variant.inventory == null) return null\n return { available: Math.max(0, Math.round(Number(variant.inventory) || 0)) }\n}\n\n/**\n * Whether stock tracking MEANS anything on a product (AGL-1744).\n *\n * `canPurchase` above gates every checkout on stock, subscription sessions\n * included, but nothing ever decrements for a subscription — not on the\n * initial charge (the `commerce-subscription` webhook branch deliberately\n * writes no order and touches no inventory, AGL-1732) and not on renewal\n * (`invoice.payment_succeeded` is unhandled repo-wide, AGL-1743). So on a\n * subscription-only product that gate is permanently satisfied: one unit of\n * stock sells an unlimited number of subscriptions, and the count never\n * moves however many boxes ship. The merchant set a number, the system\n * checks it on every sale, and the number is never true.\n *\n * AGL-1750 resolved the half AGL-1744 had to leave open: every paid invoice\n * of a PHYSICAL subscription now mints an order and decrements its variant's\n * stock (the `invoice.paid` branch of the commerce webhook), opening cycle\n * included. So the honest split is now three-way:\n *\n * - `subscriptionOptional` (\"Both\") products have a real one-time path —\n * the cart only ever builds `mode: 'payment'` sessions, and buy-now with\n * `billing: 'once'` records a plain order — and that path decrements\n * honestly. Withdrawing stock tracking there would delete a working\n * control and re-open the AGL-1711 oversell on the half that works.\n * - a PHYSICAL subscription box consumes a unit every cycle, and since\n * AGL-1750 the cycle's own order decrements it — the count moves as the\n * boxes ship, so the number the merchant sets is kept true again.\n * - a DIGITAL or SERVICE subscription-only product still has no\n * decrementing path anywhere, so the field stays withdrawn there: one\n * unit would still sell unlimited subscriptions.\n *\n * This says nothing about what `canPurchase` returns — that is the gate for\n * every other purchase path and is deliberately untouched. This is a console\n * predicate: it stops the editor inviting a merchant to set a number the\n * system will never keep true.\n */\nexport function stockTrackingApplies(\n product: Pick<HostProduct, 'subscription' | 'subscriptionOptional' | 'type'>,\n): boolean {\n return (\n !product.subscription ||\n product.subscriptionOptional === true ||\n product.type === 'physical'\n )\n}\n\n/** Buyer's requested billing mode on a checkout POST (AGL-545). */\nexport type CheckoutBillingChoice = 'once' | 'subscribe'\n\n/**\n * Resolves the Stripe Checkout mode for a product + the buyer's\n * requested billing choice (AGL-545). The product doc is authoritative:\n * - no `subscription` → always one-time (the request field is ignored)\n * - `subscription` without `subscriptionOptional` → always subscription\n * (a forged `billing: 'once'` cannot buy a subscription product once)\n * - `subscriptionOptional` → the buyer chooses; absent/invalid choices\n * default to one-time, matching the PDP default.\n */\nexport function resolveCheckoutBillingMode(\n product: Pick<HostProduct, 'subscription' | 'subscriptionOptional'>,\n requested?: string | null,\n): 'payment' | 'subscription' {\n if (!product.subscription) return 'payment'\n if (!product.subscriptionOptional) return 'subscription'\n return requested === 'subscribe' ? 'subscription' : 'payment'\n}\n\n/**\n * Applies a stock delta to one variant, flooring at zero (race-window\n * sales can't drive the display negative), and returns the new variants\n * array — callers persist it plus the `productInventory` denormalization.\n * With `locationId`, the delta lands in that location's bucket and the\n * flat `inventory` re-sums across locations (AGL-286).\n */\nexport function adjustVariantInventory(\n product: Pick<HostProduct, 'variants'>,\n variantId: string,\n delta: number,\n locationId?: string,\n): ProductVariant[] {\n return (product.variants ?? []).map((variant) => {\n if (variant.id !== variantId || variant.inventory == null) return variant\n if (locationId && variant.inventoryByLocation) {\n const buckets = {\n ...variant.inventoryByLocation,\n [locationId]: Math.max(\n 0,\n Number(variant.inventoryByLocation[locationId] ?? 0) + delta,\n ),\n }\n return {\n ...variant,\n inventoryByLocation: buckets,\n inventory: Object.values(buckets).reduce(\n (sum, count) => sum + Number(count),\n 0,\n ),\n }\n }\n return {\n ...variant,\n inventory: Math.max(0, Number(variant.inventory) + delta),\n }\n })\n}\n\n/**\n * What `adjustVariantInventory` will ACTUALLY move the count by (AGL-2149).\n *\n * The floor in that helper is not a rounding detail, it is a silent absorber. A\n * backorder product (`oversellPolicy: 'backorder'`, which `canPurchase` admits\n * at any stock level) sitting at 0 sells 3: `Math.max(0, 0 + -3)` is 0, so the\n * count does not move — correctly, since inventory is a shelf count and shelves\n * do not go negative — but the ledger row said `-3` and every reversal read it\n * as three units to put back. Cancelling that order handed the merchant 3 units\n * that never existed and a stock badge that offers them for sale.\n *\n * Removing the floor is not the alternative: every reader of `inventory` — the\n * badges, `canPurchase`, `productInventory`, `isLowStock` — assumes it cannot\n * go negative, and a negative count would be a far wider change than the bug.\n * So the clamp stays and the amount it absorbed is recorded instead.\n *\n * Deliberately mirrors `adjustVariantInventory`'s own arithmetic, including\n * which field the clamp applies to in the location-tracked case, so the two\n * cannot disagree about what happened.\n */\nexport function appliedVariantInventoryDelta(\n product: Pick<HostProduct, 'variants'>,\n variantId: string,\n delta: number,\n locationId?: string,\n): number {\n const variant = (product.variants ?? []).find(\n (item) => item.id === variantId,\n )\n if (!variant || variant.inventory == null) return 0\n if (locationId && variant.inventoryByLocation) {\n const before = Number(variant.inventoryByLocation[locationId] ?? 0)\n return Math.max(0, before + delta) - before\n }\n const before = Number(variant.inventory)\n return Math.max(0, before + delta) - before\n}\n\n/**\n * Moves stock between two locations of a tracked variant (AGL-286).\n * Quantity clamps to what the source location holds; the flat total is\n * unchanged by construction.\n */\nexport function transferVariantInventory(\n product: Pick<HostProduct, 'variants'>,\n variantId: string,\n fromLocationId: string,\n toLocationId: string,\n quantity: number,\n): ProductVariant[] {\n return (product.variants ?? []).map((variant) => {\n if (variant.id !== variantId || variant.inventory == null) return variant\n const buckets = { ...(variant.inventoryByLocation ?? {}) }\n const available = Math.max(0, Number(buckets[fromLocationId] ?? 0))\n const moved = Math.min(available, Math.max(0, Math.round(quantity)))\n if (moved === 0) return variant\n buckets[fromLocationId] = available - moved\n buckets[toLocationId] = Math.max(0, Number(buckets[toLocationId] ?? 0)) + moved\n return { ...variant, inventoryByLocation: buckets }\n })\n}\n\n/** True when tracked stock is at/below the product's alert threshold. */\nexport function isLowStock(\n product: Pick<HostProduct, 'variants' | 'lowStockThreshold'>,\n): boolean {\n const threshold = product.lowStockThreshold\n if (threshold == null || !(threshold >= 0)) return false\n const total = productInventory(product)\n return total != null && total <= threshold\n}\n\nfunction ruleMatches(product: HostProduct, rule: CollectionRule): boolean {\n const value = rule.value\n switch (rule.field) {\n case 'tag': {\n const tags = product.tags ?? []\n const has = tags.includes(String(value))\n return rule.op === 'neq' ? !has : has\n }\n case 'categoryId': {\n const ids = product.categoryIds ?? []\n const has = ids.includes(String(value))\n return rule.op === 'neq' ? !has : has\n }\n case 'priceUsd': {\n const [min, max] = productPriceRange(product)\n const numeric = Number(value)\n if (!Number.isFinite(numeric)) return false\n if (rule.op === 'lt') return min < numeric\n if (rule.op === 'gt') return max > numeric\n if (rule.op === 'neq') return min !== numeric || max !== numeric\n return min <= numeric && numeric <= max\n }\n case 'name': {\n const name = (product.name ?? '').toLowerCase()\n const needle = String(value).toLowerCase()\n if (rule.op === 'contains') return name.includes(needle)\n if (rule.op === 'neq') return name !== needle\n return name === needle\n }\n case 'type': {\n const matches = product.type === value\n return rule.op === 'neq' ? !matches : matches\n }\n default:\n return false\n }\n}\n\n/**\n * Whether a product's own fields answer a SMART collection's rules — its\n * tags, categories, prices, name and type, and nothing about whether it is\n * on sale. That is the storefront query's scope (`deletedAt == null`,\n * `status == 'active'`), so membership stored on the product never goes stale\n * when a product is archived or brought back.\n *\n * A collection with no rules holds nothing.\n */\n/** What smart-collection membership reads of a product — a draft's too. */\nexport interface MembershipProduct {\n name?: string\n type?: ProductType\n tags?: string[]\n categoryIds?: string[]\n variants?: ReadonlyArray<{ id?: string; priceUsd?: number | null }>\n}\n\nexport function smartCollectionMatches(\n product: MembershipProduct,\n collection: Pick<HostCollection, 'rules' | 'matchAll'>,\n): boolean {\n const rules = collection.rules ?? []\n if (rules.length === 0) return false\n const matcher = (rule: CollectionRule) => ruleMatches(product as HostProduct, rule)\n return collection.matchAll === false\n ? rules.some(matcher)\n : rules.every(matcher)\n}\n\n/**\n * Smart-collection membership: draft/archived/deleted products never\n * match; manual collections answer from productIds.\n */\nexport function matchesCollection(\n product: HostProduct,\n collection: HostCollection,\n productId?: string,\n): boolean {\n if (product.deletedAt || product.status !== 'active') return false\n if (collection.mode === 'manual') {\n return productId != null &&\n (collection.productIds ?? []).includes(productId)\n }\n return smartCollectionMatches(product, collection)\n}\n\n/**\n * The stored membership of a product in the host's SMART collections\n * (AGL-3321): the ids of those whose rules it answers\n * ({@link smartCollectionMatches}), sorted.\n *\n * The storefront reads a smart collection whose rules no query can hold (a\n * NOT, an OR, a name rule, a price over) as `collectionIds array-contains\n * <id>` — so the membership is WRITTEN, by every product writer from the\n * host's smart collections as they stand, and by the collection-membership\n * route for every product whenever a smart collection's rules change or it\n * is deleted. `tools/scripts/backfill-product-collection-ids.mjs` stamps the\n * products written before; the two answer\n * `tools/scripts/lib/product-collection-ids.fixtures.json`.\n */\nexport const PRODUCT_COLLECTION_IDS = 'collectionIds'\n\n/** A smart collection as membership is computed from it. */\nexport interface SmartCollectionRules {\n id: string\n rules?: CollectionRule[]\n matchAll?: boolean\n}\n\nexport function productCollectionIds(\n product: MembershipProduct,\n smartCollections: readonly SmartCollectionRules[],\n): string[] {\n return smartCollections\n .filter((collection) => smartCollectionMatches(product, collection))\n .map((collection) => collection.id)\n .sort()\n}\n\n/**\n * Lifts a legacy Commerce Starter doc (AGL-90: flat name/priceUsd/\n * inventory/imageUrl) into the catalog shape with a single default\n * variant. Already-lifted docs pass through unchanged.\n */\nexport function liftLegacyProduct(\n raw: Partial<HostProduct> & { name?: string },\n): HostProduct {\n if (Array.isArray(raw.variants) && raw.variants.length > 0) {\n return raw as HostProduct\n }\n const priceUsd = Number(raw.priceUsd ?? 0)\n return {\n ...raw,\n name: raw.name ?? 'Product',\n slug: raw.slug || commerceSlug(raw.name ?? 'product'),\n type: raw.type ?? 'physical',\n status: raw.status ?? 'active',\n variants: [\n {\n id: 'default',\n priceUsd: Number.isFinite(priceUsd) ? priceUsd : 0,\n inventory: raw.inventory ?? null,\n },\n ],\n }\n}\n\n/** Actionable error, or null when the product is storable. */\nexport function validateProduct(product: HostProduct): string | null {\n if (!product.name?.trim()) return 'Product name is required'\n if (!product.slug || product.slug !== commerceSlug(product.slug)) {\n return 'Slug must be lowercase letters, numbers, and dashes'\n }\n if (!['physical', 'digital', 'service'].includes(product.type)) {\n return 'Unknown product type'\n }\n if (!['draft', 'active', 'archived'].includes(product.status)) {\n return 'Unknown product status'\n }\n const options = product.options ?? []\n if (options.length > COMMERCE_MAX_OPTIONS) {\n return `At most ${COMMERCE_MAX_OPTIONS} options per product`\n }\n for (const option of options) {\n if (!option.name?.trim()) return 'Option names are required'\n if ((option.values?.length ?? 0) === 0) {\n return `Option \"${option.name}\" needs at least one value`\n }\n if (option.values.length > COMMERCE_MAX_OPTION_VALUES) {\n return `Option \"${option.name}\" has too many values`\n }\n if (new Set(option.values).size !== option.values.length) {\n return `Option \"${option.name}\" has duplicate values`\n }\n }\n // A variant keys its selections by option name, so two options with one\n // name cannot both be selected (AGL-3066).\n if (new Set(options.map((option) => option.name.trim())).size !== options.length) {\n return 'Each option needs its own name'\n }\n const modifierProblem = modifierGroupsProblem(product.modifierGroups)\n if (modifierProblem) return modifierProblem\n const variants = product.variants ?? []\n if (variants.length === 0) return 'Products need at least one variant'\n if (variants.length > COMMERCE_MAX_VARIANTS) {\n return `At most ${COMMERCE_MAX_VARIANTS} variants per product`\n }\n const ids = new Set<string>()\n const skus = new Set<string>()\n for (const variant of variants) {\n if (!variant.id) return 'Variants need stable ids'\n if (ids.has(variant.id)) return 'Variant ids must be unique'\n ids.add(variant.id)\n // A variant with no price is storable (AGL-3676): the owner may list a\n // product before pricing it, the storefront says \"Price coming soon\",\n // and no sale door sells it (`variantHasPrice`).\n if (variant.priceUsd === undefined || variant.priceUsd === null) {\n if (variant.sku) {\n if (skus.has(variant.sku)) return 'Variant SKUs must be unique'\n skus.add(variant.sku)\n }\n continue\n }\n const price = Number(variant.priceUsd)\n if (!Number.isFinite(price) || price < 0) {\n return 'Variant prices must be zero or more'\n }\n if (price > COMMERCE_MAX_PRICE_USD) {\n return `Prices are capped at $${COMMERCE_MAX_PRICE_USD}`\n }\n if (variant.sku) {\n if (skus.has(variant.sku)) return 'Variant SKUs must be unique'\n skus.add(variant.sku)\n }\n if (\n variant.compareAtPriceUsd != null &&\n Number(variant.compareAtPriceUsd) <= price\n ) {\n return 'Compare-at price must exceed the price'\n }\n }\n return null\n}\n\n/** Actionable error, or null when the collection is storable. */\nexport function validateCollection(collection: HostCollection): string | null {\n if (!collection.name?.trim()) return 'Collection name is required'\n if (\n !collection.slug ||\n collection.slug !== commerceSlug(collection.slug)\n ) {\n return 'Slug must be lowercase letters, numbers, and dashes'\n }\n if (collection.mode !== 'manual' && collection.mode !== 'smart') {\n return 'Unknown collection mode'\n }\n if (collection.mode === 'smart') {\n const rules = collection.rules ?? []\n if (rules.length === 0) return 'Smart collections need at least one rule'\n for (const rule of rules) {\n if (\n !['tag', 'categoryId', 'priceUsd', 'name', 'type'].includes(rule.field)\n ) {\n return 'Unknown rule field'\n }\n if (!['eq', 'neq', 'lt', 'gt', 'contains'].includes(rule.op)) {\n return 'Unknown rule operator'\n }\n if (rule.value === '' || rule.value == null) {\n return 'Rule values are required'\n }\n }\n }\n return null\n}\n"],"names":["modifierGroupsProblem","nameSearchFields","draftProductChannelFacts","COMMERCE_MAX_PRICE_USD","COMMERCE_DEFAULT_PRICE_USD","productParcelDimensions","product","facts","shipping","side","value","number","Number","isFinite","Math","min","lengthCm","widthCm","heightCm","undefined","draftProductShippingFacts","normalizeProductShippingFacts","typed","String","trim","toUpperCase","test","originCountry","raw","round","hsCode","replace","slice","Object","keys","length","normalizePostalAddress","address","field","text","country","registersWithinCap","registers","cap","createdMs","r","createdAt","toMillis","seconds","ranked","sort","a","b","$id","localeCompare","Set","max","map","COMMERCE_MAX_VARIANTS","COMMERCE_MAX_OPTIONS","COMMERCE_MAX_OPTION_VALUES","COMMERCE_SLUG_MAX_LENGTH","commerceSlug","name","normalize","toLowerCase","productSearchFields","flatten","read","variants","variant","filter","Boolean","skus","sku","barcodes","barcode","priceFromCents","productPriceFromCents","productPriceRange","productStockFields","inventory","productInventory","soldOut","oversellPolicy","expandVariantMatrix","options","usable","option","values","combos","next","combo","push","renameProductOptions","names","current","index","size","selections","axisOf","optionAxesOf","moved","changed","key","entries","axis","get","Map","taken","forEach","prototype","hasOwnProperty","call","has","set","add","findIndex","includes","variantHasPrice","price","priceUsd","productPriceMissing","some","productCopyPatch","patch","channel","description","Array","isArray","tags","tag","categoryIds","seoTitle","seoDescription","seo","title","optionNames","every","assign","unpricedProductDraft","proposal","takenSlugs","nowMs","base","slug","suffix","seen","id","toString","type","status","createdAtMs","updatedAtMs","defaultPricedProductDraft","draft","findVariant","find","prices","total","canPurchase","variantId","quantity","item","stockShortfall","available","stockTrackingApplies","subscription","subscriptionOptional","resolveCheckoutBillingMode","requested","adjustVariantInventory","delta","locationId","inventoryByLocation","buckets","reduce","sum","count","appliedVariantInventoryDelta","before","transferVariantInventory","fromLocationId","toLocationId","isLowStock","threshold","lowStockThreshold","ruleMatches","rule","op","ids","numeric","needle","matches","smartCollectionMatches","collection","rules","matcher","matchAll","matchesCollection","productId","deletedAt","mode","productIds","PRODUCT_COLLECTION_IDS","productCollectionIds","smartCollections","liftLegacyProduct","validateProduct","modifierProblem","modifierGroups","compareAtPriceUsd","validateCollection"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,qBAAqB,QAAmC,yBAAqB;AACtF,SAASC,gBAAgB,QAAQ,qCAAoC;AACrE,SAASC,wBAAwB,QAAkC,uBAAmB;AAGtF;;;;;;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T;AAEA,+DAA+D,GAC/D,OAAO,SAAS4F,mBAAmBjB,UAA0B;QACtDA;IAAL,IAAI,GAACA,mBAAAA,WAAWpI,IAAI,qBAAfoI,iBAAiB3K,IAAI,KAAI,OAAO;IACrC,IACE,CAAC2K,WAAWlD,IAAI,IAChBkD,WAAWlD,IAAI,KAAKnF,aAAaqI,WAAWlD,IAAI,GAChD;QACA,OAAO;IACT;IACA,IAAIkD,WAAWO,IAAI,KAAK,YAAYP,WAAWO,IAAI,KAAK,SAAS;QAC/D,OAAO;IACT;IACA,IAAIP,WAAWO,IAAI,KAAK,SAAS;YACjBP;QAAd,MAAMC,SAAQD,oBAAAA,WAAWC,KAAK,YAAhBD,oBAAoB,EAAE;QACpC,IAAIC,MAAMjK,MAAM,KAAK,GAAG,OAAO;QAC/B,KAAK,MAAMyJ,QAAQQ,MAAO;YACxB,IACE,CAAC;gBAAC;gBAAO;gBAAc;gBAAY;gBAAQ;aAAO,CAAC9E,QAAQ,CAACsE,KAAKtJ,KAAK,GACtE;gBACA,OAAO;YACT;YACA,IAAI,CAAC;gBAAC;gBAAM;gBAAO;gBAAM;gBAAM;aAAW,CAACgF,QAAQ,CAACsE,KAAKC,EAAE,GAAG;gBAC5D,OAAO;YACT;YACA,IAAID,KAAKlL,KAAK,KAAK,MAAMkL,KAAKlL,KAAK,IAAI,MAAM;gBAC3C,OAAO;YACT;QACF;IACF;IACA,OAAO;AACT"}
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/commerce.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { modifierGroupsProblem, type ProductModifierGroup } from './product-modifiers'\nimport { nameSearchFields } from '@aglyn/aglyn/app-utils/name-search'\nimport { draftProductChannelFacts, type ProductChannelFacts } from './product-channel'\nimport type { PickupLocationSettings } from './order-local-fulfillment'\n\n/**\n * Commerce catalog v1 (AGL-276): products with options/variants,\n * hierarchical categories, tags, and manual/smart collections. Pure\n * model + helpers — no I/O; the products hub and storefront APIs operate\n * over these shapes. Documents live under `hosts/{hostId}/products`,\n * `.../productCategories`, and `.../collections`; the legacy Commerce\n * Starter product doc (AGL-90: name/priceUsd/inventory) lifts into this\n * shape via `liftLegacyProduct`, so existing docs keep working.\n */\n\n/*\n * THERE IS NO STOREFRONT FEE CONSTANT, and that absence is load-bearing\n * (AGL-2295).\n *\n * `COMMERCE_PLATFORM_FEE_PERCENT = 2` lived here with ZERO call sites. It\n * asserted a flat 2% that agrees with the plan table for exactly one cell\n * (Starter, physical) and disagrees with the other fifteen — Starter digital\n * is 5%, Pro digital 3%, Business 2%, Scale 1%, and every physical rate above\n * Starter is 0%. Nothing was wrong while nothing read it; the cost was that\n * the next reader wiring it up would silently re-price every plan, and a\n * constant sitting in the model beside the types reads like the answer.\n *\n * The single source is `resolveTransactionFeePct(org, productType)` in\n * `plan-entitlements.ts`, resolved PER REQUEST from the org — because it\n * depends on the plan, on per-org staff overrides, and on whether the\n * subscription is still alive. A constant cannot express any of that.\n * `commerce-fee-single-source.spec.ts` fails the build if one comes back.\n */\n/** Product price ceiling (whole USD). */\nexport const COMMERCE_MAX_PRICE_USD = 10000\n\n/**\n * The price a new product starts with (AGL-3676), in the store's currency —\n * USD, the only one a catalog prices in (`priceUsd`). Zach, 2026-10-08:\n * \"product without price is ok, but default a price\". So every create path\n * (the editor's New product, an accepted AI proposal, a guided start's\n * products) fills this in, where nothing states one, for the owner to\n * change; an owner may still clear it on purpose, and the storefront then\n * says \"Price coming soon\" and sells it nowhere (`variantHasPrice`).\n *\n * $25: a plain round number in the middle of what small shops list a\n * typical item at, above every payment method's floor but Affirm's ($50),\n * and never $0 — a default of nothing would sell the product for free.\n */\nexport const COMMERCE_DEFAULT_PRICE_USD = 25\n\nexport type ProductType = 'physical' | 'digital' | 'service'\nexport type ProductStatus = 'draft' | 'active' | 'archived'\n\n/** One axis of variation, e.g. { name: 'Size', values: ['S','M','L'] }. */\nexport interface ProductOption {\n name: string\n values: string[]\n}\n\n/** A sellable configuration of a product (every product has ≥ 1). */\nexport interface ProductVariant {\n /** Stable id unique within the product (never reused after delete). */\n id: string\n /** Option selections keyed by option name; {} for the default variant. */\n options?: Record<string, string>\n sku?: string\n barcode?: string\n priceUsd: number\n /** Strike-through price; a sale badge shows when > priceUsd. */\n compareAtPriceUsd?: number\n weightGrams?: number\n /** null/undefined = untracked; 0 = sold out (matches AGL-96). */\n inventory?: number | null\n /**\n * Per-location stock (AGL-286); `inventory` stays the summed\n * denormalization. Absent = single default location.\n */\n inventoryByLocation?: Record<string, number>\n /** Media-library image URL shown when this variant is selected. */\n imageUrl?: string\n}\n\n/** A product's packed size and customs facts (AGL-3612). */\nexport interface ProductShippingFacts {\n lengthCm?: number | null\n widthCm?: number | null\n heightCm?: number | null\n /** Harmonized System tariff code. */\n hsCode?: string\n /** ISO-3166 alpha-2 country the product was made in. */\n originCountry?: string\n}\n\n/** A place's postal address, in parts (AGL-3612). Country is ISO-3166 alpha-2. */\nexport interface PostalAddress {\n line1?: string\n line2?: string\n city?: string\n state?: string\n postalCode?: string\n country?: string\n phone?: string\n}\n\n/** `hosts/{hostId}/locations/{id}` doc (AGL-286). */\nexport interface InventoryLocation {\n name: string\n isDefault?: boolean\n /** Free text, as the location was first described. */\n address?: string\n /**\n * The same place as a postal address (AGL-3612), so parcels can ship from\n * it. Optional: a location written before this has only `address`.\n */\n postalAddress?: PostalAddress\n /** Whether buyers may collect orders here, and when (AGL-3624). */\n pickup?: PickupLocationSettings\n}\n\n/** A product's packed size for one unit, when all three sides are known. */\nexport function productParcelDimensions(\n product: Pick<HostProduct, 'shipping'> | null | undefined,\n): { lengthCm: number; widthCm: number; heightCm: number } | undefined {\n const facts = product?.shipping\n const side = (value: unknown) => {\n const number = Number(value)\n return Number.isFinite(number) && number > 0 ? Math.min(number, 300) : 0\n }\n const lengthCm = side(facts?.lengthCm)\n const widthCm = side(facts?.widthCm)\n const heightCm = side(facts?.heightCm)\n return lengthCm && widthCm && heightCm ? { lengthCm, widthCm, heightCm } : undefined\n}\n\n/**\n * Shipping facts as the product editor stages them, keystroke by keystroke\n * (AGL-3647): {@link normalizeProductShippingFacts}, except that a country\n * of origin being typed keeps its first letter. The editor runs this on\n * every keystroke, and the strict form drops anything short of two letters,\n * so the field could never be typed into. Readers normalize what is stored.\n */\nexport function draftProductShippingFacts(value: unknown): ProductShippingFacts | undefined {\n const facts = normalizeProductShippingFacts(value)\n const typed = String((value as Record<string, unknown> | null)?.['originCountry'] ?? '')\n .trim()\n .toUpperCase()\n if (/^[A-Z]$/.test(typed)) return { ...facts, originCountry: typed }\n return facts\n}\n\n/** A stored product's shipping facts made safe (AGL-3612); never throws. */\nexport function normalizeProductShippingFacts(value: unknown): ProductShippingFacts | undefined {\n if (!value || typeof value !== 'object') return undefined\n const raw = value as Record<string, unknown>\n const facts: ProductShippingFacts = {}\n for (const side of ['lengthCm', 'widthCm', 'heightCm'] as const) {\n const number = Number(raw[side])\n if (raw[side] !== null && raw[side] !== undefined && Number.isFinite(number) && number > 0) {\n facts[side] = Math.min(Math.round(number * 10) / 10, 300)\n }\n }\n const hsCode = String(raw['hsCode'] ?? '').replace(/[^0-9.]/g, '').slice(0, 14)\n if (hsCode) facts.hsCode = hsCode\n const originCountry = String(raw['originCountry'] ?? '').trim().toUpperCase()\n if (/^[A-Z]{2}$/.test(originCountry)) facts.originCountry = originCountry\n return Object.keys(facts).length ? facts : undefined\n}\n\n/** A stored location's postal address made safe (AGL-3612); never throws. */\nexport function normalizePostalAddress(value: unknown): PostalAddress | undefined {\n if (!value || typeof value !== 'object') return undefined\n const raw = value as Record<string, unknown>\n const address: PostalAddress = {}\n for (const field of ['line1', 'line2', 'city', 'state', 'postalCode', 'phone'] as const) {\n const text = String(raw[field] ?? '').trim().slice(0, 120)\n if (text) address[field] = text\n }\n const country = String(raw['country'] ?? '').trim().toUpperCase()\n if (/^[A-Z]{2}$/.test(country)) address.country = country\n return Object.keys(address).length ? address : undefined\n}\n\n/**\n * `hosts/{hostId}/registers/{id}` doc (AGL-472): a named point-of-sale\n * register. Creation is capped by the plan's `posRegisters` quota (Pro 1,\n * Business 2, Advanced 5; raised by a per-org entitlement override for the\n * $89/mo add-on), enforced server-side by the resources route. Each POS\n * sale stamps its `registerId` so takings are attributable per register.\n */\nexport interface PosRegister {\n name: string\n /** Optional default inventory location this register sells from. */\n locationId?: string\n}\n\n/**\n * The register ids within the plan's `posRegisters` cap (AGL-482), ranked\n * by creation order — the same ordering `pos-order.ts` enforces at sale\n * time. Registers beyond the cap (e.g. after a Business→Pro downgrade) are\n * excluded so the console/POS can dim or hide them instead of surfacing a\n * checkout 403. `cap` of `Infinity` returns all.\n */\nexport function registersWithinCap(\n registers: Array<{ $id: string; createdAt?: any }>,\n cap: number,\n): Set<string> {\n const createdMs = (r: { createdAt?: any }) =>\n r.createdAt?.toMillis?.() ?? r.createdAt?.seconds ?? 0\n const ranked = [...registers].sort(\n (a, b) => createdMs(a) - createdMs(b) || a.$id.localeCompare(b.$id),\n )\n return new Set(ranked.slice(0, Math.max(0, cap)).map((r) => r.$id))\n}\n\n/**\n * `hosts/{hostId}/suppliers/{id}` doc (AGL-289): where dropshipped\n * order lines route on payment. Email and webhook are both optional but\n * one must be set for routing to do anything; webhook payloads are\n * HMAC-SHA256-signed with `webhookSecret`.\n */\nexport interface HostSupplier {\n name: string\n email?: string\n webhookUrl?: string\n webhookSecret?: string\n}\n\n/** `hosts/{hostId}/products/{id}` doc. */\nexport interface HostProduct {\n name: string\n /** Host-unique URL segment for /products/{slug}. */\n slug: string\n description?: string\n type: ProductType\n status: ProductStatus\n /** Ordered media-library image URLs (first = primary). */\n mediaUrls?: string[]\n categoryIds?: string[]\n /**\n * The SMART collections this product's rules answer (AGL-3321), written by\n * every writer — see `productCollectionIds`.\n */\n collectionIds?: string[]\n tags?: string[]\n options?: ProductOption[]\n variants: ProductVariant[]\n /** Per-product overrides for PDP meta tags (AGL-299 consumes). */\n seo?: { title?: string; description?: string; imageUrl?: string }\n /**\n * What shopping channels ask of the product (AGL-3637): brand, GTIN, MPN,\n * condition and Google category. Absent means nothing was entered; the\n * channel feeds fall back to the store's defaults.\n */\n channel?: ProductChannelFacts\n /** Supplier for dropship routing (AGL-289). */\n supplierId?: string\n /** Out-of-stock behavior (AGL-281): deny (default) or allow backorder. */\n oversellPolicy?: 'deny' | 'backorder'\n /** Never taxed regardless of tax settings (AGL-285). */\n taxExempt?: boolean\n /**\n * Digital delivery (AGL-302): downloadable files for `digital`\n * products. Buyers always download the CURRENT list, so uploading a\n * new version re-delivers to everyone.\n */\n digitalFiles?: Array<{ url: string; fileName: string; version?: string }>\n /** Max download attempts per order line; absent = unlimited. */\n downloadLimit?: number\n /**\n * Recurring billing (AGL-303): buyers subscribe instead of buying\n * once; an active subscription is the entitlement content gating\n * checks (AGL-309).\n */\n subscription?: { interval: 'month' | 'year'; trialDays?: number }\n /**\n * Buyer-chosen billing (AGL-545): with `subscription` set, true lets\n * the buyer pick one-time OR subscribe on the PDP (default one-time);\n * absent/false keeps the product subscription-only.\n */\n subscriptionOptional?: boolean\n /** Members-only videos (AGL-315), streamed via short-TTL links. */\n gatedVideos?: Array<{ url: string; title?: string }>\n /** Manual related products for the upsell block (AGL-325). */\n relatedProductIds?: string[]\n /**\n * What one unit ships as (AGL-3612): its packed size, for a carrier to\n * price, and its customs facts, for a parcel that crosses a border. Weight\n * stays per variant. Absent means unknown, and a label is priced in the\n * merchant's default box.\n */\n shipping?: ProductShippingFacts\n /** Buying this issues a gift-card code for its price (AGL-322). */\n giftCard?: boolean\n /** Tracked-total at/below this alerts host managers (AGL-281). */\n lowStockThreshold?: number\n /**\n * Choices added to the item at the register (AGL-3607) — \"Oat milk\",\n * \"Extra shot\" — priced by the server from this list. See\n * `product-modifiers.ts`.\n */\n modifierGroups?: ProductModifierGroup[]\n /** Shown under Quick keys on the register's first screen (AGL-3607). */\n posQuickKey?: boolean\n createdAtMs?: number\n updatedAtMs?: number\n deletedAt?: number | null\n /** Legacy Commerce Starter fields kept so old docs read back (AGL-90). */\n priceUsd?: number\n inventory?: number | null\n imageUrl?: string\n}\n\n/** `hosts/{hostId}/productCategories/{id}` doc (tree via parentId). */\nexport interface ProductCategory {\n name: string\n slug: string\n parentId?: string | null\n order?: number\n}\n\nexport type CollectionRuleField =\n | 'tag'\n | 'categoryId'\n | 'priceUsd'\n | 'name'\n | 'type'\nexport type CollectionRuleOp = 'eq' | 'neq' | 'lt' | 'gt' | 'contains'\n\nexport interface CollectionRule {\n field: CollectionRuleField\n op: CollectionRuleOp\n value: string | number\n}\n\n/** `hosts/{hostId}/collections/{id}` doc. */\nexport interface HostCollection {\n name: string\n slug: string\n description?: string\n mode: 'manual' | 'smart'\n /** Manual mode: explicit ordered membership. */\n productIds?: string[]\n /** Smart mode: rules evaluated over active products. */\n rules?: CollectionRule[]\n /** Smart mode: true = every rule must match (default), false = any. */\n matchAll?: boolean\n /** Media-library image URL for the collection card/landing hero. */\n imageUrl?: string\n order?: number\n}\n\n/**\n * Payment-provider seam (AGL-284): checkout/session creation goes\n * through a provider id so PayPal (etc.) can slot in without reworking\n * callers. Stripe is the only implementation today; store settings\n * (AGL-295) will carry the selection when a second provider exists.\n */\nexport type PaymentProviderId = 'stripe'\n\nexport interface PaymentCheckoutRequest {\n provider: PaymentProviderId\n hostId: string\n amountCents: number\n feeCents: number\n productName: string\n successUrl: string\n cancelUrl: string\n metadata?: Record<string, string>\n}\n\n/** Variant ceiling per product (Shopify parity). */\nexport const COMMERCE_MAX_VARIANTS = 100\nexport const COMMERCE_MAX_OPTIONS = 3\nexport const COMMERCE_MAX_OPTION_VALUES = 25\nexport const COMMERCE_SLUG_MAX_LENGTH = 80\n\n/** Lowercase-kebab slug from a product/category/collection name. */\nexport function commerceSlug(name: string): string {\n return String(name ?? '')\n .normalize('NFKD')\n .replace(/[̀-ͯ]/g, '')\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, COMMERCE_SLUG_MAX_LENGTH)\n}\n\n/**\n * The search keys a product write carries, so the catalog can be searched by\n * the QUERY rather than by the rows a listener happened to fetch (AGL-2501).\n *\n * The hub lists `limit(500)`, and the search compared what that returned — so\n * a product past the window answered \"no products match\", which reads as the\n * product not existing rather than as the search not reaching it. Filtering\n * server-side needs fields Firestore can index, and neither of the two things\n * a merchant searches by is one:\n *\n * - A NAME needs case-insensitive contains, which Firestore has no operator\n * for. `nameSearchFields` denormalizes it into the three shapes that are\n * indexable — an exact key, word-prefix tokens for `array-contains`, and\n * the key reversed so \"ends with\" becomes a prefix range.\n * - A SKU lives inside `variants`, an array of OBJECTS, and Firestore cannot\n * query a field inside one. Flattened here into a top-level array so a SKU\n * is one `array-contains`.\n *\n * ⛔ A SKU therefore matches WHOLE, not as a substring: \"abc-123\" finds it and\n * \"123\" does not. That is the right way round for a SKU, which is a value\n * somebody copies rather than half-remembers — unlike a name, where the tokens\n * above buy word-prefix matching precisely because names are half-remembered.\n *\n * `barcodes` is the same flattening for the same reason, and it is the one the\n * register depends on: a keyboard-wedge scanner types the code and presses\n * Enter, so the lookup has to be exact and has to reach the whole catalog. A\n * scan is never a half-remembered value.\n *\n * Lower-cased because the translator lower-cases the typed query before it\n * builds the `array-contains`; stored and typed have to be normalized the same\n * way or the two silently disagree.\n *\n * ⚠️ Each array is OMITTED when a product has none, rather than written as\n * `[]`. `isNotEmpty` is served as `!= null`, and an empty array is not null —\n * so a product with no SKUs at all would answer \"has a SKU\" for every row.\n *\n * `priceFromCents` is the storefront's price key (AGL-3321): the price a\n * product grid card shows — \"From $X\", the lowest variant price — in whole\n * cents, so the catalog's price slider and its price sorts are a range and an\n * `orderBy` on one stored number rather than a comparison over variants no\n * query can reach. Written only when the variants are, because it is derived\n * from them: a write that carries no variants must not stamp a price.\n *\n * ⚠️ Spread this at EVERY write that sets a product's name or variants. A\n * write that sets the name without it leaves the keys describing the previous\n * name, and the product becomes findable only by what it used to be called.\n */\nexport function productSearchFields(product: {\n name: string\n variants?: ProductVariant[]\n}): {\n name: string\n nameLower: string\n nameTokens: string[]\n nameReversed: string\n skus?: string[]\n barcodes?: string[]\n priceFromCents?: number\n} {\n const flatten = (read: (variant: ProductVariant) => string | undefined) => [\n ...new Set(\n (product.variants ?? [])\n .map((variant) => (read(variant) ?? '').trim().toLowerCase())\n .filter(Boolean),\n ),\n ]\n const skus = flatten((variant) => variant.sku)\n const barcodes = flatten((variant) => variant.barcode)\n return {\n ...nameSearchFields(product.name),\n ...(skus.length ? { skus } : {}),\n ...(barcodes.length ? { barcodes } : {}),\n ...(product.variants\n ? { priceFromCents: productPriceFromCents({ variants: product.variants }) }\n : {}),\n }\n}\n\n/**\n * The price a storefront card shows, in whole cents (AGL-3321): the low end\n * of the variant range, which is what \"From $X\" prints and what the catalog's\n * price filter and price sorts compare. Stored as `priceFromCents` by\n * {@link productSearchFields}; `0` for a product with no priced variant yet.\n */\nexport function productPriceFromCents(\n product: Pick<HostProduct, 'variants'>,\n): number {\n return Math.round(productPriceRange(product)[0] * 100)\n}\n\n/**\n * The stock keys a product write carries (AGL-3321): the flat `inventory`\n * total, and `soldOut` — whether a storefront card reads \"Sold out\", which is\n * what the product grid's In stock chip asks the catalog query for\n * (`soldOut == false`). A grid cannot ask a query about stock inside\n * `variants`, so the verdict is stored beside them.\n *\n * ⚠️ Spread this at EVERY write that changes `variants` or `oversellPolicy`:\n * a sale, a cancellation, a stock adjustment and the editor alike. A write\n * that moves stock without it leaves a sold-out product under In stock, or an\n * in-stock one missing from it. Stored by `backfill-products-list-fields.mjs`\n * on the products written before it.\n */\nexport function productStockFields(\n product: Pick<HostProduct, 'variants' | 'oversellPolicy'>,\n): { inventory: number | null; soldOut: boolean } {\n const inventory = productInventory(product)\n return {\n inventory,\n soldOut: inventory != null && inventory <= 0 && product.oversellPolicy !== 'backorder',\n }\n}\n\n/**\n * Cartesian product of option values → the variant option combinations\n * the products hub materializes into a variants matrix. Empty/absent\n * options yield a single default combination ({}).\n */\nexport function expandVariantMatrix(\n options: ProductOption[] | undefined,\n): Array<Record<string, string>> {\n const usable = (options ?? []).filter(\n (option) => option.name && (option.values?.length ?? 0) > 0,\n )\n if (usable.length === 0) return [{}]\n let combos: Array<Record<string, string>> = [{}]\n for (const option of usable) {\n const next: Array<Record<string, string>> = []\n for (const combo of combos) {\n for (const value of option.values) {\n next.push({ ...combo, [option.name]: value })\n }\n }\n combos = next\n if (combos.length > COMMERCE_MAX_VARIANTS) {\n return combos.slice(0, COMMERCE_MAX_VARIANTS)\n }\n }\n return combos\n}\n\n/**\n * The option axes renamed, with every variant's selection carried to the new\n * name in place (AGL-3066).\n *\n * A variant keeps its selections keyed by option NAME, so a rename has to move\n * each key rather than rebuild the matrix: a rebuild matches variants by name\n * and value, finds none under the new name, and replaces every variant with a\n * new id holding the first variant's price and stock and no SKU, barcode,\n * compare-at price or weight. Moving the key keeps the id every order line\n * and stock movement refers to, and every field a person set.\n *\n * `names` holds one entry per option, in order; an entry that is not a string\n * keeps that option's name. Each selection keeps its position among a\n * variant's selections, because a variant is labeled by them in order.\n *\n * Two options may share a name for a moment while a person types. A variant\n * cannot hold two selections under one name, so while the new names are not\n * all different nothing is moved, and `validateProduct` refuses the product.\n * The selection left under the old name is found again by its value once the\n * names differ, so typing through a duplicate loses nothing.\n */\nexport function renameProductOptions(\n product: Pick<HostProduct, 'options' | 'variants'>,\n names: ReadonlyArray<string | null | undefined>,\n): Pick<HostProduct, 'options' | 'variants'> {\n const current = product.options ?? []\n const options = current.map((option, index) => {\n const name = names[index]\n return typeof name === 'string' ? { ...option, name } : option\n })\n const next = options.map((option) => option.name)\n if (new Set(next).size !== next.length) return { options, variants: product.variants }\n const variants = (product.variants ?? []).map((variant) => {\n const selections = variant.options\n if (!selections) return variant\n const axisOf = optionAxesOf(current, selections)\n const moved: Record<string, string> = {}\n let changed = false\n for (const [key, value] of Object.entries(selections)) {\n const axis = axisOf.get(key)\n const name = axis === undefined ? key : next[axis]\n if (name !== key) changed = true\n moved[name] = value\n }\n return changed ? { ...variant, options: moved } : variant\n })\n return { options, variants }\n}\n\n/**\n * Which option each of a variant's selections belongs to: the option of the\n * same name first, and then, for a selection under a name no option has any\n * longer, the one remaining option whose values hold its value. A selection\n * that fits neither belongs to no option and is left as it is.\n */\nfunction optionAxesOf(\n options: readonly ProductOption[],\n selections: Readonly<Record<string, string>>,\n): Map<string, number> {\n const axisOf = new Map<string, number>()\n const taken = new Set<number>()\n options.forEach((option, index) => {\n if (Object.prototype.hasOwnProperty.call(selections, option.name) && !axisOf.has(option.name)) {\n axisOf.set(option.name, index)\n taken.add(index)\n }\n })\n for (const [key, value] of Object.entries(selections)) {\n if (axisOf.has(key)) continue\n const axis = options.findIndex(\n (option, index) => !taken.has(index) && (option.values ?? []).includes(value),\n )\n if (axis === -1) continue\n axisOf.set(key, axis)\n taken.add(axis)\n }\n return axisOf\n}\n\n/**\n * Whether a variant has a price a sale can charge (AGL-2916).\n *\n * A product may be stored with a price left empty on purpose (an owner who\n * clears it, or a writer asked for none), so a stored variant can lack one. `Number(undefined)` is\n * no price, and neither is `null`, which `Number` reads as zero: a sale door\n * that priced either would charge nothing, or fail at the processor.\n */\nexport function variantHasPrice(variant: object | null | undefined): boolean {\n const price = (variant as { priceUsd?: unknown } | null | undefined)?.priceUsd\n return typeof price === 'number' && Number.isFinite(price) && price >= 0\n}\n\n/** Whether any variant of a product still needs its price set (AGL-2916). */\nexport function productPriceMissing(product: { variants?: readonly object[] }): boolean {\n return (product.variants ?? []).some((variant) => !variantHasPrice(variant))\n}\n\n/** Copy proposed for a product, by field (AGL-2916). A field left out is left alone. */\nexport interface ProductCopyValues {\n description?: string\n tags?: string[]\n categoryIds?: string[]\n /** One name per option the product has, in order. */\n optionNames?: string[]\n seoTitle?: string\n seoDescription?: string\n /**\n * Packed size and customs facts (AGL-3612), merged over what the product\n * has; a side set to `null` is cleared.\n */\n shipping?: Partial<ProductShippingFacts>\n /**\n * Shopping-channel facts (AGL-3637), merged over what the product has; a\n * field set to `''` is cleared.\n */\n channel?: { [Field in keyof ProductChannelFacts]?: string }\n}\n\n/**\n * The fields proposed copy changes on a product (AGL-2916), in the shape the\n * editor stores them: only the fields `values` names. Tags and categories are\n * each kept once; the search listing keeps its share image; and option names\n * rename the product's options through `renameProductOptions`, keeping every\n * variant, only when they name each option once with a name of its own.\n */\nexport function productCopyPatch(\n product: Pick<HostProduct, 'options' | 'variants' | 'seo' | 'shipping'> &\n Pick<Partial<HostProduct>, 'channel'>,\n values: ProductCopyValues,\n): Partial<\n Pick<HostProduct, 'description' | 'tags' | 'categoryIds' | 'seo' | 'options' | 'variants' | 'shipping' | 'channel'>\n> {\n const patch: Partial<\n Pick<HostProduct, 'description' | 'tags' | 'categoryIds' | 'seo' | 'options' | 'variants' | 'shipping' | 'channel'>\n > = {}\n if (values.shipping && typeof values.shipping === 'object') {\n // Stored whole, never `undefined`: the editor's save is a full `setDoc`,\n // which refuses an undefined field. Nothing known is an empty map.\n patch.shipping = draftProductShippingFacts({ ...product.shipping, ...values.shipping }) ?? {}\n }\n if (values.channel && typeof values.channel === 'object') {\n // Stored whole, never `undefined`, for the same reason. Nothing entered\n // is an empty map. Kept as typed: this runs on every keystroke.\n patch.channel = draftProductChannelFacts({ ...product.channel, ...values.channel }) ?? {}\n }\n if (typeof values.description === 'string') patch.description = values.description\n if (Array.isArray(values.tags)) {\n patch.tags = [...new Set(values.tags.map((tag) => tag.trim()).filter(Boolean))]\n }\n if (Array.isArray(values.categoryIds)) {\n patch.categoryIds = [...new Set(values.categoryIds.filter(Boolean))]\n }\n if (typeof values.seoTitle === 'string' || typeof values.seoDescription === 'string') {\n patch.seo = {\n ...product.seo,\n ...(typeof values.seoTitle === 'string' ? { title: values.seoTitle } : {}),\n ...(typeof values.seoDescription === 'string' ? { description: values.seoDescription } : {}),\n }\n }\n const options = product.options ?? []\n const names = values.optionNames?.map((name) => name.trim())\n if (\n names &&\n options.length > 0 &&\n names.length === options.length &&\n names.every(Boolean) &&\n new Set(names).size === names.length &&\n names.some((name, index) => name !== options[index].name)\n ) {\n Object.assign(patch, renameProductOptions(product, names))\n }\n return patch\n}\n\n/** A product proposed for a store, before anyone has priced it (AGL-2916). */\nexport interface ProposedProduct {\n name: string\n type: ProductType\n description: string\n tags: string[]\n options: ProductOption[]\n seoTitle: string\n seoDescription: string\n}\n\n/** A new product whose variants carry no price yet. */\nexport type UnpricedProductDraft = Omit<HostProduct, 'variants'> & {\n variants: Array<Omit<ProductVariant, 'priceUsd'>>\n}\n\n/**\n * The document a proposed product is created as (AGL-2916): a `draft`, under\n * a slug no product in `takenSlugs` has, with one variant for each\n * combination of its options and NO price on any of them. Prices are the\n * merchant's to set: the editor refuses to save the product until every\n * variant has one, and no sale door sells a variant without one\n * (`variantHasPrice`). The search keys travel with the name, as on every\n * create path.\n */\nexport function unpricedProductDraft(\n proposal: ProposedProduct,\n takenSlugs: ReadonlySet<string>,\n nowMs: number,\n): UnpricedProductDraft {\n const name = proposal.name.trim().slice(0, 120)\n const base = commerceSlug(name) || 'product'\n let slug = base\n for (let suffix = 2; takenSlugs.has(slug); suffix += 1) slug = `${base}-${suffix}`\n const seen = new Set<string>()\n const options = proposal.options\n .map((option) => ({\n name: option.name.trim(),\n values: [...new Set(option.values.map((value) => value.trim()).filter(Boolean))].slice(\n 0,\n COMMERCE_MAX_OPTION_VALUES,\n ),\n }))\n .filter((option) => {\n if (!option.name || !option.values.length || seen.has(option.name)) return false\n seen.add(option.name)\n return true\n })\n .slice(0, COMMERCE_MAX_OPTIONS)\n const variants = options.length\n ? expandVariantMatrix(options).map((combo, index) => ({\n id: `v${nowMs.toString(36)}${index}`,\n options: combo,\n }))\n : [{ id: 'default' }]\n const seoTitle = proposal.seoTitle.trim()\n const seoDescription = proposal.seoDescription.trim()\n return {\n // The keys for the variants as created: no price is `priceFromCents: 0`\n // and no tracked stock is never sold out, so the draft is ordered and\n // filtered like every other product once it is priced and activated.\n ...productSearchFields({ name, variants: variants as ProductVariant[] }),\n ...productStockFields({ variants: variants as ProductVariant[] }),\n slug,\n type: proposal.type,\n status: 'draft',\n description: proposal.description,\n tags: [...new Set(proposal.tags.map((tag) => tag.trim()).filter(Boolean))],\n ...(options.length ? { options } : {}),\n variants,\n ...(seoTitle || seoDescription\n ? { seo: { ...(seoTitle ? { title: seoTitle } : {}), ...(seoDescription ? { description: seoDescription } : {}) } }\n : {}),\n createdAtMs: nowMs,\n updatedAtMs: nowMs,\n }\n}\n\n/**\n * A proposed product as a new product is created from it (AGL-3676): the\n * draft `unpricedProductDraft` builds, with `priceUsd` on every variant —\n * the default price unless the caller states one — and the search keys\n * worked out again for that price. `null` leaves it unpriced on purpose.\n */\nexport function defaultPricedProductDraft(\n proposal: ProposedProduct,\n takenSlugs: ReadonlySet<string>,\n nowMs: number,\n priceUsd: number | null = COMMERCE_DEFAULT_PRICE_USD,\n): UnpricedProductDraft | HostProduct {\n const draft = unpricedProductDraft(proposal, takenSlugs, nowMs)\n if (priceUsd === null) return draft\n const variants = draft.variants.map((variant) => ({ ...variant, priceUsd })) as ProductVariant[]\n return { ...draft, ...productSearchFields({ name: draft.name, variants }), variants }\n}\n\n/**\n * The price a proposed product is created at (Zach, 2026-10-12): a price an\n * AI proposal suggests for what the product is, when it is a real one — a\n * finite amount above $0, at most `COMMERCE_MAX_PRICE_USD`, rounded to whole\n * cents — else the default price (`COMMERCE_DEFAULT_PRICE_USD`). Never $0,\n * negative or past the ceiling, whatever a proposal carries; the owner\n * changes it like any price.\n */\nexport function proposedProductPriceUsd(suggested: unknown): number {\n if (typeof suggested !== 'number' || !Number.isFinite(suggested)) return COMMERCE_DEFAULT_PRICE_USD\n const cents = Math.round(suggested * 100)\n return cents > 0 && cents <= COMMERCE_MAX_PRICE_USD * 100 ? cents / 100 : COMMERCE_DEFAULT_PRICE_USD\n}\n\n/** Variant whose option selections match exactly; undefined if none. */\nexport function findVariant(\n product: Pick<HostProduct, 'variants'>,\n selections: Record<string, string>,\n): ProductVariant | undefined {\n return product.variants?.find((variant) => {\n const options = variant.options ?? {}\n const keys = new Set([...Object.keys(options), ...Object.keys(selections)])\n for (const key of keys) {\n if (options[key] !== selections[key]) return false\n }\n return true\n })\n}\n\n/** [min, max] price across variants; [0, 0] when there are none. */\nexport function productPriceRange(\n product: Pick<HostProduct, 'variants'>,\n): [number, number] {\n const prices = (product.variants ?? [])\n .map((variant) => Number(variant.priceUsd))\n .filter((price) => Number.isFinite(price) && price >= 0)\n if (prices.length === 0) return [0, 0]\n return [Math.min(...prices), Math.max(...prices)]\n}\n\n/** Sum of tracked inventory; null when every variant is untracked. */\nexport function productInventory(\n product: Pick<HostProduct, 'variants'>,\n): number | null {\n let total: number | null = null\n for (const variant of product.variants ?? []) {\n if (variant.inventory == null) continue\n total = (total ?? 0) + Number(variant.inventory)\n }\n return total\n}\n\n/**\n * Reasons an inventory adjustment doc may carry (AGL-281).\n *\n * `cancellation` is the only one written automatically in the PLUS direction\n * (AGL-1808): cancelling an order the transition rules prove was never\n * fulfilled puts back exactly what the sale took. It is deliberately not\n * `refund` — no money moved — and not `correction`, which is what a merchant\n * picks when the count disagreed with the shelf for a reason nobody recorded.\n * Kept out of the \"Adjust stock\" dialog's menu on purpose: a merchant reaching\n * for it by hand means something else happened.\n *\n * `sync` is a count another warehouse reported (AGL-3634): a fulfillment\n * network holding the goods said how many it can ship, and the count was set\n * to that. Written only through core's `core.stock-levels`, with `source`\n * naming who counted, and kept out of the dialog's menu for the same reason.\n */\nexport type InventoryAdjustmentReason =\n 'sale' | 'refund' | 'restock' | 'correction' | 'damage' | 'cancellation' | 'sync'\n\n/** `hosts/{hostId}/inventoryAdjustments/{id}` doc. */\nexport interface InventoryAdjustment {\n productId: string\n variantId: string\n /** Positive = stock added, negative = removed. */\n delta: number\n /**\n * What the count ACTUALLY moved by, when the floor in\n * `adjustVariantInventory` absorbed part of `delta` (AGL-2149). Written only\n * when the two differ, which for a sale means a backorder product that sold\n * past zero: `delta` stays the units sold — that is the merchant's stock\n * history and it must keep saying \"3 went out the door\" — while this says how\n * many of them the count could give up.\n *\n * A REVERSAL MUST READ THIS, NOT `delta`. Restoring `delta` on a backorder\n * sale that the floor swallowed invents inventory that never existed: 0 sells\n * 3, the count stays 0, and a `+3` on cancellation leaves 3 units nobody has.\n * Absent on every row written before AGL-2149 and on every row where nothing\n * was clamped, so `appliedDelta ?? delta` is the reader.\n */\n appliedDelta?: number\n reason: InventoryAdjustmentReason\n /** Order id for sale/refund adjustments. */\n orderId?: string\n /** Location for multi-location stock (AGL-286); absent = default. */\n locationId?: string\n /**\n * Who counted, for a `sync` row: the warehouse that reported it (AGL-3634);\n * for a `sale` another channel made, that channel (AGL-3638).\n */\n source?: string\n atMs: number\n}\n\n/**\n * Whether `quantity` of a variant is purchasable (AGL-281): untracked\n * stock always is; tracked stock honors the product's oversell policy.\n */\nexport function canPurchase(\n product: Pick<HostProduct, 'variants' | 'oversellPolicy'>,\n variantId: string | undefined,\n quantity = 1,\n): boolean {\n const variant = variantId\n ? product.variants?.find((item) => item.id === variantId)\n : product.variants?.[0]\n if (!variant) return false\n if (variant.inventory == null) return true\n if (product.oversellPolicy === 'backorder') return true\n return Number(variant.inventory) >= quantity\n}\n\n/**\n * What the register has to SAY about a line it is about to sell (AGL-2357).\n *\n * ## The defect\n *\n * `pos-order.ts` contains no `canPurchase` call. Every storefront door gates on\n * it; the register does not, so a merchant who set `oversellPolicy: 'deny'` in\n * the product editor silently got `backorder` at the counter — the count floors\n * at zero underneath and nothing anywhere says so.\n *\n * ## The decision, and why it is a shortfall and not a refusal\n *\n * Warn, never block. A till is the wrong place for a stale number to stop a\n * real sale: the cashier is holding the goods, the physical shelf is the truth,\n * and refusing loses a sale over data that is behind. But \"deny\" must at least\n * SAY something. So this reports the shortfall and the caller sells anyway.\n * Honouring the policy with a manager-level override is the post-launch shape\n * (AGL-2372) and wants an audit trail this deliberately does not build.\n *\n * ## Why the test is `canPurchase`\n *\n * Exactly the gate the register was missing, and no wider. An untracked variant\n * has no number to be short against, and a `backorder` product's merchant chose\n * to sell past zero — warning them would be noise about a setting they set on\n * purpose. So a shortfall is reported only where a purchase would have been\n * refused anywhere else in the product.\n */\nexport interface StockShortfall {\n productId: string\n variantId?: string\n /** Product name at the time of sale, for a message the cashier can read. */\n name: string\n variantLabel?: string\n /** Units the cashier is ringing up. */\n requested: number\n /** Units the count says are on the shelf; never negative. */\n available: number\n}\n\nexport function stockShortfall(\n product: Pick<HostProduct, 'variants' | 'oversellPolicy'>,\n variantId: string | undefined,\n quantity = 1,\n): { available: number } | null {\n if (canPurchase(product, variantId, quantity)) return null\n const variant = variantId\n ? product.variants?.find((item) => item.id === variantId)\n : product.variants?.[0]\n // `canPurchase` answers false for a variant that does not exist at all. That\n // is not a stock shortfall and must not be reported as \"0 in stock\" — the\n // register's own line builder falls back to `variants[0]`, so the case is\n // unreachable from the counter, and inventing a count for it would be the\n // one failure mode this feature must not have.\n if (!variant || variant.inventory == null) return null\n return { available: Math.max(0, Math.round(Number(variant.inventory) || 0)) }\n}\n\n/**\n * Whether stock tracking MEANS anything on a product (AGL-1744).\n *\n * `canPurchase` above gates every checkout on stock, subscription sessions\n * included, but nothing ever decrements for a subscription — not on the\n * initial charge (the `commerce-subscription` webhook branch deliberately\n * writes no order and touches no inventory, AGL-1732) and not on renewal\n * (`invoice.payment_succeeded` is unhandled repo-wide, AGL-1743). So on a\n * subscription-only product that gate is permanently satisfied: one unit of\n * stock sells an unlimited number of subscriptions, and the count never\n * moves however many boxes ship. The merchant set a number, the system\n * checks it on every sale, and the number is never true.\n *\n * AGL-1750 resolved the half AGL-1744 had to leave open: every paid invoice\n * of a PHYSICAL subscription now mints an order and decrements its variant's\n * stock (the `invoice.paid` branch of the commerce webhook), opening cycle\n * included. So the honest split is now three-way:\n *\n * - `subscriptionOptional` (\"Both\") products have a real one-time path —\n * the cart only ever builds `mode: 'payment'` sessions, and buy-now with\n * `billing: 'once'` records a plain order — and that path decrements\n * honestly. Withdrawing stock tracking there would delete a working\n * control and re-open the AGL-1711 oversell on the half that works.\n * - a PHYSICAL subscription box consumes a unit every cycle, and since\n * AGL-1750 the cycle's own order decrements it — the count moves as the\n * boxes ship, so the number the merchant sets is kept true again.\n * - a DIGITAL or SERVICE subscription-only product still has no\n * decrementing path anywhere, so the field stays withdrawn there: one\n * unit would still sell unlimited subscriptions.\n *\n * This says nothing about what `canPurchase` returns — that is the gate for\n * every other purchase path and is deliberately untouched. This is a console\n * predicate: it stops the editor inviting a merchant to set a number the\n * system will never keep true.\n */\nexport function stockTrackingApplies(\n product: Pick<HostProduct, 'subscription' | 'subscriptionOptional' | 'type'>,\n): boolean {\n return (\n !product.subscription ||\n product.subscriptionOptional === true ||\n product.type === 'physical'\n )\n}\n\n/** Buyer's requested billing mode on a checkout POST (AGL-545). */\nexport type CheckoutBillingChoice = 'once' | 'subscribe'\n\n/**\n * Resolves the Stripe Checkout mode for a product + the buyer's\n * requested billing choice (AGL-545). The product doc is authoritative:\n * - no `subscription` → always one-time (the request field is ignored)\n * - `subscription` without `subscriptionOptional` → always subscription\n * (a forged `billing: 'once'` cannot buy a subscription product once)\n * - `subscriptionOptional` → the buyer chooses; absent/invalid choices\n * default to one-time, matching the PDP default.\n */\nexport function resolveCheckoutBillingMode(\n product: Pick<HostProduct, 'subscription' | 'subscriptionOptional'>,\n requested?: string | null,\n): 'payment' | 'subscription' {\n if (!product.subscription) return 'payment'\n if (!product.subscriptionOptional) return 'subscription'\n return requested === 'subscribe' ? 'subscription' : 'payment'\n}\n\n/**\n * Applies a stock delta to one variant, flooring at zero (race-window\n * sales can't drive the display negative), and returns the new variants\n * array — callers persist it plus the `productInventory` denormalization.\n * With `locationId`, the delta lands in that location's bucket and the\n * flat `inventory` re-sums across locations (AGL-286).\n */\nexport function adjustVariantInventory(\n product: Pick<HostProduct, 'variants'>,\n variantId: string,\n delta: number,\n locationId?: string,\n): ProductVariant[] {\n return (product.variants ?? []).map((variant) => {\n if (variant.id !== variantId || variant.inventory == null) return variant\n if (locationId && variant.inventoryByLocation) {\n const buckets = {\n ...variant.inventoryByLocation,\n [locationId]: Math.max(\n 0,\n Number(variant.inventoryByLocation[locationId] ?? 0) + delta,\n ),\n }\n return {\n ...variant,\n inventoryByLocation: buckets,\n inventory: Object.values(buckets).reduce(\n (sum, count) => sum + Number(count),\n 0,\n ),\n }\n }\n return {\n ...variant,\n inventory: Math.max(0, Number(variant.inventory) + delta),\n }\n })\n}\n\n/**\n * What `adjustVariantInventory` will ACTUALLY move the count by (AGL-2149).\n *\n * The floor in that helper is not a rounding detail, it is a silent absorber. A\n * backorder product (`oversellPolicy: 'backorder'`, which `canPurchase` admits\n * at any stock level) sitting at 0 sells 3: `Math.max(0, 0 + -3)` is 0, so the\n * count does not move — correctly, since inventory is a shelf count and shelves\n * do not go negative — but the ledger row said `-3` and every reversal read it\n * as three units to put back. Cancelling that order handed the merchant 3 units\n * that never existed and a stock badge that offers them for sale.\n *\n * Removing the floor is not the alternative: every reader of `inventory` — the\n * badges, `canPurchase`, `productInventory`, `isLowStock` — assumes it cannot\n * go negative, and a negative count would be a far wider change than the bug.\n * So the clamp stays and the amount it absorbed is recorded instead.\n *\n * Deliberately mirrors `adjustVariantInventory`'s own arithmetic, including\n * which field the clamp applies to in the location-tracked case, so the two\n * cannot disagree about what happened.\n */\nexport function appliedVariantInventoryDelta(\n product: Pick<HostProduct, 'variants'>,\n variantId: string,\n delta: number,\n locationId?: string,\n): number {\n const variant = (product.variants ?? []).find(\n (item) => item.id === variantId,\n )\n if (!variant || variant.inventory == null) return 0\n if (locationId && variant.inventoryByLocation) {\n const before = Number(variant.inventoryByLocation[locationId] ?? 0)\n return Math.max(0, before + delta) - before\n }\n const before = Number(variant.inventory)\n return Math.max(0, before + delta) - before\n}\n\n/**\n * Moves stock between two locations of a tracked variant (AGL-286).\n * Quantity clamps to what the source location holds; the flat total is\n * unchanged by construction.\n */\nexport function transferVariantInventory(\n product: Pick<HostProduct, 'variants'>,\n variantId: string,\n fromLocationId: string,\n toLocationId: string,\n quantity: number,\n): ProductVariant[] {\n return (product.variants ?? []).map((variant) => {\n if (variant.id !== variantId || variant.inventory == null) return variant\n const buckets = { ...(variant.inventoryByLocation ?? {}) }\n const available = Math.max(0, Number(buckets[fromLocationId] ?? 0))\n const moved = Math.min(available, Math.max(0, Math.round(quantity)))\n if (moved === 0) return variant\n buckets[fromLocationId] = available - moved\n buckets[toLocationId] = Math.max(0, Number(buckets[toLocationId] ?? 0)) + moved\n return { ...variant, inventoryByLocation: buckets }\n })\n}\n\n/** True when tracked stock is at/below the product's alert threshold. */\nexport function isLowStock(\n product: Pick<HostProduct, 'variants' | 'lowStockThreshold'>,\n): boolean {\n const threshold = product.lowStockThreshold\n if (threshold == null || !(threshold >= 0)) return false\n const total = productInventory(product)\n return total != null && total <= threshold\n}\n\nfunction ruleMatches(product: HostProduct, rule: CollectionRule): boolean {\n const value = rule.value\n switch (rule.field) {\n case 'tag': {\n const tags = product.tags ?? []\n const has = tags.includes(String(value))\n return rule.op === 'neq' ? !has : has\n }\n case 'categoryId': {\n const ids = product.categoryIds ?? []\n const has = ids.includes(String(value))\n return rule.op === 'neq' ? !has : has\n }\n case 'priceUsd': {\n const [min, max] = productPriceRange(product)\n const numeric = Number(value)\n if (!Number.isFinite(numeric)) return false\n if (rule.op === 'lt') return min < numeric\n if (rule.op === 'gt') return max > numeric\n if (rule.op === 'neq') return min !== numeric || max !== numeric\n return min <= numeric && numeric <= max\n }\n case 'name': {\n const name = (product.name ?? '').toLowerCase()\n const needle = String(value).toLowerCase()\n if (rule.op === 'contains') return name.includes(needle)\n if (rule.op === 'neq') return name !== needle\n return name === needle\n }\n case 'type': {\n const matches = product.type === value\n return rule.op === 'neq' ? !matches : matches\n }\n default:\n return false\n }\n}\n\n/**\n * Whether a product's own fields answer a SMART collection's rules — its\n * tags, categories, prices, name and type, and nothing about whether it is\n * on sale. That is the storefront query's scope (`deletedAt == null`,\n * `status == 'active'`), so membership stored on the product never goes stale\n * when a product is archived or brought back.\n *\n * A collection with no rules holds nothing.\n */\n/** What smart-collection membership reads of a product — a draft's too. */\nexport interface MembershipProduct {\n name?: string\n type?: ProductType\n tags?: string[]\n categoryIds?: string[]\n variants?: ReadonlyArray<{ id?: string; priceUsd?: number | null }>\n}\n\nexport function smartCollectionMatches(\n product: MembershipProduct,\n collection: Pick<HostCollection, 'rules' | 'matchAll'>,\n): boolean {\n const rules = collection.rules ?? []\n if (rules.length === 0) return false\n const matcher = (rule: CollectionRule) => ruleMatches(product as HostProduct, rule)\n return collection.matchAll === false\n ? rules.some(matcher)\n : rules.every(matcher)\n}\n\n/**\n * Smart-collection membership: draft/archived/deleted products never\n * match; manual collections answer from productIds.\n */\nexport function matchesCollection(\n product: HostProduct,\n collection: HostCollection,\n productId?: string,\n): boolean {\n if (product.deletedAt || product.status !== 'active') return false\n if (collection.mode === 'manual') {\n return productId != null &&\n (collection.productIds ?? []).includes(productId)\n }\n return smartCollectionMatches(product, collection)\n}\n\n/**\n * The stored membership of a product in the host's SMART collections\n * (AGL-3321): the ids of those whose rules it answers\n * ({@link smartCollectionMatches}), sorted.\n *\n * The storefront reads a smart collection whose rules no query can hold (a\n * NOT, an OR, a name rule, a price over) as `collectionIds array-contains\n * <id>` — so the membership is WRITTEN, by every product writer from the\n * host's smart collections as they stand, and by the collection-membership\n * route for every product whenever a smart collection's rules change or it\n * is deleted. `tools/scripts/backfill-product-collection-ids.mjs` stamps the\n * products written before; the two answer\n * `tools/scripts/lib/product-collection-ids.fixtures.json`.\n */\nexport const PRODUCT_COLLECTION_IDS = 'collectionIds'\n\n/** A smart collection as membership is computed from it. */\nexport interface SmartCollectionRules {\n id: string\n rules?: CollectionRule[]\n matchAll?: boolean\n}\n\nexport function productCollectionIds(\n product: MembershipProduct,\n smartCollections: readonly SmartCollectionRules[],\n): string[] {\n return smartCollections\n .filter((collection) => smartCollectionMatches(product, collection))\n .map((collection) => collection.id)\n .sort()\n}\n\n/**\n * Lifts a legacy Commerce Starter doc (AGL-90: flat name/priceUsd/\n * inventory/imageUrl) into the catalog shape with a single default\n * variant. Already-lifted docs pass through unchanged.\n */\nexport function liftLegacyProduct(\n raw: Partial<HostProduct> & { name?: string },\n): HostProduct {\n if (Array.isArray(raw.variants) && raw.variants.length > 0) {\n return raw as HostProduct\n }\n const priceUsd = Number(raw.priceUsd ?? 0)\n return {\n ...raw,\n name: raw.name ?? 'Product',\n slug: raw.slug || commerceSlug(raw.name ?? 'product'),\n type: raw.type ?? 'physical',\n status: raw.status ?? 'active',\n variants: [\n {\n id: 'default',\n priceUsd: Number.isFinite(priceUsd) ? priceUsd : 0,\n inventory: raw.inventory ?? null,\n },\n ],\n }\n}\n\n/** Actionable error, or null when the product is storable. */\nexport function validateProduct(product: HostProduct): string | null {\n if (!product.name?.trim()) return 'Product name is required'\n if (!product.slug || product.slug !== commerceSlug(product.slug)) {\n return 'Slug must be lowercase letters, numbers, and dashes'\n }\n if (!['physical', 'digital', 'service'].includes(product.type)) {\n return 'Unknown product type'\n }\n if (!['draft', 'active', 'archived'].includes(product.status)) {\n return 'Unknown product status'\n }\n const options = product.options ?? []\n if (options.length > COMMERCE_MAX_OPTIONS) {\n return `At most ${COMMERCE_MAX_OPTIONS} options per product`\n }\n for (const option of options) {\n if (!option.name?.trim()) return 'Option names are required'\n if ((option.values?.length ?? 0) === 0) {\n return `Option \"${option.name}\" needs at least one value`\n }\n if (option.values.length > COMMERCE_MAX_OPTION_VALUES) {\n return `Option \"${option.name}\" has too many values`\n }\n if (new Set(option.values).size !== option.values.length) {\n return `Option \"${option.name}\" has duplicate values`\n }\n }\n // A variant keys its selections by option name, so two options with one\n // name cannot both be selected (AGL-3066).\n if (new Set(options.map((option) => option.name.trim())).size !== options.length) {\n return 'Each option needs its own name'\n }\n const modifierProblem = modifierGroupsProblem(product.modifierGroups)\n if (modifierProblem) return modifierProblem\n const variants = product.variants ?? []\n if (variants.length === 0) return 'Products need at least one variant'\n if (variants.length > COMMERCE_MAX_VARIANTS) {\n return `At most ${COMMERCE_MAX_VARIANTS} variants per product`\n }\n const ids = new Set<string>()\n const skus = new Set<string>()\n for (const variant of variants) {\n if (!variant.id) return 'Variants need stable ids'\n if (ids.has(variant.id)) return 'Variant ids must be unique'\n ids.add(variant.id)\n // A variant with no price is storable (AGL-3676): the owner may list a\n // product before pricing it, the storefront says \"Price coming soon\",\n // and no sale door sells it (`variantHasPrice`).\n if (variant.priceUsd === undefined || variant.priceUsd === null) {\n if (variant.sku) {\n if (skus.has(variant.sku)) return 'Variant SKUs must be unique'\n skus.add(variant.sku)\n }\n continue\n }\n const price = Number(variant.priceUsd)\n if (!Number.isFinite(price) || price < 0) {\n return 'Variant prices must be zero or more'\n }\n if (price > COMMERCE_MAX_PRICE_USD) {\n return `Prices are capped at $${COMMERCE_MAX_PRICE_USD}`\n }\n if (variant.sku) {\n if (skus.has(variant.sku)) return 'Variant SKUs must be unique'\n skus.add(variant.sku)\n }\n if (\n variant.compareAtPriceUsd != null &&\n Number(variant.compareAtPriceUsd) <= price\n ) {\n return 'Compare-at price must exceed the price'\n }\n }\n return null\n}\n\n/** Actionable error, or null when the collection is storable. */\nexport function validateCollection(collection: HostCollection): string | null {\n if (!collection.name?.trim()) return 'Collection name is required'\n if (\n !collection.slug ||\n collection.slug !== commerceSlug(collection.slug)\n ) {\n return 'Slug must be lowercase letters, numbers, and dashes'\n }\n if (collection.mode !== 'manual' && collection.mode !== 'smart') {\n return 'Unknown collection mode'\n }\n if (collection.mode === 'smart') {\n const rules = collection.rules ?? []\n if (rules.length === 0) return 'Smart collections need at least one rule'\n for (const rule of rules) {\n if (\n !['tag', 'categoryId', 'priceUsd', 'name', 'type'].includes(rule.field)\n ) {\n return 'Unknown rule field'\n }\n if (!['eq', 'neq', 'lt', 'gt', 'contains'].includes(rule.op)) {\n return 'Unknown rule operator'\n }\n if (rule.value === '' || rule.value == null) {\n return 'Rule values are required'\n }\n }\n }\n return null\n}\n"],"names":["modifierGroupsProblem","nameSearchFields","draftProductChannelFacts","COMMERCE_MAX_PRICE_USD","COMMERCE_DEFAULT_PRICE_USD","productParcelDimensions","product","facts","shipping","side","value","number","Number","isFinite","Math","min","lengthCm","widthCm","heightCm","undefined","draftProductShippingFacts","normalizeProductShippingFacts","typed","String","trim","toUpperCase","test","originCountry","raw","round","hsCode","replace","slice","Object","keys","length","normalizePostalAddress","address","field","text","country","registersWithinCap","registers","cap","createdMs","r","createdAt","toMillis","seconds","ranked","sort","a","b","$id","localeCompare","Set","max","map","COMMERCE_MAX_VARIANTS","COMMERCE_MAX_OPTIONS","COMMERCE_MAX_OPTION_VALUES","COMMERCE_SLUG_MAX_LENGTH","commerceSlug","name","normalize","toLowerCase","productSearchFields","flatten","read","variants","variant","filter","Boolean","skus","sku","barcodes","barcode","priceFromCents","productPriceFromCents","productPriceRange","productStockFields","inventory","productInventory","soldOut","oversellPolicy","expandVariantMatrix","options","usable","option","values","combos","next","combo","push","renameProductOptions","names","current","index","size","selections","axisOf","optionAxesOf","moved","changed","key","entries","axis","get","Map","taken","forEach","prototype","hasOwnProperty","call","has","set","add","findIndex","includes","variantHasPrice","price","priceUsd","productPriceMissing","some","productCopyPatch","patch","channel","description","Array","isArray","tags","tag","categoryIds","seoTitle","seoDescription","seo","title","optionNames","every","assign","unpricedProductDraft","proposal","takenSlugs","nowMs","base","slug","suffix","seen","id","toString","type","status","createdAtMs","updatedAtMs","defaultPricedProductDraft","draft","proposedProductPriceUsd","suggested","cents","findVariant","find","prices","total","canPurchase","variantId","quantity","item","stockShortfall","available","stockTrackingApplies","subscription","subscriptionOptional","resolveCheckoutBillingMode","requested","adjustVariantInventory","delta","locationId","inventoryByLocation","buckets","reduce","sum","count","appliedVariantInventoryDelta","before","transferVariantInventory","fromLocationId","toLocationId","isLowStock","threshold","lowStockThreshold","ruleMatches","rule","op","ids","numeric","needle","matches","smartCollectionMatches","collection","rules","matcher","matchAll","matchesCollection","productId","deletedAt","mode","productIds","PRODUCT_COLLECTION_IDS","productCollectionIds","smartCollections","liftLegacyProduct","validateProduct","modifierProblem","modifierGroups","compareAtPriceUsd","validateCollection"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,qBAAqB,QAAmC,yBAAqB;AACtF,SAASC,gBAAgB,QAAQ,qCAAoC;AACrE,SAASC,wBAAwB,QAAkC,uBAAmB;AAGtF;;;;;;;;CAQC,GAED;;;;;;;;;;;;;;;;;CAiBC,GACD,uCAAuC,GACvC,OAAO,MAAMC,yBAAyB,MAAK;AAE3C;;;;;;;;;;;;CAYC,GACD,OAAO,MAAMC,6BAA6B,GAAE;AAuE5C,0EAA0E,GAC1E,OAAO,SAASC,wBACdC,OAAyD;IAEzD,MAAMC,QAAQD,2BAAAA,QAASE,QAAQ;IAC/B,MAAMC,OAAO,CAACC;QACZ,MAAMC,SAASC,OAAOF;QACtB,OAAOE,OAAOC,QAAQ,CAACF,WAAWA,SAAS,IAAIG,KAAKC,GAAG,CAACJ,QAAQ,OAAO;IACzE;IACA,MAAMK,WAAWP,KAAKF,yBAAAA,MAAOS,QAAQ;IACrC,MAAMC,UAAUR,KAAKF,yBAAAA,MAAOU,OAAO;IACnC,MAAMC,WAAWT,KAAKF,yBAAAA,MAAOW,QAAQ;IACrC,OAAOF,YAAYC,WAAWC,WAAW;QAAEF;QAAUC;QAASC;IAAS,IAAIC;AAC7E;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,0BAA0BV,KAAc;;IACtD,MAAMH,QAAQc,8BAA8BX;IAC5C,MAAMY,QAAQC,eAAQb,yBAAD,AAACA,KAA0C,CAAC,gBAAgB,mBAAI,IAClFc,IAAI,GACJC,WAAW;IACd,IAAI,UAAUC,IAAI,CAACJ,QAAQ,OAAO,aAAKf;QAAOoB,eAAeL;;IAC7D,OAAO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,CAACyE,KAAKzJ,KAAK,GACtE;gBACA,OAAO;YACT;YACA,IAAI,CAAC;gBAAC;gBAAM;gBAAO;gBAAM;gBAAM;aAAW,CAACgF,QAAQ,CAACyE,KAAKC,EAAE,GAAG;gBAC5D,OAAO;YACT;YACA,IAAID,KAAKrL,KAAK,KAAK,MAAMqL,KAAKrL,KAAK,IAAI,MAAM;gBAC3C,OAAO;YACT;QACF;IACF;IACA,OAAO;AACT"}
@@ -60,3 +60,4 @@ export * from './commerce-payment-methods';
60
60
  export * from './product-channel';
61
61
  export * from './order-annotations';
62
62
  export * from './product-write';
63
+ export * from './product-reviews';
@@ -58,5 +58,6 @@ export * from "./commerce-payment-methods.js";
58
58
  export * from "./product-channel.js";
59
59
  export * from "./order-annotations.js";
60
60
  export * from "./product-write.js";
61
+ export * from "./product-reviews.js";
61
62
 
62
63
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/index.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Commerce domain model (AGL-411): pure types + logic relocated from the\n * core `@aglyn/aglyn` app-utils — the commerce plugin owns its domain.\n * Context-free and framework-free, so both the plugin's client components\n * and its `/server` handlers (and other plugins/apps via\n * `@aglyn/plugins-commerce/model`) can import it.\n */\nexport * from './commerce'\nexport * from './commerce-cart'\nexport * from './commerce-connect-transfer'\nexport * from './commerce-discounts'\nexport * from './commerce-dispute'\nexport * from './commerce-topics'\nexport * from './commerce-gift-cards'\nexport * from './commerce-orders'\nexport * from './commerce-printers'\nexport * from './commerce-pos'\nexport * from './commerce-pos-offline'\nexport * from './commerce-pos-kiosk'\nexport * from './product-modifiers'\nexport * from './commerce-promotions'\nexport * from './commerce-receipt'\nexport * from './commerce-reservations'\nexport * from './commerce-shipping'\nexport * from './commerce-stock-holds'\nexport * from './commerce-subscription-status'\nexport * from './commerce-tax'\nexport * from './commerce-tax-decision'\nexport * from './order-figures'\nexport * from './order-list-fields'\nexport * from './purchase-analytics'\nexport * from './tracking-url'\nexport * from './buyer-notifications'\nexport * from './order-fulfillment'\nexport * from './order-local-fulfillment'\nexport * from './local-fulfillment-settings'\nexport * from './order-events'\nexport * from './commerce-returns'\nexport * from './order-shipping-export'\nexport * from './order-status-view'\nexport * from './order-webhooks'\nexport * from './shipstation'\nexport * from './commerce-payment-methods'\nexport * from './product-channel'\nexport * from './order-annotations'\nexport * from './product-write'\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;CAMC,GACD,cAAc,gBAAY;AAC1B,cAAc,qBAAiB;AAC/B,cAAc,iCAA6B;AAC3C,cAAc,0BAAsB;AACpC,cAAc,wBAAoB;AAClC,cAAc,uBAAmB;AACjC,cAAc,2BAAuB;AACrC,cAAc,uBAAmB;AACjC,cAAc,yBAAqB;AACnC,cAAc,oBAAgB;AAC9B,cAAc,4BAAwB;AACtC,cAAc,0BAAsB;AACpC,cAAc,yBAAqB;AACnC,cAAc,2BAAuB;AACrC,cAAc,wBAAoB;AAClC,cAAc,6BAAyB;AACvC,cAAc,yBAAqB;AACnC,cAAc,4BAAwB;AACtC,cAAc,oCAAgC;AAC9C,cAAc,oBAAgB;AAC9B,cAAc,6BAAyB;AACvC,cAAc,qBAAiB;AAC/B,cAAc,yBAAqB;AACnC,cAAc,0BAAsB;AACpC,cAAc,oBAAgB;AAC9B,cAAc,2BAAuB;AACrC,cAAc,yBAAqB;AACnC,cAAc,+BAA2B;AACzC,cAAc,kCAA8B;AAC5C,cAAc,oBAAgB;AAC9B,cAAc,wBAAoB;AAClC,cAAc,6BAAyB;AACvC,cAAc,yBAAqB;AACnC,cAAc,sBAAkB;AAChC,cAAc,mBAAe;AAC7B,cAAc,gCAA4B;AAC1C,cAAc,uBAAmB;AACjC,cAAc,yBAAqB;AACnC,cAAc,qBAAiB"}
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/index.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Commerce domain model (AGL-411): pure types + logic relocated from the\n * core `@aglyn/aglyn` app-utils — the commerce plugin owns its domain.\n * Context-free and framework-free, so both the plugin's client components\n * and its `/server` handlers (and other plugins/apps via\n * `@aglyn/plugins-commerce/model`) can import it.\n */\nexport * from './commerce'\nexport * from './commerce-cart'\nexport * from './commerce-connect-transfer'\nexport * from './commerce-discounts'\nexport * from './commerce-dispute'\nexport * from './commerce-topics'\nexport * from './commerce-gift-cards'\nexport * from './commerce-orders'\nexport * from './commerce-printers'\nexport * from './commerce-pos'\nexport * from './commerce-pos-offline'\nexport * from './commerce-pos-kiosk'\nexport * from './product-modifiers'\nexport * from './commerce-promotions'\nexport * from './commerce-receipt'\nexport * from './commerce-reservations'\nexport * from './commerce-shipping'\nexport * from './commerce-stock-holds'\nexport * from './commerce-subscription-status'\nexport * from './commerce-tax'\nexport * from './commerce-tax-decision'\nexport * from './order-figures'\nexport * from './order-list-fields'\nexport * from './purchase-analytics'\nexport * from './tracking-url'\nexport * from './buyer-notifications'\nexport * from './order-fulfillment'\nexport * from './order-local-fulfillment'\nexport * from './local-fulfillment-settings'\nexport * from './order-events'\nexport * from './commerce-returns'\nexport * from './order-shipping-export'\nexport * from './order-status-view'\nexport * from './order-webhooks'\nexport * from './shipstation'\nexport * from './commerce-payment-methods'\nexport * from './product-channel'\nexport * from './order-annotations'\nexport * from './product-write'\nexport * from './product-reviews'\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;CAMC,GACD,cAAc,gBAAY;AAC1B,cAAc,qBAAiB;AAC/B,cAAc,iCAA6B;AAC3C,cAAc,0BAAsB;AACpC,cAAc,wBAAoB;AAClC,cAAc,uBAAmB;AACjC,cAAc,2BAAuB;AACrC,cAAc,uBAAmB;AACjC,cAAc,yBAAqB;AACnC,cAAc,oBAAgB;AAC9B,cAAc,4BAAwB;AACtC,cAAc,0BAAsB;AACpC,cAAc,yBAAqB;AACnC,cAAc,2BAAuB;AACrC,cAAc,wBAAoB;AAClC,cAAc,6BAAyB;AACvC,cAAc,yBAAqB;AACnC,cAAc,4BAAwB;AACtC,cAAc,oCAAgC;AAC9C,cAAc,oBAAgB;AAC9B,cAAc,6BAAyB;AACvC,cAAc,qBAAiB;AAC/B,cAAc,yBAAqB;AACnC,cAAc,0BAAsB;AACpC,cAAc,oBAAgB;AAC9B,cAAc,2BAAuB;AACrC,cAAc,yBAAqB;AACnC,cAAc,+BAA2B;AACzC,cAAc,kCAA8B;AAC5C,cAAc,oBAAgB;AAC9B,cAAc,wBAAoB;AAClC,cAAc,6BAAyB;AACvC,cAAc,yBAAqB;AACnC,cAAc,sBAAkB;AAChC,cAAc,mBAAe;AAC7B,cAAc,gCAA4B;AAC1C,cAAc,uBAAmB;AACjC,cAAc,yBAAqB;AACnC,cAAc,qBAAiB;AAC/B,cAAc,uBAAmB"}
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Aglyn LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import type { HostOrder } from './commerce-orders';
18
+ /**
19
+ * Storefront product reviews, verified buyers by default (AGL-3736).
20
+ *
21
+ * The industry standard (the Shopify review apps, Squarespace, Etsy): a review
22
+ * carries "Verified buyer" only when the platform KNOWS the reviewer bought
23
+ * the product — a signed-in customer whose orders hold it, or a one-time link
24
+ * from the review-request email. A typed email proves nothing (anybody can
25
+ * type anybody's address), so it no longer verifies anything.
26
+ *
27
+ * Pure: read by the storefront block, the console and the server.
28
+ */
29
+ /** Who may review a product on this store. */
30
+ export type ReviewPolicy = 'verified' | 'anyone';
31
+ /** Every status a review document can hold. */
32
+ export type ReviewStatus = 'pending' | 'approved' | 'hidden' | 'rejected';
33
+ export declare const REVIEW_STATUSES: readonly ReviewStatus[];
34
+ /**
35
+ * Why a merchant hid or rejected a review. Content reasons only: the FTC's
36
+ * 2024 rule on fake reviews bars suppressing an honest review for being
37
+ * negative, so "I don't like it" is not on the list.
38
+ */
39
+ export declare const REVIEW_MODERATION_REASONS: readonly ["spam", "abusive", "off_topic", "private_information"];
40
+ export type ReviewModerationReason = (typeof REVIEW_MODERATION_REASONS)[number];
41
+ export declare const REVIEW_MODERATION_REASON_LABELS: Readonly<Record<ReviewModerationReason, string>>;
42
+ export declare function isReviewModerationReason(value: unknown): value is ReviewModerationReason;
43
+ /** The review-request email's delay, in days after fulfilment. */
44
+ export declare const REVIEW_REQUEST_DELAY_DEFAULT_DAYS = 7;
45
+ export declare const REVIEW_REQUEST_DELAY_MIN_DAYS = 3;
46
+ export declare const REVIEW_REQUEST_DELAY_MAX_DAYS = 30;
47
+ /** How long a review link from the email works. */
48
+ export declare const REVIEW_TOKEN_TTL_MS: number;
49
+ /** A store's review settings, typed and bounded. */
50
+ export interface ReviewSettings {
51
+ policy: ReviewPolicy;
52
+ /** Whether the review-request email goes out. On unless switched off. */
53
+ requestEmail: boolean;
54
+ /** Days after fulfilment, 3–30. */
55
+ requestDelayDays: number;
56
+ }
57
+ /**
58
+ * Reads `hosts/{hostId}/settings/store.reviews`. Defensive in the safe
59
+ * direction: anything but an explicit `'anyone'` is `verified`, so every
60
+ * existing store (no map at all) is verified-only, and only an explicit
61
+ * `false` turns the email off — the transaction-email convention (AGL-3600).
62
+ */
63
+ export declare function reviewSettingsFrom(storeSettings: unknown): ReviewSettings;
64
+ /**
65
+ * When an order fulfilled at `fulfilledAtMs` is owed its review request.
66
+ * The scheduler stamps the EARLIEST possible moment (the 3-day minimum) and
67
+ * the sweep re-reads the store's current delay, so a setting changed after
68
+ * fulfilment still applies.
69
+ */
70
+ export declare function reviewRequestDueAtMs(fulfilledAtMs: number, delayDays: number): number;
71
+ export declare function reviewRequestEarliestDueAtMs(fulfilledAtMs: number): number;
72
+ /**
73
+ * Whether an order's buyer is asked for a review at all. A register sale is
74
+ * not (the address was given for a receipt at the counter), nor is an order
75
+ * sold on an outside marketplace (its reviews belong there).
76
+ */
77
+ export declare function orderTakesReviewRequest(order: Pick<HostOrder, 'channel' | 'status'>): boolean;
78
+ /**
79
+ * The products a review request lists: each distinct product on the order
80
+ * that the order still entitles — a line refunded by name is never asked
81
+ * about, and neither is a fully refunded or cancelled order.
82
+ */
83
+ export declare function reviewableProductIds(order: Partial<HostOrder>): string[];
84
+ /** Error codes the reviews route answers with, for the storefront to word. */
85
+ export declare const REVIEW_ERROR_CODES: {
86
+ readonly verifiedPurchaseRequired: "verified_purchase_required";
87
+ readonly tokenInvalid: "review_token_invalid";
88
+ readonly tokenUsed: "review_token_used";
89
+ readonly tokenExpired: "review_token_expired";
90
+ readonly rateLimited: "review_rate_limited";
91
+ readonly notEnabled: "reviews_not_enabled";
92
+ readonly memberSuspended: "member_suspended";
93
+ readonly alreadyReviewed: "already_reviewed";
94
+ };
95
+ export type ReviewErrorCode = (typeof REVIEW_ERROR_CODES)[keyof typeof REVIEW_ERROR_CODES];
96
+ /** What the storefront form is told before it draws itself. */
97
+ export interface ReviewEligibility {
98
+ /** The store's plan includes reviews. */
99
+ enabled?: boolean;
100
+ policy: ReviewPolicy;
101
+ /** A site member is signed in on this store. */
102
+ signedIn: boolean;
103
+ /** The review would carry "Verified buyer", and through what. */
104
+ verifiedVia: 'member' | 'token' | null;
105
+ /** The form may be submitted at all. */
106
+ canReview: boolean;
107
+ /** The form must ask for a name and an email (an unverified visitor). */
108
+ needsEmail: boolean;
109
+ /** Why a presented review link does not work, when it does not. */
110
+ tokenProblem?: ReviewErrorCode;
111
+ }
112
+ /**
113
+ * The eligibility answer from what the server established. Pure, so the
114
+ * storefront's three states are specified in one place.
115
+ */
116
+ export declare function reviewEligibility(input: {
117
+ policy: ReviewPolicy;
118
+ signedIn: boolean;
119
+ memberVerified: boolean;
120
+ tokenVerified: boolean;
121
+ tokenProblem?: ReviewErrorCode;
122
+ }): ReviewEligibility;
123
+ /** The status a new review is saved with. */
124
+ export declare function initialReviewStatus(verified: boolean): ReviewStatus;