@tokenoftrust/storefront-runner 6.11.8 → 6.11.10

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.
@@ -72,3 +72,196 @@ export interface ProductsViewModel {
72
72
  rows: ProductRow[];
73
73
  detailsById: Record<string, ProductDetail>;
74
74
  }
75
+
76
+ /**
77
+ * Closed per-SKU outcome returned by tot-foxycart's excise SKU registration route
78
+ * (`POST /api/commerce/excise/products`).
79
+ */
80
+ export const EXCISE_SKU_OUTCOMES = [
81
+ "registered",
82
+ "already-registered",
83
+ "found",
84
+ "not-found",
85
+ "deactivated",
86
+ "refused-invalid-sku",
87
+ "refused-registry",
88
+ "refused-owned-by-other-tenant",
89
+ ] as const;
90
+
91
+ export type ExciseSkuOutcome = (typeof EXCISE_SKU_OUTCOMES)[number];
92
+
93
+ /**
94
+ * The admin view states for a catalog SKU in the Excise tax section (`ac-1`).
95
+ */
96
+ export type ExciseSkuState = "registered" | "missing" | "refused" | "unavailable";
97
+
98
+ export const EXCISE_UNAVAILABLE_MESSAGE =
99
+ "Registry status is unavailable right now; reload to try again.";
100
+
101
+ export type ExciseProductType = "vape" | "nicotinePouch" | "hemp";
102
+
103
+ export type ExciseVapeSystemType =
104
+ | "open"
105
+ | "closed"
106
+ | "isLiquid"
107
+ | "isPart"
108
+ | "isOpen"
109
+ | "closedSingleUse"
110
+ | "closedCartridge"
111
+ | "batteryOnly"
112
+ | "pack";
113
+
114
+ export interface ExciseSkuAttributes {
115
+ type: ExciseProductType;
116
+ vapeSystemType: ExciseVapeSystemType;
117
+ volumeInMl: number;
118
+ wholesaleCost: number;
119
+ wholesalePrice: number;
120
+ msrp: number;
121
+ retailPrice: number;
122
+ hasNicotine: boolean;
123
+ packQuantity: number;
124
+ }
125
+
126
+ export interface ExciseCatalogSkuRow {
127
+ sku: string;
128
+ productHandle: string;
129
+ productTitle: string;
130
+ variantId: string;
131
+ variantTitle: string;
132
+ brand: string;
133
+ currency: string;
134
+ state: ExciseSkuState;
135
+ outcome: ExciseSkuOutcome | null;
136
+ message: string;
137
+ attributes: Partial<ExciseSkuAttributes>;
138
+ }
139
+
140
+ export interface ExciseRegistrySkuSnapshot {
141
+ outcome: ExciseSkuOutcome;
142
+ active?: boolean;
143
+ attributes?: Partial<ExciseSkuAttributes>;
144
+ }
145
+
146
+ const CLOSED_OUTCOME_MESSAGES: Readonly<Record<ExciseSkuOutcome, string>> = Object.freeze({
147
+ registered: "Registered in the excise tax registry.",
148
+ "already-registered": "Already registered in the excise tax registry.",
149
+ found: "Registered in the excise tax registry.",
150
+ "not-found": "Not registered in the excise tax registry yet.",
151
+ deactivated: "Deactivated in the excise tax registry (exempt).",
152
+ "refused-invalid-sku": "Registration refused: invalid SKU or engine attributes.",
153
+ "refused-registry": "Registration refused: the excise tax registry could not complete this request.",
154
+ "refused-owned-by-other-tenant": "Registration refused: this SKU code is owned by another store.",
155
+ });
156
+
157
+ export function isExciseSkuOutcome(value: unknown): value is ExciseSkuOutcome {
158
+ return typeof value === "string" && (EXCISE_SKU_OUTCOMES as readonly string[]).includes(value);
159
+ }
160
+
161
+ /**
162
+ * Map a closed registry outcome into the admin SKU state (`registered`, `missing`, or `refused`).
163
+ */
164
+ export function mapExciseOutcomeToState(
165
+ outcome: ExciseSkuOutcome | null | undefined,
166
+ active?: boolean,
167
+ ): ExciseSkuState {
168
+ if (!outcome || outcome === "not-found" || outcome === "deactivated") return "missing";
169
+ if (outcome === "found") return active === false ? "missing" : "registered";
170
+ if (outcome === "registered" || outcome === "already-registered") return "registered";
171
+ return "refused";
172
+ }
173
+
174
+ /**
175
+ * Return the closed, truthful message for a SKU's registration outcome (`ac-3`).
176
+ * Upstream response bodies are never interpolated here.
177
+ */
178
+ export function exciseOutcomeMessage(
179
+ outcome: ExciseSkuOutcome | null | undefined,
180
+ active?: boolean,
181
+ ): string {
182
+ if (!outcome) return CLOSED_OUTCOME_MESSAGES["not-found"];
183
+ if (outcome === "found" && active === false) return CLOSED_OUTCOME_MESSAGES.deactivated;
184
+ return CLOSED_OUTCOME_MESSAGES[outcome] ?? CLOSED_OUTCOME_MESSAGES["refused-registry"];
185
+ }
186
+
187
+ /**
188
+ * Validate a bare catalog SKU before any U4 call: 1..200 chars, trimmed, no whitespace,
189
+ * no colon (`:`), and no wildcard (`*`).
190
+ */
191
+ export function isValidBareCatalogSku(value: unknown): value is string {
192
+ if (typeof value !== "string") return false;
193
+ const trimmed = value.trim();
194
+ if (!trimmed || trimmed !== value || trimmed.length > 200) return false;
195
+ if (/[\s:*]/.test(trimmed)) return false;
196
+ return true;
197
+ }
198
+
199
+ export interface CatalogProductLike {
200
+ handle: string;
201
+ title: string;
202
+ vendor?: string;
203
+ product_type?: string;
204
+ metafields?: ReadonlyArray<{ namespace: string; key: string; value: string }>;
205
+ variants: ReadonlyArray<{
206
+ id: string;
207
+ title: string;
208
+ sku?: string;
209
+ price: number;
210
+ compare_at_price?: number;
211
+ currency: string;
212
+ }>;
213
+ }
214
+
215
+ /**
216
+ * Build the admin excise SKU rows from the store's catalog products and optional registry read-back.
217
+ * Only variants with a valid bare SKU in the store's catalog are included.
218
+ * Regulated engine attributes are never guessed from catalog prices; only registry-returned
219
+ * attributes are populated. When a registry read fails (`registryUnavailable: true`), rows with no
220
+ * snapshot are marked `"unavailable"` rather than `"missing"`.
221
+ */
222
+ export function toExciseCatalogSkuRows(
223
+ products: readonly CatalogProductLike[],
224
+ registryBySku?: ReadonlyMap<string, ExciseRegistrySkuSnapshot>,
225
+ options?: { registryUnavailable?: boolean },
226
+ ): ExciseCatalogSkuRow[] {
227
+ const rows: ExciseCatalogSkuRow[] = [];
228
+ const seen = new Set<string>();
229
+ const registryUnavailable = options?.registryUnavailable === true;
230
+ const sortedProducts = [...products].sort((a, b) => a.title.localeCompare(b.title));
231
+ for (const product of sortedProducts) {
232
+ for (const variant of product.variants) {
233
+ const sku = typeof variant.sku === "string" ? variant.sku.trim() : "";
234
+ if (!isValidBareCatalogSku(sku) || seen.has(sku)) continue;
235
+ seen.add(sku);
236
+ const snapshot = registryBySku?.get(sku);
237
+ const outcome = snapshot?.outcome ?? null;
238
+ const state: ExciseSkuState = snapshot
239
+ ? mapExciseOutcomeToState(outcome, snapshot.active)
240
+ : registryUnavailable
241
+ ? "unavailable"
242
+ : "missing";
243
+ const message = snapshot
244
+ ? exciseOutcomeMessage(outcome, snapshot.active)
245
+ : registryUnavailable
246
+ ? EXCISE_UNAVAILABLE_MESSAGE
247
+ : exciseOutcomeMessage(null);
248
+ const attributes: Partial<ExciseSkuAttributes> = snapshot?.attributes
249
+ ? { ...snapshot.attributes }
250
+ : {};
251
+ rows.push({
252
+ sku,
253
+ productHandle: product.handle,
254
+ productTitle: product.title,
255
+ variantId: variant.id,
256
+ variantTitle: variant.title,
257
+ brand: (product.vendor ?? "").trim() || product.title,
258
+ currency: variant.currency || "USD",
259
+ state,
260
+ outcome,
261
+ message,
262
+ attributes,
263
+ });
264
+ }
265
+ }
266
+ return rows;
267
+ }
@@ -37,3 +37,116 @@ export function mountCheckoutExciseForm(form: HTMLElement): void {
37
37
  }
38
38
  });
39
39
  }
40
+
41
+ const SKU_CHIP_CLASSES: Record<"registered" | "missing" | "refused" | "unavailable", string> = {
42
+ registered: "green",
43
+ missing: "amber",
44
+ refused: "red",
45
+ unavailable: "amber",
46
+ };
47
+
48
+ const SKU_CHIP_LABELS: Record<"registered" | "missing" | "refused" | "unavailable", string> = {
49
+ registered: "Registered",
50
+ missing: "Missing",
51
+ refused: "Refused",
52
+ unavailable: "Unavailable",
53
+ };
54
+
55
+ function readRowProduct(row: Element, sku: string, action: string): Record<string, unknown> {
56
+ if (action !== "add") return { sku };
57
+ const getVal = (name: string): string | boolean | undefined => {
58
+ const el = row.querySelector<HTMLInputElement | HTMLSelectElement>(`[data-excise-attr="${name}"]`);
59
+ if (!el) return undefined;
60
+ if (el instanceof HTMLInputElement && el.type === "checkbox") return el.checked;
61
+ return el.value;
62
+ };
63
+ return {
64
+ sku,
65
+ type: getVal("type"),
66
+ vapeSystemType: getVal("vapeSystemType"),
67
+ volumeInMl: getVal("volumeInMl"),
68
+ wholesaleCost: getVal("wholesaleCost"),
69
+ wholesalePrice: getVal("wholesalePrice"),
70
+ msrp: getVal("msrp"),
71
+ retailPrice: getVal("retailPrice"),
72
+ packQuantity: getVal("packQuantity"),
73
+ hasNicotine: Boolean(getVal("hasNicotine")),
74
+ };
75
+ }
76
+
77
+ function applyRowResult(
78
+ row: Element,
79
+ item: { state?: string; message?: string } | null | undefined,
80
+ ): void {
81
+ const badge = row.querySelector<HTMLElement>("[data-excise-sku-state]");
82
+ const msg = row.querySelector<HTMLElement>("[data-excise-sku-message]");
83
+ const state: "registered" | "missing" | "refused" | "unavailable" =
84
+ item &&
85
+ (item.state === "registered" ||
86
+ item.state === "missing" ||
87
+ item.state === "refused" ||
88
+ item.state === "unavailable")
89
+ ? item.state
90
+ : "refused";
91
+ if (badge) {
92
+ badge.setAttribute("data-excise-sku-state", state);
93
+ badge.className = `chip ${SKU_CHIP_CLASSES[state]}`;
94
+ badge.textContent = SKU_CHIP_LABELS[state];
95
+ }
96
+ if (msg && item && typeof item.message === "string") {
97
+ msg.textContent = item.message;
98
+ }
99
+ }
100
+
101
+ export function mountExciseProductsPanel(panel: HTMLElement): void {
102
+ const endpoint = panel.getAttribute("data-endpoint") || "/api/admin/excise-products";
103
+ const statusEl = panel.querySelector<HTMLElement>("[data-excise-products-status]");
104
+
105
+ panel.addEventListener("click", async (event) => {
106
+ const target = event.target instanceof Element ? event.target.closest("[data-excise-sku-action]") : null;
107
+ if (!target) return;
108
+ const action = target.getAttribute("data-excise-sku-action");
109
+ const sku = target.getAttribute("data-excise-sku");
110
+ const row = target.closest("[data-excise-sku-row]");
111
+ if (!action || !sku || !row) return;
112
+
113
+ const buttons = row.querySelectorAll<HTMLButtonElement>("button[data-excise-sku-action]");
114
+ buttons.forEach((b) => {
115
+ b.disabled = true;
116
+ });
117
+ if (statusEl) {
118
+ statusEl.textContent = action === "add" ? `Registering ${sku}...` : `Deactivating ${sku}...`;
119
+ }
120
+
121
+ try {
122
+ const product = readRowProduct(row, sku, action);
123
+ const res = await fetch(endpoint, {
124
+ method: "POST",
125
+ headers: { "content-type": "application/json" },
126
+ body: JSON.stringify({ action, products: [product] }),
127
+ });
128
+ const data = (await res.json()) as {
129
+ message?: string;
130
+ results?: Array<{ sku?: string; state?: string; message?: string }>;
131
+ };
132
+ const entry = Array.isArray(data?.results)
133
+ ? data.results.find((r) => r && r.sku === sku)
134
+ : null;
135
+ if (entry) {
136
+ applyRowResult(row, entry);
137
+ }
138
+ if (statusEl) {
139
+ statusEl.textContent =
140
+ entry?.message || data?.message || (res.ok ? "Updated." : "Request refused.");
141
+ }
142
+ } catch {
143
+ if (statusEl) {
144
+ statusEl.textContent = "Could not reach the excise registration service.";
145
+ }
146
+ } finally {
147
+ buttons.forEach((b) => {
148
+ b.disabled = false;
149
+ });
150
+ }
151
+ });
152
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "6.11.8",
3
+ "version": "6.11.10",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",