@tokenoftrust/storefront-runner 7.6.17 → 7.6.18
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.
- package/apps/storefront/src/components/content/storefront/content/BulkQuoteForm.astro +396 -0
- package/apps/storefront/src/components/content/storefront/content/ProseSections.astro +3 -0
- package/apps/storefront/src/lib/content/prose.ts +36 -0
- package/apps/storefront/src/lib/engagement/contact/bulkQuote.ts +521 -0
- package/apps/storefront/src/lib/engagement/contact/submitToken.ts +29 -13
- package/apps/storefront/src/pages/api/contact/token.ts +11 -5
- package/package.json +1 -1
- package/packages/cli/src/visual-parity.mjs +3 -1
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* <BulkQuoteForm> — the `bulk_quote_form` page section: a wholesale quote request with
|
|
4
|
+
* a repeatable line-item list. Submissions go to the platform-owned JSON endpoint
|
|
5
|
+
* (contract: lib/engagement/contact/bulkQuote.ts), so the section carries copy and an
|
|
6
|
+
* optional unit subset only: no recipient, endpoint or script prop.
|
|
7
|
+
*
|
|
8
|
+
* The endpoint is JSON-only, so the form is revealed by script and visitors without JS
|
|
9
|
+
* see an explanatory note. Markup is static and escaped; the script builds line items by
|
|
10
|
+
* cloning the <template> row and writes every message with textContent.
|
|
11
|
+
*
|
|
12
|
+
* The endpoint is api-base-scoped (`apiBasePath`, never `basePath`, which a
|
|
13
|
+
* preview/candidate pin overrides with a render prefix that has no `/api` mirror). The
|
|
14
|
+
* markup is identical for every serving provider.
|
|
15
|
+
*/
|
|
16
|
+
import type { ProseBulkQuoteFormSection } from "@/lib/content/prose";
|
|
17
|
+
import { withBase } from "@/lib/publication/shared/basePath";
|
|
18
|
+
import { BULK_QUOTE_LIMITS, BULK_QUOTE_UNITS } from "@/lib/engagement/contact/bulkQuote";
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
section: ProseBulkQuoteFormSection;
|
|
22
|
+
/** Distinguishes ids when several forms share a page. */
|
|
23
|
+
index?: number;
|
|
24
|
+
}
|
|
25
|
+
const { section, index = 0 } = Astro.props;
|
|
26
|
+
const { apiBasePath } = Astro.locals;
|
|
27
|
+
const endpoint = withBase(apiBasePath ?? "", "/api/contact/bulk-quote");
|
|
28
|
+
const id = `bulk-quote-form-${index}`;
|
|
29
|
+
const L = BULK_QUOTE_LIMITS;
|
|
30
|
+
const declared: readonly string[] = Array.isArray(section.units) ? section.units : [];
|
|
31
|
+
const picked = BULK_QUOTE_UNITS.filter((u) => declared.includes(u));
|
|
32
|
+
const units = picked.length ? picked : BULK_QUOTE_UNITS;
|
|
33
|
+
const submitLabel = section.submitLabel ?? "Request a quote";
|
|
34
|
+
const successMessage = section.successMessage ?? "Thanks. We received your quote request.";
|
|
35
|
+
---
|
|
36
|
+
<section
|
|
37
|
+
class="bulk-quote"
|
|
38
|
+
data-bulk-quote-form
|
|
39
|
+
data-endpoint={endpoint}
|
|
40
|
+
data-success={successMessage}
|
|
41
|
+
data-max-items={L.lineItems}
|
|
42
|
+
aria-labelledby={section.heading ? `${id}-heading` : undefined}
|
|
43
|
+
>
|
|
44
|
+
{section.heading && <h2 id={`${id}-heading`} class="bulk-quote__heading">{section.heading}</h2>}
|
|
45
|
+
{section.intro && <p class="bulk-quote__intro">{section.intro}</p>}
|
|
46
|
+
<noscript>
|
|
47
|
+
<p class="bulk-quote__note">This quote form needs JavaScript. Enable it and reload, or use the contact details on this page.</p>
|
|
48
|
+
</noscript>
|
|
49
|
+
<form hidden novalidate data-bulk-quote-el>
|
|
50
|
+
<fieldset class="bulk-quote__group">
|
|
51
|
+
<legend>Your details</legend>
|
|
52
|
+
<div class="bulk-quote__field">
|
|
53
|
+
<label for={`${id}-businessName`}>Business name</label>
|
|
54
|
+
<input id={`${id}-businessName`} name="businessName" type="text" autocomplete="organization" required maxlength={L.businessName} />
|
|
55
|
+
</div>
|
|
56
|
+
<div class="bulk-quote__field">
|
|
57
|
+
<label for={`${id}-name`}>Your name</label>
|
|
58
|
+
<input id={`${id}-name`} name="name" type="text" autocomplete="name" required maxlength={L.name} />
|
|
59
|
+
</div>
|
|
60
|
+
<div class="bulk-quote__field">
|
|
61
|
+
<label for={`${id}-email`}>Email</label>
|
|
62
|
+
<input id={`${id}-email`} name="email" type="email" inputmode="email" autocomplete="email" required maxlength={L.email} />
|
|
63
|
+
</div>
|
|
64
|
+
<div class="bulk-quote__field">
|
|
65
|
+
<label for={`${id}-phone`}>Phone</label>
|
|
66
|
+
<input id={`${id}-phone`} name="phone" type="tel" inputmode="tel" autocomplete="tel" maxlength={L.phone} />
|
|
67
|
+
</div>
|
|
68
|
+
</fieldset>
|
|
69
|
+
|
|
70
|
+
<fieldset class="bulk-quote__group">
|
|
71
|
+
<legend>Items</legend>
|
|
72
|
+
<ol class="bulk-quote__items" data-bulk-quote-items></ol>
|
|
73
|
+
<button type="button" class="bulk-quote__add" data-bulk-quote-add>Add item</button>
|
|
74
|
+
</fieldset>
|
|
75
|
+
|
|
76
|
+
<fieldset class="bulk-quote__group">
|
|
77
|
+
<legend>Delivery</legend>
|
|
78
|
+
<div class="bulk-quote__field">
|
|
79
|
+
<label for={`${id}-destinationRegion`}>Destination state or region</label>
|
|
80
|
+
<input id={`${id}-destinationRegion`} name="destinationRegion" type="text" autocomplete="address-level1" maxlength={L.destinationRegion} />
|
|
81
|
+
</div>
|
|
82
|
+
<div class="bulk-quote__field">
|
|
83
|
+
<label for={`${id}-destinationPostalCode`}>Destination postal code</label>
|
|
84
|
+
<input id={`${id}-destinationPostalCode`} name="destinationPostalCode" type="text" autocomplete="postal-code" maxlength={L.destinationPostalCode} />
|
|
85
|
+
</div>
|
|
86
|
+
<div class="bulk-quote__field bulk-quote__field--check">
|
|
87
|
+
<input id={`${id}-liftgate`} name="liftgate" type="checkbox" />
|
|
88
|
+
<label for={`${id}-liftgate`}>Liftgate delivery needed</label>
|
|
89
|
+
</div>
|
|
90
|
+
<div class="bulk-quote__field">
|
|
91
|
+
<label for={`${id}-freightNotes`}>Freight notes</label>
|
|
92
|
+
<textarea id={`${id}-freightNotes`} name="freightNotes" rows="3" maxlength={L.freightNotes}></textarea>
|
|
93
|
+
</div>
|
|
94
|
+
</fieldset>
|
|
95
|
+
|
|
96
|
+
<fieldset class="bulk-quote__group">
|
|
97
|
+
<legend>Order details</legend>
|
|
98
|
+
<div class="bulk-quote__field">
|
|
99
|
+
<label for={`${id}-poNumber`}>PO number</label>
|
|
100
|
+
<input id={`${id}-poNumber`} name="poNumber" type="text" maxlength={L.poNumber} />
|
|
101
|
+
</div>
|
|
102
|
+
<div class="bulk-quote__field">
|
|
103
|
+
<label for={`${id}-timeline`}>Timeline</label>
|
|
104
|
+
<input id={`${id}-timeline`} name="timeline" type="text" maxlength={L.timeline} />
|
|
105
|
+
</div>
|
|
106
|
+
<div class="bulk-quote__field">
|
|
107
|
+
<label for={`${id}-notes`}>Notes</label>
|
|
108
|
+
<textarea id={`${id}-notes`} name="notes" rows="5" maxlength={L.notes}></textarea>
|
|
109
|
+
</div>
|
|
110
|
+
</fieldset>
|
|
111
|
+
|
|
112
|
+
<div class="bulk-quote__trap" aria-hidden="true">
|
|
113
|
+
<input name="company" type="text" tabindex="-1" autocomplete="off" />
|
|
114
|
+
</div>
|
|
115
|
+
<button type="submit" class="bulk-quote__submit">{submitLabel}</button>
|
|
116
|
+
</form>
|
|
117
|
+
<p class="bulk-quote__status" role="status" aria-live="polite" data-bulk-quote-status></p>
|
|
118
|
+
<p class="bulk-quote__ref" data-bulk-quote-ref hidden></p>
|
|
119
|
+
|
|
120
|
+
<template data-bulk-quote-row>
|
|
121
|
+
<li class="bulk-quote__item" data-bulk-quote-item>
|
|
122
|
+
<div class="bulk-quote__field">
|
|
123
|
+
<label>Description <textarea data-li="description" rows="2" required maxlength={L.description}></textarea></label>
|
|
124
|
+
</div>
|
|
125
|
+
<div class="bulk-quote__field">
|
|
126
|
+
<label>SKU <input data-li="sku" type="text" maxlength={L.sku} /></label>
|
|
127
|
+
</div>
|
|
128
|
+
<div class="bulk-quote__field">
|
|
129
|
+
<label>Quantity <input data-li="quantity" type="number" inputmode="decimal" min="0" max={L.quantityMax} step="any" required /></label>
|
|
130
|
+
</div>
|
|
131
|
+
<div class="bulk-quote__field">
|
|
132
|
+
<label>
|
|
133
|
+
Unit
|
|
134
|
+
<select data-li="unit">
|
|
135
|
+
{units.map((u) => <option value={u}>{u}</option>)}
|
|
136
|
+
</select>
|
|
137
|
+
</label>
|
|
138
|
+
</div>
|
|
139
|
+
<div class="bulk-quote__field">
|
|
140
|
+
<label>Pack size <input data-li="packSize" type="text" maxlength={L.packSize} /></label>
|
|
141
|
+
</div>
|
|
142
|
+
<button type="button" class="bulk-quote__remove" data-bulk-quote-remove>Remove item</button>
|
|
143
|
+
</li>
|
|
144
|
+
</template>
|
|
145
|
+
</section>
|
|
146
|
+
|
|
147
|
+
<style>
|
|
148
|
+
.bulk-quote { margin-top: 1.5rem; }
|
|
149
|
+
.bulk-quote__heading { font-family: var(--font-display, inherit); font-size: var(--heading-h2, 1.5rem); font-weight: 600; }
|
|
150
|
+
.bulk-quote__group { margin-top: 1.5rem; padding: 0; border: 0; }
|
|
151
|
+
.bulk-quote__group legend { font-weight: 700; padding: 0; }
|
|
152
|
+
.bulk-quote__items { list-style: none; margin: 0; padding: 0; display: grid; gap: 1rem; }
|
|
153
|
+
.bulk-quote__item { padding: 1rem; border: 1px solid var(--color-border); border-radius: var(--radius-md, 0.375rem); }
|
|
154
|
+
.bulk-quote__field { display: flex; flex-direction: column; gap: 0.35rem; margin-top: 1rem; }
|
|
155
|
+
.bulk-quote__field label { display: flex; flex-direction: column; gap: 0.35rem; font-weight: 600; }
|
|
156
|
+
.bulk-quote__field--check { flex-direction: row; align-items: center; }
|
|
157
|
+
.bulk-quote__field input:not([type="checkbox"]),
|
|
158
|
+
.bulk-quote__field textarea,
|
|
159
|
+
.bulk-quote__field select {
|
|
160
|
+
width: 100%;
|
|
161
|
+
padding: 0.65rem 0.8rem;
|
|
162
|
+
border: 1px solid var(--color-border);
|
|
163
|
+
border-radius: var(--radius-md, 0.375rem);
|
|
164
|
+
background: var(--color-surface);
|
|
165
|
+
color: var(--color-text);
|
|
166
|
+
font: inherit;
|
|
167
|
+
font-weight: 400;
|
|
168
|
+
}
|
|
169
|
+
.bulk-quote [aria-invalid="true"] { border-color: var(--color-danger, currentColor); }
|
|
170
|
+
.bulk-quote input:focus-visible,
|
|
171
|
+
.bulk-quote textarea:focus-visible,
|
|
172
|
+
.bulk-quote select:focus-visible,
|
|
173
|
+
.bulk-quote button:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
|
|
174
|
+
.bulk-quote__trap { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
|
|
175
|
+
.bulk-quote__add,
|
|
176
|
+
.bulk-quote__remove {
|
|
177
|
+
margin-top: 1rem;
|
|
178
|
+
min-height: var(--tap-target, 44px);
|
|
179
|
+
padding: 0.5rem 1.1rem;
|
|
180
|
+
border: 1px solid var(--color-border);
|
|
181
|
+
border-radius: var(--radius-md, 0.375rem);
|
|
182
|
+
background: var(--color-surface);
|
|
183
|
+
color: var(--color-text);
|
|
184
|
+
font: inherit;
|
|
185
|
+
cursor: pointer;
|
|
186
|
+
}
|
|
187
|
+
.bulk-quote__add[disabled] { opacity: 0.6; cursor: not-allowed; }
|
|
188
|
+
.bulk-quote__submit {
|
|
189
|
+
margin-top: 1.5rem;
|
|
190
|
+
min-height: var(--tap-target, 44px);
|
|
191
|
+
padding: 0.65rem 1.4rem;
|
|
192
|
+
border: 0;
|
|
193
|
+
border-radius: var(--radius-md, 0.375rem);
|
|
194
|
+
background: var(--color-accent);
|
|
195
|
+
color: var(--color-accent-contrast, var(--color-primary-contrast));
|
|
196
|
+
font: inherit;
|
|
197
|
+
font-weight: 700;
|
|
198
|
+
cursor: pointer;
|
|
199
|
+
}
|
|
200
|
+
.bulk-quote__submit[disabled] { opacity: 0.6; cursor: progress; }
|
|
201
|
+
.bulk-quote__status { margin-top: 1rem; min-height: 1.5em; color: var(--color-text); }
|
|
202
|
+
.bulk-quote__status[data-state="error"] { color: var(--color-danger, var(--color-text)); font-weight: 600; }
|
|
203
|
+
.bulk-quote__ref { font-weight: 600; }
|
|
204
|
+
.bulk-quote__note { color: var(--color-muted); }
|
|
205
|
+
</style>
|
|
206
|
+
|
|
207
|
+
<script>
|
|
208
|
+
import {
|
|
209
|
+
CONTACT_TOKEN_MAX_AGE_MS,
|
|
210
|
+
CONTACT_TOKEN_MIN_AGE_MS,
|
|
211
|
+
} from "@/lib/engagement/contact/contract";
|
|
212
|
+
import {
|
|
213
|
+
fetchBulkQuoteToken,
|
|
214
|
+
submitBulkQuote,
|
|
215
|
+
type BulkQuoteErrorCode,
|
|
216
|
+
type BulkQuoteSubmission,
|
|
217
|
+
type BulkQuoteUnit,
|
|
218
|
+
} from "@/lib/engagement/contact/bulkQuote";
|
|
219
|
+
|
|
220
|
+
const MESSAGES: Partial<Record<BulkQuoteErrorCode, string>> = {
|
|
221
|
+
rate_limited: "You have sent a few requests already. Wait a minute and try again.",
|
|
222
|
+
invalid_input: "Please check the highlighted field and try again.",
|
|
223
|
+
unavailable: "Quote requests can't be received right now. Your entries are still here; try again shortly.",
|
|
224
|
+
store_failed: "We couldn't save your request. Your entries are still here; try again.",
|
|
225
|
+
too_large: "That request is too large. Shorten it and try again.",
|
|
226
|
+
};
|
|
227
|
+
const GENERIC = "Something went wrong. Your entries are still here; try again.";
|
|
228
|
+
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
229
|
+
type Control = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
230
|
+
|
|
231
|
+
function setup(root: HTMLElement) {
|
|
232
|
+
const form = root.querySelector<HTMLFormElement>("[data-bulk-quote-el]");
|
|
233
|
+
const status = root.querySelector<HTMLElement>("[data-bulk-quote-status]");
|
|
234
|
+
const ref = root.querySelector<HTMLElement>("[data-bulk-quote-ref]");
|
|
235
|
+
const list = root.querySelector<HTMLElement>("[data-bulk-quote-items]");
|
|
236
|
+
const add = root.querySelector<HTMLButtonElement>("[data-bulk-quote-add]");
|
|
237
|
+
const row = root.querySelector<HTMLTemplateElement>("[data-bulk-quote-row]");
|
|
238
|
+
const endpoint = root.dataset.endpoint;
|
|
239
|
+
if (!form || !status || !ref || !list || !add || !row || !endpoint) return;
|
|
240
|
+
const maxItems = Number(root.dataset.maxItems) || 25;
|
|
241
|
+
// The wire contract appends /api/contact/... itself; hand it the api base.
|
|
242
|
+
const apiBase = endpoint.replace(/\/api\/contact\/bulk-quote$/, "");
|
|
243
|
+
const say = (text: string, state: "ok" | "error" | "") => {
|
|
244
|
+
status.textContent = text;
|
|
245
|
+
status.dataset.state = state;
|
|
246
|
+
};
|
|
247
|
+
const items = () => Array.from(list.querySelectorAll<HTMLElement>("[data-bulk-quote-item]"));
|
|
248
|
+
const control = (item: HTMLElement, key: string) =>
|
|
249
|
+
item.querySelector<Control>(`[data-li="${key}"]`);
|
|
250
|
+
const sync = () => {
|
|
251
|
+
const all = items();
|
|
252
|
+
add.disabled = all.length >= maxItems;
|
|
253
|
+
all.forEach((item) => {
|
|
254
|
+
const remove = item.querySelector<HTMLButtonElement>("[data-bulk-quote-remove]");
|
|
255
|
+
if (remove) remove.hidden = all.length <= 1;
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
const addItem = () => {
|
|
259
|
+
if (items().length >= maxItems) return;
|
|
260
|
+
list.appendChild(row.content.cloneNode(true));
|
|
261
|
+
sync();
|
|
262
|
+
};
|
|
263
|
+
addItem();
|
|
264
|
+
add.addEventListener("click", () => {
|
|
265
|
+
addItem();
|
|
266
|
+
const last = items().at(-1);
|
|
267
|
+
if (last) control(last, "description")?.focus();
|
|
268
|
+
});
|
|
269
|
+
list.addEventListener("click", (e) => {
|
|
270
|
+
const target = e.target as Element | null;
|
|
271
|
+
const remove = target?.closest("[data-bulk-quote-remove]");
|
|
272
|
+
if (!remove || items().length <= 1) return;
|
|
273
|
+
remove.closest("[data-bulk-quote-item]")?.remove();
|
|
274
|
+
sync();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
let token: { value: string; at: number } | null = null;
|
|
278
|
+
const loadToken = async () => {
|
|
279
|
+
token = { value: await fetchBulkQuoteToken(apiBase), at: Date.now() };
|
|
280
|
+
};
|
|
281
|
+
const ready = loadToken();
|
|
282
|
+
form.hidden = false;
|
|
283
|
+
ready.catch(() => say("Quote requests can't be received right now. Try again shortly.", "error"));
|
|
284
|
+
|
|
285
|
+
const top = (name: string) => form.elements.namedItem(name) as Control | null;
|
|
286
|
+
const value = (name: string) => top(name)?.value.trim() ?? "";
|
|
287
|
+
const mark = (el: Element | null | undefined) => {
|
|
288
|
+
el?.setAttribute("aria-invalid", "true");
|
|
289
|
+
(el as HTMLElement | null | undefined)?.focus();
|
|
290
|
+
};
|
|
291
|
+
const flag = (field: string, index?: number) => {
|
|
292
|
+
const all = items();
|
|
293
|
+
if (field === "lineItems") return mark(add);
|
|
294
|
+
if (index !== undefined && all[index]) {
|
|
295
|
+
const target = control(all[index], field);
|
|
296
|
+
if (target) return mark(target);
|
|
297
|
+
return mark(all[index]);
|
|
298
|
+
}
|
|
299
|
+
mark(top(field));
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
form.addEventListener("submit", async (e) => {
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
const button = form.querySelector<HTMLButtonElement>("button[type=submit]");
|
|
305
|
+
root.querySelectorAll("[aria-invalid]").forEach((el) => el.removeAttribute("aria-invalid"));
|
|
306
|
+
ref.hidden = true;
|
|
307
|
+
ref.textContent = "";
|
|
308
|
+
|
|
309
|
+
for (const field of ["businessName", "name"]) {
|
|
310
|
+
if (!value(field)) {
|
|
311
|
+
flag(field);
|
|
312
|
+
return say("Fill in the highlighted field.", "error");
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (!/^\S+@\S+$/.test(value("email"))) {
|
|
316
|
+
flag("email");
|
|
317
|
+
return say("Enter a valid email address.", "error");
|
|
318
|
+
}
|
|
319
|
+
const lineItems: BulkQuoteSubmission["lineItems"] = [];
|
|
320
|
+
const rows = items();
|
|
321
|
+
for (const [i, item] of rows.entries()) {
|
|
322
|
+
const read = (key: string) => control(item, key)?.value.trim() ?? "";
|
|
323
|
+
const description = read("description");
|
|
324
|
+
const quantity = Number(read("quantity"));
|
|
325
|
+
if (!description) {
|
|
326
|
+
flag("description", i);
|
|
327
|
+
return say("Describe each item.", "error");
|
|
328
|
+
}
|
|
329
|
+
if (!(quantity > 0)) {
|
|
330
|
+
flag("quantity", i);
|
|
331
|
+
return say("Enter a quantity greater than zero for each item.", "error");
|
|
332
|
+
}
|
|
333
|
+
const sku = read("sku");
|
|
334
|
+
const packSize = read("packSize");
|
|
335
|
+
lineItems.push({
|
|
336
|
+
description,
|
|
337
|
+
...(sku ? { sku } : {}),
|
|
338
|
+
quantity,
|
|
339
|
+
unit: read("unit") as BulkQuoteUnit,
|
|
340
|
+
...(packSize ? { packSize } : {}),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (button) button.disabled = true;
|
|
345
|
+
say("Sending...", "");
|
|
346
|
+
try {
|
|
347
|
+
if (!token || Date.now() - token.at > CONTACT_TOKEN_MAX_AGE_MS - 60_000) await loadToken();
|
|
348
|
+
const age = Date.now() - token!.at;
|
|
349
|
+
if (age < CONTACT_TOKEN_MIN_AGE_MS) await wait(CONTACT_TOKEN_MIN_AGE_MS - age + 100);
|
|
350
|
+
const optional = (name: string) => {
|
|
351
|
+
const v = value(name);
|
|
352
|
+
return v ? { [name]: v } : {};
|
|
353
|
+
};
|
|
354
|
+
const result = await submitBulkQuote(apiBase, {
|
|
355
|
+
token: token!.value,
|
|
356
|
+
businessName: value("businessName"),
|
|
357
|
+
name: value("name"),
|
|
358
|
+
email: value("email"),
|
|
359
|
+
...optional("phone"),
|
|
360
|
+
lineItems,
|
|
361
|
+
...optional("destinationRegion"),
|
|
362
|
+
...optional("destinationPostalCode"),
|
|
363
|
+
liftgate: (top("liftgate") as HTMLInputElement | null)?.checked === true,
|
|
364
|
+
...optional("freightNotes"),
|
|
365
|
+
...optional("poNumber"),
|
|
366
|
+
...optional("timeline"),
|
|
367
|
+
...optional("notes"),
|
|
368
|
+
company: top("company")?.value ?? "",
|
|
369
|
+
});
|
|
370
|
+
if (result.body.ok) {
|
|
371
|
+
const quoteId = result.body.quoteId;
|
|
372
|
+
form.reset();
|
|
373
|
+
list.replaceChildren();
|
|
374
|
+
addItem();
|
|
375
|
+
token = null;
|
|
376
|
+
say(root.dataset.success ?? "Thanks. We received your quote request.", "ok");
|
|
377
|
+
ref.textContent = `Quote reference: ${quoteId}`;
|
|
378
|
+
ref.hidden = false;
|
|
379
|
+
loadToken().catch(() => {});
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const code = result.body.error;
|
|
383
|
+
if (code === "invalid_token" || code === "forbidden_origin") token = null;
|
|
384
|
+
if (result.body.field) flag(result.body.field, result.body.index);
|
|
385
|
+
say(MESSAGES[code] ?? GENERIC, "error");
|
|
386
|
+
if (!token) loadToken().catch(() => {});
|
|
387
|
+
} catch {
|
|
388
|
+
say(GENERIC, "error");
|
|
389
|
+
} finally {
|
|
390
|
+
if (button) button.disabled = false;
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
document.querySelectorAll<HTMLElement>("[data-bulk-quote-form]").forEach(setup);
|
|
396
|
+
</script>
|
|
@@ -17,6 +17,7 @@ import { normalizeProse, withHeadingIds } from "@/lib/content/prose";
|
|
|
17
17
|
import RichText from "@/components/content/storefront/content/RichText.astro";
|
|
18
18
|
import Callout from "@/components/content/storefront/content/Callout.astro";
|
|
19
19
|
import ContactForm from "@/components/content/storefront/content/ContactForm.astro";
|
|
20
|
+
import BulkQuoteForm from "@/components/content/storefront/content/BulkQuoteForm.astro";
|
|
20
21
|
import NeedsReviewCallout from "@/components/content/storefront/content/NeedsReviewCallout.astro";
|
|
21
22
|
|
|
22
23
|
interface Props {
|
|
@@ -112,6 +113,8 @@ const rendered = withHeadingIds(normalizeProse(sections));
|
|
|
112
113
|
);
|
|
113
114
|
case "contact_form":
|
|
114
115
|
return <ContactForm section={s} index={i} />;
|
|
116
|
+
case "bulk_quote_form":
|
|
117
|
+
return <BulkQuoteForm section={s} index={i} />;
|
|
115
118
|
case "needs_review":
|
|
116
119
|
return includeNeedsReview ? (
|
|
117
120
|
<NeedsReviewCallout flag={{ reason: s.reason, severity: s.severity, owner: s.owner, href: s.href }} />
|
|
@@ -18,6 +18,7 @@ import type { RichText } from "./richtext.js";
|
|
|
18
18
|
import { plainText } from "./richtext.js";
|
|
19
19
|
import type { CalloutTone } from "./callout.js";
|
|
20
20
|
import type { NeedsReview } from "./needsReview.js";
|
|
21
|
+
import { BULK_QUOTE_UNITS, type BulkQuoteUnit } from "../engagement/contact/bulkQuote.js";
|
|
21
22
|
|
|
22
23
|
/** A section heading (renders <h2> or <h3>; carries/derives an anchor id). */
|
|
23
24
|
export interface ProseHeadingSection {
|
|
@@ -79,6 +80,20 @@ export interface ProseContactFormSection {
|
|
|
79
80
|
successMessage?: string;
|
|
80
81
|
}
|
|
81
82
|
|
|
83
|
+
/**
|
|
84
|
+
* A bulk / custom wholesale quote request form. The fields, endpoint and script are
|
|
85
|
+
* platform-owned (lib/engagement/contact/bulkQuote.ts): this section carries copy and an
|
|
86
|
+
* optional subset of the quote units only.
|
|
87
|
+
*/
|
|
88
|
+
export interface ProseBulkQuoteFormSection {
|
|
89
|
+
kind: "bulk_quote_form";
|
|
90
|
+
heading?: string;
|
|
91
|
+
intro?: string;
|
|
92
|
+
submitLabel?: string;
|
|
93
|
+
successMessage?: string;
|
|
94
|
+
units?: BulkQuoteUnit[];
|
|
95
|
+
}
|
|
96
|
+
|
|
82
97
|
/** A needs-review flag embedded in the flow (see needsReview.ts). */
|
|
83
98
|
export type ProseNeedsReviewSection = { kind: "needs_review" } & NeedsReview;
|
|
84
99
|
|
|
@@ -90,6 +105,7 @@ export type ProseSection =
|
|
|
90
105
|
| ProseCalloutSection
|
|
91
106
|
| ProseContactSection
|
|
92
107
|
| ProseContactFormSection
|
|
108
|
+
| ProseBulkQuoteFormSection
|
|
93
109
|
| ProseNeedsReviewSection;
|
|
94
110
|
|
|
95
111
|
/** A full structured page (policy / about / compliance). */
|
|
@@ -147,6 +163,9 @@ export function normalizeProse(
|
|
|
147
163
|
case "contact_form":
|
|
148
164
|
out.push(normalizeContactForm(s));
|
|
149
165
|
break;
|
|
166
|
+
case "bulk_quote_form":
|
|
167
|
+
out.push(normalizeBulkQuoteForm(s));
|
|
168
|
+
break;
|
|
150
169
|
case "needs_review":
|
|
151
170
|
if (s.reason.trim()) out.push(s);
|
|
152
171
|
break;
|
|
@@ -177,6 +196,23 @@ function normalizeContactForm(s: ProseContactFormSection): ProseContactFormSecti
|
|
|
177
196
|
return out;
|
|
178
197
|
}
|
|
179
198
|
|
|
199
|
+
/** Rebuild from the known copy props only; `units` keeps the declared units in canonical order. */
|
|
200
|
+
function normalizeBulkQuoteForm(s: ProseBulkQuoteFormSection): ProseBulkQuoteFormSection {
|
|
201
|
+
const raw: unknown = s.units;
|
|
202
|
+
const units = BULK_QUOTE_UNITS.filter((u) => Array.isArray(raw) && raw.includes(u));
|
|
203
|
+
const out: ProseBulkQuoteFormSection = { kind: "bulk_quote_form" };
|
|
204
|
+
const heading = copy(s.heading);
|
|
205
|
+
const intro = copy(s.intro);
|
|
206
|
+
const submitLabel = copy(s.submitLabel);
|
|
207
|
+
const successMessage = copy(s.successMessage);
|
|
208
|
+
if (heading) out.heading = heading;
|
|
209
|
+
if (intro) out.intro = intro;
|
|
210
|
+
if (submitLabel) out.submitLabel = submitLabel;
|
|
211
|
+
if (successMessage) out.successMessage = successMessage;
|
|
212
|
+
if (units.length) out.units = [...units];
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
|
|
180
216
|
/** Fill in a derived `id` for every heading that lacks one (for TOC anchoring). */
|
|
181
217
|
export function withHeadingIds(
|
|
182
218
|
sections: ReadonlyArray<ProseSection>,
|
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bulk / custom wholesale quote request: its public wire contract, its strict
|
|
3
|
+
* parser, the sealed envelope it is stored as, and the typed record the /admin inbox
|
|
4
|
+
* and order conversion read back. Browser-safe: no server imports.
|
|
5
|
+
*
|
|
6
|
+
* GET {apiBasePath}/api/contact/token?form=bulk-quote → 200 { ok:true, token }
|
|
7
|
+
* POST {apiBasePath}/api/contact/bulk-quote ← BulkQuoteSubmission (application/json, same origin)
|
|
8
|
+
* → 201 { ok:true, quoteId:"bq_<32 hex>" }
|
|
9
|
+
* 400 invalid_json | invalid_input (+ field)
|
|
10
|
+
* 403 forbidden_origin | invalid_token
|
|
11
|
+
* 409 stage-unknown · 413 too_large
|
|
12
|
+
* 415 unsupported_media_type · 429 rate_limited
|
|
13
|
+
* 503 unavailable · 500 store_failed
|
|
14
|
+
*
|
|
15
|
+
* Storage: a quote is one `contact_messages` row whose id is the quote id. Name, email
|
|
16
|
+
* and phone use the row's sealed contact columns and blind indexes; everything else is
|
|
17
|
+
* the versioned {@link BulkQuoteEnvelope}, sealed as the row's message. The server
|
|
18
|
+
* mints the id, so the `bq_` prefix is a trusted classification: a contact message id
|
|
19
|
+
* is always a UUID. A row whose id and envelope disagree is not a quote.
|
|
20
|
+
*
|
|
21
|
+
* Buyer identity (`orgId`, `shopperId`) comes only from a server-verified session and
|
|
22
|
+
* is null when the buyer is anonymous. The submission carries no identity field; one
|
|
23
|
+
* sent anyway is an unknown field and refuses.
|
|
24
|
+
*
|
|
25
|
+
* Order conversion loads a quote by id: {@link isBulkQuoteId} admits the reference,
|
|
26
|
+
* the owner or support read returns the stored message, and {@link toBulkQuoteRecord}
|
|
27
|
+
* turns it into a {@link BulkQuoteRecord} (null when the row is not a valid quote).
|
|
28
|
+
*/
|
|
29
|
+
import { CONTACT_LIMITS } from "./contract.js";
|
|
30
|
+
|
|
31
|
+
export const BULK_QUOTE_FORM = "bulk-quote" as const;
|
|
32
|
+
export const BULK_QUOTE_KIND = "bulk-quote" as const;
|
|
33
|
+
export const BULK_QUOTE_ENVELOPE_VERSION = 1 as const;
|
|
34
|
+
|
|
35
|
+
export const BULK_QUOTE_UNITS = [
|
|
36
|
+
"each",
|
|
37
|
+
"case",
|
|
38
|
+
"gallon",
|
|
39
|
+
"pail",
|
|
40
|
+
"drum",
|
|
41
|
+
"tote",
|
|
42
|
+
"pound",
|
|
43
|
+
"kilogram",
|
|
44
|
+
"liter",
|
|
45
|
+
"custom",
|
|
46
|
+
] as const;
|
|
47
|
+
export type BulkQuoteUnit = (typeof BULK_QUOTE_UNITS)[number];
|
|
48
|
+
|
|
49
|
+
export const BULK_QUOTE_LIMITS = {
|
|
50
|
+
// Every text field at its limit, at 3 UTF-8 bytes per character, plus JSON structure.
|
|
51
|
+
bodyBytes: 64 * 1024,
|
|
52
|
+
businessName: 160,
|
|
53
|
+
name: CONTACT_LIMITS.name,
|
|
54
|
+
email: CONTACT_LIMITS.email,
|
|
55
|
+
phone: CONTACT_LIMITS.phone,
|
|
56
|
+
lineItemsMin: 1,
|
|
57
|
+
lineItems: 25,
|
|
58
|
+
description: 500,
|
|
59
|
+
sku: 64,
|
|
60
|
+
packSize: 60,
|
|
61
|
+
quantityMax: 1_000_000,
|
|
62
|
+
destinationRegion: 64,
|
|
63
|
+
destinationPostalCode: 12,
|
|
64
|
+
freightNotes: 1000,
|
|
65
|
+
poNumber: 64,
|
|
66
|
+
timeline: 200,
|
|
67
|
+
notes: 2000,
|
|
68
|
+
} as const;
|
|
69
|
+
|
|
70
|
+
/** A quote id: `bq_` and 32 lowercase hex digits, minted by the server. */
|
|
71
|
+
const QUOTE_ID = /^bq_[0-9a-f]{32}$/;
|
|
72
|
+
/** Every id a contact_messages row may carry: a contact message UUID or a quote id. */
|
|
73
|
+
export const CONTACT_MESSAGE_ID_SHAPE =
|
|
74
|
+
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|bq_[0-9a-f]{32})$/;
|
|
75
|
+
/** A SQL LIKE pattern (with `ESCAPE '\'`) matching exactly the quote rows. */
|
|
76
|
+
export const BULK_QUOTE_ID_LIKE = "bq\\_%";
|
|
77
|
+
|
|
78
|
+
export function isBulkQuoteId(value: unknown): value is string {
|
|
79
|
+
return typeof value === "string" && QUOTE_ID.test(value);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function mintBulkQuoteId(): string {
|
|
83
|
+
return `bq_${crypto.randomUUID().replace(/-/g, "")}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface BulkQuoteLineItem {
|
|
87
|
+
description: string;
|
|
88
|
+
sku: string | null;
|
|
89
|
+
quantity: number;
|
|
90
|
+
unit: BulkQuoteUnit;
|
|
91
|
+
packSize: string | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface BulkQuoteFreight {
|
|
95
|
+
destinationRegion: string | null;
|
|
96
|
+
destinationPostalCode: string | null;
|
|
97
|
+
liftgate: boolean;
|
|
98
|
+
notes: string | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface BulkQuoteBuyer {
|
|
102
|
+
orgId: string | null;
|
|
103
|
+
shopperId: string | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The submission body. `company` is the honeypot: render it hidden and send it empty. */
|
|
107
|
+
export interface BulkQuoteSubmission {
|
|
108
|
+
token: string;
|
|
109
|
+
businessName: string;
|
|
110
|
+
name: string;
|
|
111
|
+
email: string;
|
|
112
|
+
phone?: string;
|
|
113
|
+
lineItems: Array<{
|
|
114
|
+
description: string;
|
|
115
|
+
sku?: string;
|
|
116
|
+
quantity: number;
|
|
117
|
+
unit: BulkQuoteUnit;
|
|
118
|
+
packSize?: string;
|
|
119
|
+
}>;
|
|
120
|
+
destinationRegion?: string;
|
|
121
|
+
destinationPostalCode?: string;
|
|
122
|
+
liftgate?: boolean;
|
|
123
|
+
freightNotes?: string;
|
|
124
|
+
poNumber?: string;
|
|
125
|
+
timeline?: string;
|
|
126
|
+
notes?: string;
|
|
127
|
+
company?: string;
|
|
128
|
+
challenge?: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type BulkQuoteField =
|
|
132
|
+
| "businessName"
|
|
133
|
+
| "name"
|
|
134
|
+
| "email"
|
|
135
|
+
| "phone"
|
|
136
|
+
| "lineItems"
|
|
137
|
+
| "description"
|
|
138
|
+
| "sku"
|
|
139
|
+
| "quantity"
|
|
140
|
+
| "unit"
|
|
141
|
+
| "packSize"
|
|
142
|
+
| "destinationRegion"
|
|
143
|
+
| "destinationPostalCode"
|
|
144
|
+
| "liftgate"
|
|
145
|
+
| "freightNotes"
|
|
146
|
+
| "poNumber"
|
|
147
|
+
| "timeline"
|
|
148
|
+
| "notes"
|
|
149
|
+
| "unknown";
|
|
150
|
+
|
|
151
|
+
/** The parsed request: contact columns plus the quote detail that is sealed as the message. */
|
|
152
|
+
export interface ParsedBulkQuote {
|
|
153
|
+
contact: { name: string; email: string; phone: string };
|
|
154
|
+
businessName: string;
|
|
155
|
+
lineItems: BulkQuoteLineItem[];
|
|
156
|
+
freight: BulkQuoteFreight;
|
|
157
|
+
poNumber: string | null;
|
|
158
|
+
timeline: string | null;
|
|
159
|
+
notes: string | null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export type BulkQuoteParse =
|
|
163
|
+
| { ok: true; quote: ParsedBulkQuote }
|
|
164
|
+
| { ok: false; field: BulkQuoteField; index?: number };
|
|
165
|
+
|
|
166
|
+
/** The sealed message of a quote row. */
|
|
167
|
+
export interface BulkQuoteEnvelope {
|
|
168
|
+
v: typeof BULK_QUOTE_ENVELOPE_VERSION;
|
|
169
|
+
kind: typeof BULK_QUOTE_KIND;
|
|
170
|
+
quoteId: string;
|
|
171
|
+
businessName: string;
|
|
172
|
+
lineItems: BulkQuoteLineItem[];
|
|
173
|
+
freight: BulkQuoteFreight;
|
|
174
|
+
poNumber: string | null;
|
|
175
|
+
timeline: string | null;
|
|
176
|
+
notes: string | null;
|
|
177
|
+
buyer: BulkQuoteBuyer;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** One quote, as /admin renders it and order conversion consumes it. */
|
|
181
|
+
export interface BulkQuoteRecord {
|
|
182
|
+
quoteId: string;
|
|
183
|
+
status: "unread" | "read";
|
|
184
|
+
createdAt: string;
|
|
185
|
+
readAt: string | null;
|
|
186
|
+
pagePath: string | null;
|
|
187
|
+
contact: { name: string; email: string; phone: string };
|
|
188
|
+
businessName: string;
|
|
189
|
+
lineItems: BulkQuoteLineItem[];
|
|
190
|
+
freight: BulkQuoteFreight;
|
|
191
|
+
poNumber: string | null;
|
|
192
|
+
timeline: string | null;
|
|
193
|
+
notes: string | null;
|
|
194
|
+
buyer: BulkQuoteBuyer;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The stored-message shape {@link toBulkQuoteRecord} reads: what the contact store opens. */
|
|
198
|
+
export interface StoredContactMessage {
|
|
199
|
+
id: string;
|
|
200
|
+
name: string;
|
|
201
|
+
email: string;
|
|
202
|
+
phone: string;
|
|
203
|
+
message: string;
|
|
204
|
+
pagePath: string | null;
|
|
205
|
+
status: "unread" | "read";
|
|
206
|
+
createdAt: string;
|
|
207
|
+
readAt: string | null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
211
|
+
const SINGLE_LINE_STRIP = /[\r\n\0]/g;
|
|
212
|
+
const NUL = /\0/g;
|
|
213
|
+
const POSTAL_CODE = /^[A-Za-z0-9][A-Za-z0-9 -]*$/;
|
|
214
|
+
const UNITS = new Set<string>(BULK_QUOTE_UNITS);
|
|
215
|
+
|
|
216
|
+
const TOP_LEVEL_KEYS = new Set([
|
|
217
|
+
"token",
|
|
218
|
+
"businessName",
|
|
219
|
+
"name",
|
|
220
|
+
"email",
|
|
221
|
+
"phone",
|
|
222
|
+
"lineItems",
|
|
223
|
+
"destinationRegion",
|
|
224
|
+
"destinationPostalCode",
|
|
225
|
+
"liftgate",
|
|
226
|
+
"freightNotes",
|
|
227
|
+
"poNumber",
|
|
228
|
+
"timeline",
|
|
229
|
+
"notes",
|
|
230
|
+
"company",
|
|
231
|
+
"challenge",
|
|
232
|
+
]);
|
|
233
|
+
const LINE_ITEM_KEYS = new Set(["description", "sku", "quantity", "unit", "packSize"]);
|
|
234
|
+
|
|
235
|
+
function singleLine(value: string): string {
|
|
236
|
+
return value.replace(SINGLE_LINE_STRIP, "").trim();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function multiLine(value: string): string {
|
|
240
|
+
return value.replace(NUL, "").trim();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
244
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
type Text = { ok: true; value: string } | { ok: false };
|
|
248
|
+
|
|
249
|
+
/** An optional string: absent or null is "", anything else must be a string within `max`. */
|
|
250
|
+
function text(raw: unknown, max: number, shape: (value: string) => string): Text {
|
|
251
|
+
if (raw === undefined || raw === null) return { ok: true, value: "" };
|
|
252
|
+
if (typeof raw !== "string") return { ok: false };
|
|
253
|
+
const value = shape(raw);
|
|
254
|
+
return value.length <= max ? { ok: true, value } : { ok: false };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function orNull(value: string): string | null {
|
|
258
|
+
return value === "" ? null : value;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function parseLineItem(raw: unknown): { ok: true; item: BulkQuoteLineItem } | { ok: false; field: BulkQuoteField } {
|
|
262
|
+
if (!isPlainObject(raw)) return { ok: false, field: "lineItems" };
|
|
263
|
+
for (const key of Object.keys(raw)) if (!LINE_ITEM_KEYS.has(key)) return { ok: false, field: "unknown" };
|
|
264
|
+
const description = text(raw.description, BULK_QUOTE_LIMITS.description, multiLine);
|
|
265
|
+
if (!description.ok || description.value === "") return { ok: false, field: "description" };
|
|
266
|
+
const sku = text(raw.sku, BULK_QUOTE_LIMITS.sku, singleLine);
|
|
267
|
+
if (!sku.ok) return { ok: false, field: "sku" };
|
|
268
|
+
const quantity = raw.quantity;
|
|
269
|
+
if (typeof quantity !== "number" || !Number.isFinite(quantity) || quantity <= 0 || quantity > BULK_QUOTE_LIMITS.quantityMax) {
|
|
270
|
+
return { ok: false, field: "quantity" };
|
|
271
|
+
}
|
|
272
|
+
if (typeof raw.unit !== "string" || !UNITS.has(raw.unit)) return { ok: false, field: "unit" };
|
|
273
|
+
const packSize = text(raw.packSize, BULK_QUOTE_LIMITS.packSize, singleLine);
|
|
274
|
+
if (!packSize.ok) return { ok: false, field: "packSize" };
|
|
275
|
+
return {
|
|
276
|
+
ok: true,
|
|
277
|
+
item: {
|
|
278
|
+
description: description.value,
|
|
279
|
+
sku: orNull(sku.value),
|
|
280
|
+
quantity,
|
|
281
|
+
unit: raw.unit as BulkQuoteUnit,
|
|
282
|
+
packSize: orNull(packSize.value),
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Parse one submission body. Every top-level and line-item key must be known; strings are
|
|
289
|
+
* capped and single-line values lose CR, LF and NUL. The token, challenge and honeypot are
|
|
290
|
+
* the route's to check and are only admitted here as known keys.
|
|
291
|
+
*/
|
|
292
|
+
export function parseBulkQuoteSubmission(body: Record<string, unknown>): BulkQuoteParse {
|
|
293
|
+
for (const key of Object.keys(body)) if (!TOP_LEVEL_KEYS.has(key)) return { ok: false, field: "unknown" };
|
|
294
|
+
|
|
295
|
+
const businessName = text(body.businessName, BULK_QUOTE_LIMITS.businessName, singleLine);
|
|
296
|
+
if (!businessName.ok || businessName.value === "") return { ok: false, field: "businessName" };
|
|
297
|
+
const name = text(body.name, BULK_QUOTE_LIMITS.name, singleLine);
|
|
298
|
+
if (!name.ok || name.value === "") return { ok: false, field: "name" };
|
|
299
|
+
const email = text(body.email, BULK_QUOTE_LIMITS.email, (v) => singleLine(v).toLowerCase());
|
|
300
|
+
if (!email.ok || !EMAIL_RE.test(email.value)) return { ok: false, field: "email" };
|
|
301
|
+
const phone = text(body.phone, BULK_QUOTE_LIMITS.phone, singleLine);
|
|
302
|
+
if (!phone.ok) return { ok: false, field: "phone" };
|
|
303
|
+
|
|
304
|
+
const rawItems = body.lineItems;
|
|
305
|
+
if (!Array.isArray(rawItems) || rawItems.length < BULK_QUOTE_LIMITS.lineItemsMin || rawItems.length > BULK_QUOTE_LIMITS.lineItems) {
|
|
306
|
+
return { ok: false, field: "lineItems" };
|
|
307
|
+
}
|
|
308
|
+
const lineItems: BulkQuoteLineItem[] = [];
|
|
309
|
+
for (const [index, raw] of rawItems.entries()) {
|
|
310
|
+
const parsed = parseLineItem(raw);
|
|
311
|
+
if (!parsed.ok) return { ok: false, field: parsed.field, index };
|
|
312
|
+
lineItems.push(parsed.item);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const region = text(body.destinationRegion, BULK_QUOTE_LIMITS.destinationRegion, singleLine);
|
|
316
|
+
if (!region.ok) return { ok: false, field: "destinationRegion" };
|
|
317
|
+
const postal = text(body.destinationPostalCode, BULK_QUOTE_LIMITS.destinationPostalCode, singleLine);
|
|
318
|
+
if (!postal.ok || (postal.value !== "" && !POSTAL_CODE.test(postal.value))) return { ok: false, field: "destinationPostalCode" };
|
|
319
|
+
const liftgate = body.liftgate;
|
|
320
|
+
if (liftgate !== undefined && liftgate !== null && typeof liftgate !== "boolean") return { ok: false, field: "liftgate" };
|
|
321
|
+
const freightNotes = text(body.freightNotes, BULK_QUOTE_LIMITS.freightNotes, multiLine);
|
|
322
|
+
if (!freightNotes.ok) return { ok: false, field: "freightNotes" };
|
|
323
|
+
const poNumber = text(body.poNumber, BULK_QUOTE_LIMITS.poNumber, singleLine);
|
|
324
|
+
if (!poNumber.ok) return { ok: false, field: "poNumber" };
|
|
325
|
+
const timeline = text(body.timeline, BULK_QUOTE_LIMITS.timeline, singleLine);
|
|
326
|
+
if (!timeline.ok) return { ok: false, field: "timeline" };
|
|
327
|
+
const notes = text(body.notes, BULK_QUOTE_LIMITS.notes, multiLine);
|
|
328
|
+
if (!notes.ok) return { ok: false, field: "notes" };
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
ok: true,
|
|
332
|
+
quote: {
|
|
333
|
+
contact: { name: name.value, email: email.value, phone: phone.value },
|
|
334
|
+
businessName: businessName.value,
|
|
335
|
+
lineItems,
|
|
336
|
+
freight: {
|
|
337
|
+
destinationRegion: orNull(region.value),
|
|
338
|
+
destinationPostalCode: orNull(postal.value),
|
|
339
|
+
liftgate: liftgate === true,
|
|
340
|
+
notes: orNull(freightNotes.value),
|
|
341
|
+
},
|
|
342
|
+
poNumber: orNull(poNumber.value),
|
|
343
|
+
timeline: orNull(timeline.value),
|
|
344
|
+
notes: orNull(notes.value),
|
|
345
|
+
},
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The sealed message text for one quote. */
|
|
350
|
+
export function encodeBulkQuoteEnvelope(quoteId: string, quote: ParsedBulkQuote, buyer: BulkQuoteBuyer): string {
|
|
351
|
+
if (!isBulkQuoteId(quoteId)) throw new Error("bulk quote id malformed");
|
|
352
|
+
const envelope: BulkQuoteEnvelope = {
|
|
353
|
+
v: BULK_QUOTE_ENVELOPE_VERSION,
|
|
354
|
+
kind: BULK_QUOTE_KIND,
|
|
355
|
+
quoteId,
|
|
356
|
+
businessName: quote.businessName,
|
|
357
|
+
lineItems: quote.lineItems,
|
|
358
|
+
freight: quote.freight,
|
|
359
|
+
poNumber: quote.poNumber,
|
|
360
|
+
timeline: quote.timeline,
|
|
361
|
+
notes: quote.notes,
|
|
362
|
+
buyer: { orgId: buyer.orgId, shopperId: buyer.shopperId },
|
|
363
|
+
};
|
|
364
|
+
return JSON.stringify(envelope);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const ENVELOPE_KEYS = ["businessName", "buyer", "freight", "kind", "lineItems", "notes", "poNumber", "quoteId", "timeline", "v"].sort().join(",");
|
|
368
|
+
const IDENTITY = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
369
|
+
|
|
370
|
+
function nullableText(value: unknown, max: number): value is string | null {
|
|
371
|
+
return value === null || (typeof value === "string" && value !== "" && value.length <= max);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function identity(value: unknown): value is string | null {
|
|
375
|
+
return value === null || (typeof value === "string" && IDENTITY.test(value));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Decode one stored message as the quote whose id it carries. Null when the id is not a
|
|
380
|
+
* quote id, or when the envelope is not exactly a valid version-1 quote for that id.
|
|
381
|
+
*/
|
|
382
|
+
export function decodeBulkQuoteEnvelope(id: string, message: string): BulkQuoteEnvelope | null {
|
|
383
|
+
if (!isBulkQuoteId(id)) return null;
|
|
384
|
+
let parsed: unknown;
|
|
385
|
+
try {
|
|
386
|
+
parsed = JSON.parse(message);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
389
|
+
parsed = undefined;
|
|
390
|
+
}
|
|
391
|
+
if (!isPlainObject(parsed) || Object.keys(parsed).sort().join(",") !== ENVELOPE_KEYS) return null;
|
|
392
|
+
const e = parsed;
|
|
393
|
+
if (e.v !== BULK_QUOTE_ENVELOPE_VERSION || e.kind !== BULK_QUOTE_KIND || e.quoteId !== id) return null;
|
|
394
|
+
if (typeof e.businessName !== "string" || e.businessName === "" || e.businessName.length > BULK_QUOTE_LIMITS.businessName) return null;
|
|
395
|
+
if (!Array.isArray(e.lineItems) || e.lineItems.length < BULK_QUOTE_LIMITS.lineItemsMin || e.lineItems.length > BULK_QUOTE_LIMITS.lineItems) return null;
|
|
396
|
+
const lineItems: BulkQuoteLineItem[] = [];
|
|
397
|
+
for (const raw of e.lineItems) {
|
|
398
|
+
if (!isPlainObject(raw) || Object.keys(raw).sort().join(",") !== "description,packSize,quantity,sku,unit") return null;
|
|
399
|
+
const parsedItem = parseLineItem({
|
|
400
|
+
description: raw.description,
|
|
401
|
+
sku: raw.sku,
|
|
402
|
+
quantity: raw.quantity,
|
|
403
|
+
unit: raw.unit,
|
|
404
|
+
packSize: raw.packSize,
|
|
405
|
+
});
|
|
406
|
+
if (!parsedItem.ok || parsedItem.item.description !== raw.description) return null;
|
|
407
|
+
lineItems.push(parsedItem.item);
|
|
408
|
+
}
|
|
409
|
+
const f = e.freight;
|
|
410
|
+
if (!isPlainObject(f) || Object.keys(f).sort().join(",") !== "destinationPostalCode,destinationRegion,liftgate,notes") return null;
|
|
411
|
+
if (!nullableText(f.destinationRegion, BULK_QUOTE_LIMITS.destinationRegion)) return null;
|
|
412
|
+
if (!nullableText(f.destinationPostalCode, BULK_QUOTE_LIMITS.destinationPostalCode)) return null;
|
|
413
|
+
if (typeof f.liftgate !== "boolean" || !nullableText(f.notes, BULK_QUOTE_LIMITS.freightNotes)) return null;
|
|
414
|
+
if (!nullableText(e.poNumber, BULK_QUOTE_LIMITS.poNumber)) return null;
|
|
415
|
+
if (!nullableText(e.timeline, BULK_QUOTE_LIMITS.timeline)) return null;
|
|
416
|
+
if (!nullableText(e.notes, BULK_QUOTE_LIMITS.notes)) return null;
|
|
417
|
+
const b = e.buyer;
|
|
418
|
+
if (!isPlainObject(b) || Object.keys(b).sort().join(",") !== "orgId,shopperId" || !identity(b.orgId) || !identity(b.shopperId)) return null;
|
|
419
|
+
return {
|
|
420
|
+
v: BULK_QUOTE_ENVELOPE_VERSION,
|
|
421
|
+
kind: BULK_QUOTE_KIND,
|
|
422
|
+
quoteId: id,
|
|
423
|
+
businessName: e.businessName,
|
|
424
|
+
lineItems,
|
|
425
|
+
freight: {
|
|
426
|
+
destinationRegion: f.destinationRegion,
|
|
427
|
+
destinationPostalCode: f.destinationPostalCode,
|
|
428
|
+
liftgate: f.liftgate,
|
|
429
|
+
notes: f.notes,
|
|
430
|
+
},
|
|
431
|
+
poNumber: e.poNumber,
|
|
432
|
+
timeline: e.timeline,
|
|
433
|
+
notes: e.notes,
|
|
434
|
+
buyer: { orgId: b.orgId, shopperId: b.shopperId },
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** The quote a stored message holds, or null when it is not a valid quote. */
|
|
439
|
+
export function toBulkQuoteRecord(message: StoredContactMessage): BulkQuoteRecord | null {
|
|
440
|
+
const envelope = decodeBulkQuoteEnvelope(message.id, message.message);
|
|
441
|
+
if (!envelope) return null;
|
|
442
|
+
return {
|
|
443
|
+
quoteId: envelope.quoteId,
|
|
444
|
+
status: message.status,
|
|
445
|
+
createdAt: message.createdAt,
|
|
446
|
+
readAt: message.readAt,
|
|
447
|
+
pagePath: message.pagePath,
|
|
448
|
+
contact: { name: message.name, email: message.email, phone: message.phone },
|
|
449
|
+
businessName: envelope.businessName,
|
|
450
|
+
lineItems: envelope.lineItems,
|
|
451
|
+
freight: envelope.freight,
|
|
452
|
+
poNumber: envelope.poNumber,
|
|
453
|
+
timeline: envelope.timeline,
|
|
454
|
+
notes: envelope.notes,
|
|
455
|
+
buyer: envelope.buyer,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export type BulkQuoteErrorCode =
|
|
460
|
+
| "not_found"
|
|
461
|
+
| "invalid_json"
|
|
462
|
+
| "invalid_input"
|
|
463
|
+
| "forbidden_origin"
|
|
464
|
+
| "invalid_token"
|
|
465
|
+
| "too_large"
|
|
466
|
+
| "unsupported_media_type"
|
|
467
|
+
| "rate_limited"
|
|
468
|
+
| "unavailable"
|
|
469
|
+
| "stage-unknown"
|
|
470
|
+
| "store_failed";
|
|
471
|
+
|
|
472
|
+
export type BulkQuoteSubmitResponse =
|
|
473
|
+
| { ok: true; quoteId: string }
|
|
474
|
+
| { ok: false; error: BulkQuoteErrorCode; field?: BulkQuoteField; index?: number };
|
|
475
|
+
|
|
476
|
+
export interface BulkQuoteClientResult {
|
|
477
|
+
status: number;
|
|
478
|
+
body: BulkQuoteSubmitResponse;
|
|
479
|
+
/** True when the buyer should keep their entries and may try again. */
|
|
480
|
+
retryable: boolean;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const RETRYABLE = new Set([429, 500, 503]);
|
|
484
|
+
|
|
485
|
+
function apiUrl(apiBasePath: string, path: string): string {
|
|
486
|
+
return `${apiBasePath.replace(/\/+$/, "")}${path}`;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** Fetch a fresh bulk-quote submit token. Call when the form is shown, not on submit. */
|
|
490
|
+
export async function fetchBulkQuoteToken(apiBasePath: string, fetchImpl: typeof fetch = fetch): Promise<string> {
|
|
491
|
+
const res = await fetchImpl(apiUrl(apiBasePath, `/api/contact/token?form=${BULK_QUOTE_FORM}`), {
|
|
492
|
+
headers: { accept: "application/json" },
|
|
493
|
+
credentials: "same-origin",
|
|
494
|
+
});
|
|
495
|
+
if (!res.ok) throw new Error(`bulk quote token unavailable (${res.status})`);
|
|
496
|
+
const body = (await res.json()) as { ok?: unknown; token?: unknown };
|
|
497
|
+
if (body.ok !== true || typeof body.token !== "string") throw new Error("bulk quote token malformed");
|
|
498
|
+
return body.token;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Post one quote request. A transport failure throws; every HTTP outcome resolves. */
|
|
502
|
+
export async function submitBulkQuote(
|
|
503
|
+
apiBasePath: string,
|
|
504
|
+
submission: BulkQuoteSubmission,
|
|
505
|
+
fetchImpl: typeof fetch = fetch,
|
|
506
|
+
): Promise<BulkQuoteClientResult> {
|
|
507
|
+
const res = await fetchImpl(apiUrl(apiBasePath, "/api/contact/bulk-quote"), {
|
|
508
|
+
method: "POST",
|
|
509
|
+
headers: { "content-type": "application/json", accept: "application/json" },
|
|
510
|
+
credentials: "same-origin",
|
|
511
|
+
body: JSON.stringify(submission),
|
|
512
|
+
});
|
|
513
|
+
const raw = await res.text();
|
|
514
|
+
let body: BulkQuoteSubmitResponse;
|
|
515
|
+
try {
|
|
516
|
+
body = JSON.parse(raw) as BulkQuoteSubmitResponse;
|
|
517
|
+
} catch {
|
|
518
|
+
body = { ok: false, error: res.ok ? "store_failed" : "unavailable" };
|
|
519
|
+
}
|
|
520
|
+
return { status: res.status, body, retryable: RETRYABLE.has(res.status) };
|
|
521
|
+
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Server-signed minimum-time-to-submit token for the contact
|
|
2
|
+
* Server-signed minimum-time-to-submit token for the public contact forms.
|
|
3
3
|
*
|
|
4
|
-
* `issueContactToken` signs `{ tenant, issuedAt }` with HMAC-SHA256 under
|
|
5
|
-
* `CONTACT_FORM_TOKEN_SECRET
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* `issueContactToken` signs `{ tenant, form, issuedAt }` with HMAC-SHA256 under
|
|
5
|
+
* `CONTACT_FORM_TOKEN_SECRET`, behind a per-form MAC prefix; `verifyContactToken`
|
|
6
|
+
* accepts it only for the same tenant and the same form, between
|
|
7
|
+
* {@link CONTACT_TOKEN_MIN_AGE_MS} and {@link CONTACT_TOKEN_MAX_AGE_MS} after issue.
|
|
8
|
+
* A token for one form never authorizes another. A script that posts without first
|
|
9
|
+
* loading the form, or posts faster than a person can type, is refused. Web Crypto only.
|
|
9
10
|
*
|
|
10
11
|
* Wire form: `v1.<base64url(JSON payload)>.<base64url(HMAC)>`.
|
|
11
12
|
*/
|
|
@@ -14,9 +15,21 @@ import { CONTACT_TOKEN_MAX_AGE_MS, CONTACT_TOKEN_MIN_AGE_MS } from "./contract.j
|
|
|
14
15
|
const VERSION = "v1";
|
|
15
16
|
const encoder = new TextEncoder();
|
|
16
17
|
|
|
18
|
+
/** The public forms a submit token can be issued for. */
|
|
19
|
+
export type ContactFormKind = "contact" | "bulk-quote";
|
|
20
|
+
|
|
21
|
+
const MAC_PREFIX: Record<ContactFormKind, string> = {
|
|
22
|
+
contact: "contact-form",
|
|
23
|
+
"bulk-quote": "bulk-quote-form",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function isContactFormKind(value: unknown): value is ContactFormKind {
|
|
27
|
+
return typeof value === "string" && Object.hasOwn(MAC_PREFIX, value);
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
export type ContactTokenVerdict =
|
|
18
31
|
| { ok: true }
|
|
19
|
-
| { ok: false; reason: "malformed" | "bad_signature" | "wrong_tenant" | "too_early" | "expired" };
|
|
32
|
+
| { ok: false; reason: "malformed" | "bad_signature" | "wrong_form" | "wrong_tenant" | "too_early" | "expired" };
|
|
20
33
|
|
|
21
34
|
function base64url(bytes: Uint8Array): string {
|
|
22
35
|
let bin = "";
|
|
@@ -36,17 +49,18 @@ function hmacKey(secret: string, usage: "sign" | "verify"): Promise<CryptoKey> {
|
|
|
36
49
|
return crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [usage]);
|
|
37
50
|
}
|
|
38
51
|
|
|
39
|
-
function signedBytes(payload: string): Uint8Array<ArrayBuffer> {
|
|
40
|
-
return new Uint8Array(encoder.encode(
|
|
52
|
+
function signedBytes(form: ContactFormKind, payload: string): Uint8Array<ArrayBuffer> {
|
|
53
|
+
return new Uint8Array(encoder.encode(`${MAC_PREFIX[form]}:${VERSION}:${payload}`));
|
|
41
54
|
}
|
|
42
55
|
|
|
43
56
|
export async function issueContactToken(
|
|
44
57
|
secret: string,
|
|
45
58
|
tenantId: string,
|
|
46
59
|
nowMs: number = Date.now(),
|
|
60
|
+
form: ContactFormKind = "contact",
|
|
47
61
|
): Promise<string> {
|
|
48
|
-
const payload = base64url(encoder.encode(JSON.stringify({ tenant: tenantId, issuedAt: nowMs })));
|
|
49
|
-
const mac = await crypto.subtle.sign("HMAC", await hmacKey(secret, "sign"), signedBytes(payload));
|
|
62
|
+
const payload = base64url(encoder.encode(JSON.stringify({ tenant: tenantId, form, issuedAt: nowMs })));
|
|
63
|
+
const mac = await crypto.subtle.sign("HMAC", await hmacKey(secret, "sign"), signedBytes(form, payload));
|
|
50
64
|
return `${VERSION}.${payload}.${base64url(new Uint8Array(mac))}`;
|
|
51
65
|
}
|
|
52
66
|
|
|
@@ -55,6 +69,7 @@ export async function verifyContactToken(
|
|
|
55
69
|
token: string,
|
|
56
70
|
tenantId: string,
|
|
57
71
|
nowMs: number = Date.now(),
|
|
72
|
+
form: ContactFormKind = "contact",
|
|
58
73
|
): Promise<ContactTokenVerdict> {
|
|
59
74
|
const parts = token.split(".");
|
|
60
75
|
if (parts.length !== 3 || parts[0] !== VERSION) return { ok: false, reason: "malformed" };
|
|
@@ -63,10 +78,10 @@ export async function verifyContactToken(
|
|
|
63
78
|
const payloadBytes = fromBase64url(payload);
|
|
64
79
|
if (!sigBytes || !payloadBytes) return { ok: false, reason: "malformed" };
|
|
65
80
|
|
|
66
|
-
const valid = await crypto.subtle.verify("HMAC", await hmacKey(secret, "verify"), sigBytes, signedBytes(payload));
|
|
81
|
+
const valid = await crypto.subtle.verify("HMAC", await hmacKey(secret, "verify"), sigBytes, signedBytes(form, payload));
|
|
67
82
|
if (!valid) return { ok: false, reason: "bad_signature" };
|
|
68
83
|
|
|
69
|
-
let claims: { tenant?: unknown; issuedAt?: unknown };
|
|
84
|
+
let claims: { tenant?: unknown; form?: unknown; issuedAt?: unknown };
|
|
70
85
|
try {
|
|
71
86
|
claims = JSON.parse(new TextDecoder().decode(payloadBytes)) as typeof claims;
|
|
72
87
|
} catch {
|
|
@@ -75,6 +90,7 @@ export async function verifyContactToken(
|
|
|
75
90
|
if (typeof claims.issuedAt !== "number" || !Number.isFinite(claims.issuedAt)) {
|
|
76
91
|
return { ok: false, reason: "malformed" };
|
|
77
92
|
}
|
|
93
|
+
if (claims.form !== form) return { ok: false, reason: "wrong_form" };
|
|
78
94
|
if (claims.tenant !== tenantId) return { ok: false, reason: "wrong_tenant" };
|
|
79
95
|
const age = nowMs - claims.issuedAt;
|
|
80
96
|
if (age < CONTACT_TOKEN_MIN_AGE_MS) return { ok: false, reason: "too_early" };
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `GET /api/contact/token` — a fresh signed submit token
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* `GET /api/contact/token[?form=contact|bulk-quote]` — a fresh signed submit token
|
|
3
|
+
* for one of this tenant's public forms (`lib/engagement/contact/submitToken.ts`).
|
|
4
|
+
* The token is bound to the form, so a contact token cannot submit a bulk quote and
|
|
5
|
+
* the reverse. No `form` means the contact form; an unknown one is 400. Without
|
|
6
|
+
* `CONTACT_FORM_TOKEN_SECRET` the forms cannot work, so this answers 503 rather
|
|
5
7
|
* than handing out an unsigned token.
|
|
6
8
|
*/
|
|
7
9
|
import type { APIContext } from "astro";
|
|
8
10
|
import { readEnv } from "@/lib/publication/shared/env";
|
|
9
|
-
import { issueContactToken } from "@/lib/engagement/contact/submitToken";
|
|
11
|
+
import { issueContactToken, isContactFormKind } from "@/lib/engagement/contact/submitToken";
|
|
10
12
|
|
|
11
13
|
export const prerender = false;
|
|
12
14
|
|
|
@@ -19,11 +21,15 @@ const HEADERS = {
|
|
|
19
21
|
export async function GET(context: APIContext): Promise<Response> {
|
|
20
22
|
const tenantId = context.locals?.tenant?.tenant_id;
|
|
21
23
|
if (!tenantId) return new Response(JSON.stringify({ ok: false, error: "not_found" }), { status: 404, headers: HEADERS });
|
|
24
|
+
const requested = new URL(context.request.url).searchParams.get("form") ?? "contact";
|
|
25
|
+
if (!isContactFormKind(requested)) {
|
|
26
|
+
return new Response(JSON.stringify({ ok: false, error: "invalid_input" }), { status: 400, headers: HEADERS });
|
|
27
|
+
}
|
|
22
28
|
const secret = await readEnv("CONTACT_FORM_TOKEN_SECRET");
|
|
23
29
|
if (!secret) {
|
|
24
30
|
console.log(`[contact] tenant=${tenantId} outcome=token_no_secret`);
|
|
25
31
|
return new Response(JSON.stringify({ ok: false, error: "unavailable" }), { status: 503, headers: HEADERS });
|
|
26
32
|
}
|
|
27
|
-
const token = await issueContactToken(secret, tenantId);
|
|
33
|
+
const token = await issueContactToken(secret, tenantId, Date.now(), requested);
|
|
28
34
|
return new Response(JSON.stringify({ ok: true, token }), { headers: HEADERS });
|
|
29
35
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "7.6.
|
|
3
|
+
"version": "7.6.18",
|
|
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",
|
|
@@ -10,6 +10,7 @@ import { extname, join, relative } from "node:path";
|
|
|
10
10
|
import { workspacePathForTenantRelative } from "./tot-repo-mappings.mjs";
|
|
11
11
|
|
|
12
12
|
const ERROR = "error";
|
|
13
|
+
const BULK_QUOTE_UNITS = ["each", "case", "gallon", "pail", "drum", "tote", "pound", "kilogram", "liter", "custom"];
|
|
13
14
|
// Public routes implemented by the renderer. Authentication and cart URLs are
|
|
14
15
|
// deliberately absent: they are flow-specific, not portable parity targets.
|
|
15
16
|
const PLATFORM_ROUTES = ["/collections", "/collections/", "/products/", "/search", "/saved", "/account", "/account/", "/blog", "/blog/"];
|
|
@@ -256,8 +257,9 @@ function pageShape(value, file) {
|
|
|
256
257
|
case "callout": if (!rich(section.body) || !["info", "warning", "legal", "success"].includes(section.tone) || section.title !== undefined && !string(section.title)) findings.push(finding("parity-section-shape", at, "callout requires supported tone, RichText body, and optional string title", "supply a valid callout")); break;
|
|
257
258
|
case "contact": if (section.heading !== undefined && !string(section.heading) || section.email !== undefined && !string(section.email) || section.phone !== undefined && !string(section.phone) || section.address !== undefined && (!Array.isArray(section.address) || section.address.some((line) => !string(line))) || !string(section.email) && !string(section.phone) && !(Array.isArray(section.address) && section.address.length)) findings.push(finding("parity-section-shape", at, "contact requires string members and email, phone, or address", "supply valid contact details")); break;
|
|
258
259
|
case "contact_form": if (["heading", "messageLabel", "submitLabel", "successMessage"].some((key) => section[key] !== undefined && !string(section[key])) || section.fields !== undefined && (!Array.isArray(section.fields) || section.fields.some((field) => field !== "name" && field !== "phone"))) findings.push(finding("parity-section-shape", at, "contact_form requires optional string copy and fields limited to name or phone", "supply a valid contact_form section")); break;
|
|
260
|
+
case "bulk_quote_form": if (["heading", "intro", "submitLabel", "successMessage"].some((key) => section[key] !== undefined && !string(section[key])) || section.units !== undefined && (!Array.isArray(section.units) || section.units.some((unit) => !BULK_QUOTE_UNITS.includes(unit)))) findings.push(finding("parity-section-shape", at, "bulk_quote_form requires optional string copy and units limited to the supported quote units", "supply a valid bulk_quote_form section")); break;
|
|
259
261
|
case "needs_review": if (!string(section.reason) || section.severity !== undefined && !["blocker", "note"].includes(section.severity) || section.owner !== undefined && !string(section.owner) || section.href !== undefined && !string(section.href)) findings.push(finding("parity-section-shape", at, "needs_review requires reason and valid optional severity/owner/href", "supply a valid needs_review section")); break;
|
|
260
|
-
default: findings.push(finding("parity-section-kind", `${at}/kind`, "section kind is not supported by the renderer", "use heading, paragraph, list, definitions, callout, contact, contact_form, or needs_review"));
|
|
262
|
+
default: findings.push(finding("parity-section-kind", `${at}/kind`, "section kind is not supported by the renderer", "use heading, paragraph, list, definitions, callout, contact, contact_form, bulk_quote_form, or needs_review"));
|
|
261
263
|
}
|
|
262
264
|
});
|
|
263
265
|
const bodyHtml = typeof value.body_html === "string" && value.body_html.trim() !== "";
|