@semics-tech/emdash-events 0.0.0-stage → 1.0.0-rc.1
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/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +416 -0
- package/dist/manifest.json +425 -0
- package/dist/plugin.d.mts +11 -0
- package/dist/plugin.mjs +7 -0
- package/dist/ts/astro/lib/call.d.ts +54 -0
- package/dist/ts/astro/lib/call.js +121 -0
- package/dist/ts/integration/index.d.ts +48 -0
- package/dist/ts/integration/index.js +76 -0
- package/dist/ts/ui/index.d.ts +6 -0
- package/dist/ts/ui/index.js +46 -0
- package/emdash-plugin.jsonc +222 -0
- package/package.json +78 -3
- package/src/astro/AddToCalendar.astro +47 -0
- package/src/astro/AttendeeList.astro +157 -0
- package/src/astro/BookingBlock.astro +30 -0
- package/src/astro/BookingPage.astro +230 -0
- package/src/astro/BookingStatus.astro +112 -0
- package/src/astro/BookingWidget.astro +776 -0
- package/src/astro/CheckIn.astro +186 -0
- package/src/astro/DoorSignPage.astro +62 -0
- package/src/astro/EventCard.astro +81 -0
- package/src/astro/EventList.astro +83 -0
- package/src/astro/OptOutPage.astro +58 -0
- package/src/astro/PaymentReturn.astro +96 -0
- package/src/astro/TicketPass.astro +95 -0
- package/src/astro/checkout-endpoint.ts +11 -0
- package/src/astro/index.js +18 -0
- package/src/astro/lib/attendees.ts +170 -0
- package/src/astro/lib/call.ts +185 -0
- package/src/astro/lib/checkout-form.ts +271 -0
- package/src/astro/lib/list.ts +99 -0
- package/src/astro/lib/opt-out.ts +49 -0
- package/src/astro/lib/pass.ts +35 -0
- package/src/astro/lib/payment.ts +73 -0
- package/src/astro/lib/picker.ts +245 -0
- package/src/astro/lib/promo.ts +162 -0
- package/src/astro/lib/questions.ts +107 -0
- package/src/astro/lib/receipt.ts +132 -0
- package/src/astro/lib/waitlist.ts +72 -0
- package/src/astro/lib/widget.ts +299 -0
- package/src/astro/promo-endpoint.ts +11 -0
- package/src/astro/virtual.d.ts +16 -0
- package/src/astro/waitlist-endpoint.ts +12 -0
- package/src/client/booking.ts +190 -0
- package/src/client/checkin.ts +260 -0
- package/src/client/payment.ts +76 -0
- package/src/client/picker.ts +143 -0
- package/src/client/receipt.ts +75 -0
- package/src/i18n/ar.ts +251 -0
- package/src/i18n/en.ts +255 -0
- package/src/i18n/index.ts +121 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom attendee questions in the booking widget (EV-FR-15, EU-FR-10, T080). Questions are Events
|
|
3
|
+
* Pro's: `public/event` lists them only on a Pro core, and Pro's reserve checks the answers (required
|
|
4
|
+
* ones, offered choices) and stores them on the booking. This module turns the listed questions into
|
|
5
|
+
* the widget's controls and reads the answers back from the posted form. Pure.
|
|
6
|
+
*
|
|
7
|
+
* The buyer answers once per booking. Each control is named `answer:<id>`; the form also carries a
|
|
8
|
+
* `questions` marker so the order sends `answers` (even `{}`) only when questions were shown: Free's
|
|
9
|
+
* reserve refuses unknown keys.
|
|
10
|
+
*/
|
|
11
|
+
import { t } from "../../i18n/index";
|
|
12
|
+
import { type PublicQuestion, QUESTION_ID, type QuestionField, type WidgetField } from "./widget";
|
|
13
|
+
|
|
14
|
+
export const ANSWER_PREFIX = "answer:";
|
|
15
|
+
export const QUESTIONS_MARKER = "questions";
|
|
16
|
+
/** The longest text answer (Pro's reserve refuses longer ones). */
|
|
17
|
+
export const MAX_ANSWER = 500;
|
|
18
|
+
|
|
19
|
+
/** English messages by question type (the 1.0 API; `questionControls` translates). */
|
|
20
|
+
export const QUESTION_MESSAGES: Record<PublicQuestion["type"], string> = {
|
|
21
|
+
text: t("en", "question.error.text"),
|
|
22
|
+
select: t("en", "question.error.select"),
|
|
23
|
+
checkbox: t("en", "question.error.checkbox"),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface QuestionControl {
|
|
27
|
+
kind: PublicQuestion["type"];
|
|
28
|
+
/** The question's id. */
|
|
29
|
+
question: string;
|
|
30
|
+
/** The control's HTML id, unique on the page. */
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
field: QuestionField;
|
|
34
|
+
label: string;
|
|
35
|
+
required: boolean;
|
|
36
|
+
/** Shown after the label of an optional question ("(optional)"). */
|
|
37
|
+
hint: string | undefined;
|
|
38
|
+
errorId: string;
|
|
39
|
+
/** What the error says when the question needs an answer. */
|
|
40
|
+
message: string;
|
|
41
|
+
invalid: boolean;
|
|
42
|
+
/** A choice question's options, after an empty prompt. */
|
|
43
|
+
options?: Array<{ value: string; label: string }>;
|
|
44
|
+
maxLength?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const KINDS = new Set<string>(["text", "select", "checkbox"]);
|
|
48
|
+
|
|
49
|
+
function wellFormed(q: PublicQuestion): boolean {
|
|
50
|
+
if (!q || typeof q !== "object" || typeof q.id !== "string" || !QUESTION_ID.test(q.id)) return false;
|
|
51
|
+
if (typeof q.label !== "string" || q.label.trim() === "" || !KINDS.has(q.type)) return false;
|
|
52
|
+
return q.type !== "select" || (Array.isArray(q.options) && q.options.some((o) => typeof o === "string" && o !== ""));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The widget's controls for `questions`, in order; `bad` are the fields a refusal named. */
|
|
56
|
+
export function questionControls(
|
|
57
|
+
questions: readonly PublicQuestion[] | undefined,
|
|
58
|
+
anchor: string,
|
|
59
|
+
bad: readonly WidgetField[] | readonly string[],
|
|
60
|
+
locale = "en-GB",
|
|
61
|
+
): QuestionControl[] {
|
|
62
|
+
return (questions ?? []).filter(wellFormed).map((q) => {
|
|
63
|
+
const id = `${anchor}-q-${q.id}`;
|
|
64
|
+
const field: QuestionField = `q:${q.id}`;
|
|
65
|
+
const required = q.required === true;
|
|
66
|
+
return {
|
|
67
|
+
kind: q.type,
|
|
68
|
+
question: q.id,
|
|
69
|
+
id,
|
|
70
|
+
name: `${ANSWER_PREFIX}${q.id}`,
|
|
71
|
+
field,
|
|
72
|
+
label: q.label.trim(),
|
|
73
|
+
required,
|
|
74
|
+
hint: required ? undefined : t(locale, "question.optional"),
|
|
75
|
+
errorId: `${id}-error`,
|
|
76
|
+
message: t(locale, `question.error.${q.type}`),
|
|
77
|
+
invalid: (bad as readonly string[]).includes(field),
|
|
78
|
+
...(q.type === "select"
|
|
79
|
+
? {
|
|
80
|
+
options: [
|
|
81
|
+
{ value: "", label: t(locale, "question.choose") },
|
|
82
|
+
...(q.options ?? [])
|
|
83
|
+
.filter((o) => typeof o === "string" && o !== "")
|
|
84
|
+
.map((o) => ({ value: o, label: o })),
|
|
85
|
+
],
|
|
86
|
+
}
|
|
87
|
+
: {}),
|
|
88
|
+
...(q.type === "text" ? { maxLength: MAX_ANSWER } : {}),
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The answers in a posted widget form, keyed by question id (text trimmed; a ticked box posts `yes`), or
|
|
95
|
+
* `undefined` when the widget showed no questions.
|
|
96
|
+
*/
|
|
97
|
+
export function answersFromForm(form: FormData): Record<string, string> | undefined {
|
|
98
|
+
if (form.get(QUESTIONS_MARKER) === null) return undefined;
|
|
99
|
+
const answers: Record<string, string> = {};
|
|
100
|
+
for (const [key, value] of form.entries()) {
|
|
101
|
+
if (!key.startsWith(ANSWER_PREFIX) || typeof value !== "string") continue;
|
|
102
|
+
const id = key.slice(ANSWER_PREFIX.length);
|
|
103
|
+
if (!QUESTION_ID.test(id)) continue;
|
|
104
|
+
answers[id] = value.trim();
|
|
105
|
+
}
|
|
106
|
+
return answers;
|
|
107
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server side of the waiver return page and door signing (EV-FR-12, SX-FR-04; esign-intent/1 §8.2,
|
|
3
|
+
* §8.3). Tokens arrive in POST bodies only (moved from the URL fragment by `src/client/receipt.ts`)
|
|
4
|
+
* and go to the cores in POST bodies; redirects never carry them.
|
|
5
|
+
*
|
|
6
|
+
* - `forwardReceipt`: POSTs a receipt to the Events core's `signatures/receipt`. When the receipt's
|
|
7
|
+
* `pdfSha256` was still null, the core adds a `lookup` request; with the e-sign core's plugin id
|
|
8
|
+
* configured, this asks e-sign's `public/receipts/lookup` once and forwards the full receipt.
|
|
9
|
+
* (Closed-tab reconciliation on later visits is the suite's job, T066.)
|
|
10
|
+
* - `startDoorSigning`: redeems a staff-issued intent (check-in's signing link) with e-sign's
|
|
11
|
+
* `public/envelopes/from-intent` in-process and sends the browser to e-sign's signing page. When the
|
|
12
|
+
* envelope is already completed (an emailed adult signed, but their return page never posted the
|
|
13
|
+
* receipt: no JavaScript, tab closed), the link's lookup request (Pro mints it with the intent) fetches
|
|
14
|
+
* the receipt from e-sign and records it, so check-in admits them (T119 F5).
|
|
15
|
+
*
|
|
16
|
+
* Web APIs only, no `@semics-tech/shared` (R35).
|
|
17
|
+
*/
|
|
18
|
+
import { callPublicRoute, type PublicRouteContext } from "./call";
|
|
19
|
+
import { BOOKING_PATH } from "./checkout-form";
|
|
20
|
+
|
|
21
|
+
export interface ReceiptOptions {
|
|
22
|
+
/** The Events core's plugin id (defaults to the slug). */
|
|
23
|
+
pluginId?: string;
|
|
24
|
+
/** The e-sign core's plugin id, when e-sign runs on this site (`eventsIntegration({ esignPluginId })`). */
|
|
25
|
+
esignPluginId?: string | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `payloadB64 "." signature` (§4), within a type's maximum length. */
|
|
29
|
+
const tokenOk = (value: unknown, max: number): value is string =>
|
|
30
|
+
typeof value === "string" && value.length <= max && /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]{43}$/.test(value);
|
|
31
|
+
|
|
32
|
+
const BOOKING_REF = /^[0-9A-HJKMNP-TV-Z]{10}$/;
|
|
33
|
+
const RECEIPT_MAX = 2048;
|
|
34
|
+
const INTENT_MAX = 8192;
|
|
35
|
+
|
|
36
|
+
/** The notice the booking page shows for `?waiver=`. */
|
|
37
|
+
export type WaiverNotice = "signed" | "declined" | "voided" | "expired" | "failed";
|
|
38
|
+
|
|
39
|
+
interface ReceiptAnswer {
|
|
40
|
+
status?: string;
|
|
41
|
+
signature?: { status?: string };
|
|
42
|
+
lookup?: unknown;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function bookingLocation(ref: string, notice: WaiverNotice): string {
|
|
46
|
+
const query = new URLSearchParams();
|
|
47
|
+
if (BOOKING_REF.test(ref)) query.set("ref", ref);
|
|
48
|
+
query.set("waiver", notice);
|
|
49
|
+
return `${BOOKING_PATH}?${query}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const post = <T>(ctx: PublicRouteContext, pluginId: string | undefined, path: string, body: unknown) =>
|
|
53
|
+
callPublicRoute<T>(ctx, { method: "POST", path, body, ...(pluginId ? { pluginId } : {}) });
|
|
54
|
+
|
|
55
|
+
/** Forward a receipt from the return page; answers with where to send the browser. */
|
|
56
|
+
export async function forwardReceipt(
|
|
57
|
+
ctx: PublicRouteContext,
|
|
58
|
+
input: { ref: string; receipt: unknown },
|
|
59
|
+
options: ReceiptOptions,
|
|
60
|
+
): Promise<{ location: string }> {
|
|
61
|
+
if (!tokenOk(input.receipt, RECEIPT_MAX)) return { location: bookingLocation(input.ref, "failed") };
|
|
62
|
+
const result = await post<ReceiptAnswer>(ctx, options.pluginId, "signatures/receipt", { receipt: input.receipt });
|
|
63
|
+
if (!result.ok) return { location: bookingLocation(input.ref, "failed") };
|
|
64
|
+
const answer = result.data;
|
|
65
|
+
if (typeof answer.lookup === "string" && options.esignPluginId) {
|
|
66
|
+
const lookup = await post<{ state?: string; receipt?: unknown }>(
|
|
67
|
+
ctx,
|
|
68
|
+
options.esignPluginId,
|
|
69
|
+
"public/receipts/lookup",
|
|
70
|
+
{
|
|
71
|
+
request: answer.lookup,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
if (lookup.ok && lookup.data.state === "terminal" && tokenOk(lookup.data.receipt, RECEIPT_MAX)) {
|
|
75
|
+
await post(ctx, options.pluginId, "signatures/receipt", { receipt: lookup.data.receipt });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const status = answer.signature?.status;
|
|
79
|
+
const notice: WaiverNotice =
|
|
80
|
+
status === "completed"
|
|
81
|
+
? "signed"
|
|
82
|
+
: status === "declined" || status === "voided" || status === "expired"
|
|
83
|
+
? status
|
|
84
|
+
: "failed";
|
|
85
|
+
return { location: bookingLocation(input.ref, notice) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The signing page e-sign's integration injects (`/sign/[token]`). */
|
|
89
|
+
export const SIGN_PATH = "/sign/";
|
|
90
|
+
|
|
91
|
+
/** Records a completed envelope's receipt, fetched with the door link's lookup request. */
|
|
92
|
+
async function recordSigned(ctx: PublicRouteContext, lookup: unknown, options: ReceiptOptions): Promise<boolean> {
|
|
93
|
+
if (!options.esignPluginId || !tokenOk(lookup, RECEIPT_MAX)) return false;
|
|
94
|
+
const found = await post<{ state?: string; receipt?: unknown }>(
|
|
95
|
+
ctx,
|
|
96
|
+
options.esignPluginId,
|
|
97
|
+
"public/receipts/lookup",
|
|
98
|
+
{
|
|
99
|
+
request: lookup,
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
if (!found.ok || found.data.state !== "terminal" || !tokenOk(found.data.receipt, RECEIPT_MAX)) return false;
|
|
103
|
+
const stored = await post<ReceiptAnswer>(ctx, options.pluginId, "signatures/receipt", {
|
|
104
|
+
receipt: found.data.receipt,
|
|
105
|
+
});
|
|
106
|
+
return stored.ok && stored.data.signature?.status === "completed";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Redeem a door-signing intent with e-sign and go to its signing page; `{ signed: true }` when the
|
|
111
|
+
* waiver was already signed and is now on record.
|
|
112
|
+
*/
|
|
113
|
+
export async function startDoorSigning(
|
|
114
|
+
ctx: PublicRouteContext,
|
|
115
|
+
intent: unknown,
|
|
116
|
+
options: ReceiptOptions,
|
|
117
|
+
lookup?: unknown,
|
|
118
|
+
): Promise<{ location: string } | { signed: true } | { error: string }> {
|
|
119
|
+
if (!options.esignPluginId) return { error: "ESIGN_NOT_CONFIGURED" };
|
|
120
|
+
if (!tokenOk(intent, INTENT_MAX)) return { error: "INTENT_INVALID" };
|
|
121
|
+
const result = await post<{ status?: unknown; signingToken?: unknown }>(
|
|
122
|
+
ctx,
|
|
123
|
+
options.esignPluginId,
|
|
124
|
+
"public/envelopes/from-intent",
|
|
125
|
+
{ intent },
|
|
126
|
+
);
|
|
127
|
+
if (!result.ok) return { error: result.error.code };
|
|
128
|
+
const token = result.data.signingToken;
|
|
129
|
+
if (typeof token === "string" && /^[A-Za-z0-9_-]{1,256}$/.test(token)) return { location: `${SIGN_PATH}${token}` };
|
|
130
|
+
if (result.data.status === "completed" && (await recordSigned(ctx, lookup, options))) return { signed: true };
|
|
131
|
+
return { error: "SIGNING_UNAVAILABLE" };
|
|
132
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The waitlist in the booking widget (EV-FR-16, EV-US-6, T081). A sold-out event whose core says it
|
|
3
|
+
* takes a waitlist (`public/event` answers `waitlist: true`) shows a "Join the waitlist" form that posts
|
|
4
|
+
* to the injected `POST /_events/waitlist` (no JavaScript needed). It refuses cross-site posts, asks
|
|
5
|
+
* `public/waitlist/join` and goes back to the widget with an outcome code only, never what was typed.
|
|
6
|
+
* Offers arrive by email; the booking page claims them (`claimOfferLink` in `./checkout-form`).
|
|
7
|
+
*/
|
|
8
|
+
import { type MessageKey, t } from "../../i18n/index";
|
|
9
|
+
import { callPublicRoute } from "./call";
|
|
10
|
+
import { type CheckoutEndpointContext, crossSiteResponse, isCrossSiteRequest } from "./checkout-form";
|
|
11
|
+
import { anchorId, safeReturnTo, WIDGET_PARAMS } from "./widget";
|
|
12
|
+
|
|
13
|
+
export const WAITLIST_PATH = "/_events/waitlist";
|
|
14
|
+
const PARAM = "waitlist";
|
|
15
|
+
|
|
16
|
+
const OUTCOMES = ["joined", "BAD_REQUEST", "RATE_LIMITED", "TURNSTILE_FAILED", "NOT_FOUND", "INTERNAL"] as const;
|
|
17
|
+
|
|
18
|
+
/** What the widget says after a join, in English (the 1.0 API); anything else reads as `INTERNAL`. */
|
|
19
|
+
export const WAITLIST_MESSAGES: Record<string, string> = Object.fromEntries(
|
|
20
|
+
OUTCOMES.map((code) => [code, t("en", `waitlist.${code}`)]),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
/** A join's outcome in the locale's language; an unknown one is `INTERNAL`'s. */
|
|
24
|
+
export const waitlistMessage = (code: string, locale?: string) =>
|
|
25
|
+
t(locale, `waitlist.${(OUTCOMES as readonly string[]).includes(code) ? code : "INTERNAL"}` as MessageKey);
|
|
26
|
+
|
|
27
|
+
/** The outcome a widget shows after a no-JS round trip; only for the widget of `eventId`. */
|
|
28
|
+
export function readWaitlistState(url: URL, eventId: string): string | undefined {
|
|
29
|
+
const raw = url.searchParams.get(PARAM);
|
|
30
|
+
if (!raw || url.searchParams.get(WIDGET_PARAMS.event) !== eventId) return undefined;
|
|
31
|
+
return Object.hasOwn(WAITLIST_MESSAGES, raw) ? raw : "INTERNAL";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const text = (form: FormData, key: string) => {
|
|
35
|
+
const value = form.get(key);
|
|
36
|
+
return typeof value === "string" ? value.trim() : "";
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** `POST /_events/waitlist`: refuse cross-site posts, join, back to the widget (303). */
|
|
40
|
+
export async function handleWaitlistRequest(
|
|
41
|
+
{ request, url, locals, redirect }: CheckoutEndpointContext,
|
|
42
|
+
options: { pluginId?: string },
|
|
43
|
+
): Promise<Response> {
|
|
44
|
+
if (isCrossSiteRequest(request, url)) return crossSiteResponse();
|
|
45
|
+
const form = await request.formData().catch(() => new FormData());
|
|
46
|
+
const eventId = text(form, "eventId");
|
|
47
|
+
// Rebuilt from its path, so the Location never leaves the site (as `/_events/promo`).
|
|
48
|
+
const back = new URL(safeReturnTo(text(form, "returnTo"), url.origin) ?? "/", "http://x");
|
|
49
|
+
const occurrenceId = text(form, "occurrenceId");
|
|
50
|
+
const ticketTypeId = text(form, "ticketTypeId");
|
|
51
|
+
const turnstileToken = text(form, "cf-turnstile-response");
|
|
52
|
+
const joined = await callPublicRoute(
|
|
53
|
+
{ locals, url, request },
|
|
54
|
+
{
|
|
55
|
+
method: "POST",
|
|
56
|
+
path: "public/waitlist/join",
|
|
57
|
+
pluginId: options.pluginId,
|
|
58
|
+
body: {
|
|
59
|
+
eventId,
|
|
60
|
+
...(occurrenceId ? { occurrenceId } : {}),
|
|
61
|
+
...(ticketTypeId ? { ticketTypeId } : {}),
|
|
62
|
+
name: text(form, "name"),
|
|
63
|
+
email: text(form, "email"),
|
|
64
|
+
qty: Number.parseInt(text(form, "qty"), 10) || 1,
|
|
65
|
+
...(turnstileToken ? { turnstileToken } : {}),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
back.searchParams.set(PARAM, joined.ok ? "joined" : joined.error.code);
|
|
70
|
+
back.searchParams.set(WIDGET_PARAMS.event, eventId);
|
|
71
|
+
return redirect(`${back.pathname}${back.search}#${anchorId(eventId)}`, 303);
|
|
72
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for the booking widget, status page, pass and calendar link (T055). Pure, so
|
|
3
|
+
* the components stay thin. `src/client/booking.ts` mirrors `validateCheckoutFields` (a test keeps
|
|
4
|
+
* them in step) because the client script ships without imports.
|
|
5
|
+
*/
|
|
6
|
+
import { type MessageKey, t } from "../../i18n/index";
|
|
7
|
+
|
|
8
|
+
/** What `GET public/event` answers for one event (T046 implements the route to this shape). */
|
|
9
|
+
export interface PublicEvent {
|
|
10
|
+
eventId: string;
|
|
11
|
+
title?: string;
|
|
12
|
+
status: "draft" | "on_sale" | "paused" | "closed" | "cancelled";
|
|
13
|
+
currency: string;
|
|
14
|
+
/** IANA zone the event's dates are shown in (the entry's `timezone` field). */
|
|
15
|
+
timezone?: string;
|
|
16
|
+
/** The event's own start, end and venue (the mapped entry fields), when the core has them. */
|
|
17
|
+
startsAt?: string;
|
|
18
|
+
endsAt?: string | null;
|
|
19
|
+
venue?: string | null;
|
|
20
|
+
ticketTypes: Array<{
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
priceMinor: number;
|
|
24
|
+
/** Seats left; `null` when not shown. */
|
|
25
|
+
available?: number | null;
|
|
26
|
+
minPerOrder?: number | null;
|
|
27
|
+
maxPerOrder?: number | null;
|
|
28
|
+
}>;
|
|
29
|
+
/** Upcoming open dates, earliest first: up to 200 from the asked `from` (T130). */
|
|
30
|
+
occurrences?: Array<{ id: string; startsAt: string; endsAt?: string | null; available?: number | null }>;
|
|
31
|
+
/** More dates follow the ones sent: ask again `from` a later day (T130). */
|
|
32
|
+
more?: boolean;
|
|
33
|
+
/** The event sells slots of a schedule (T129): the form uses the day-then-time picker (T130). */
|
|
34
|
+
slots?: boolean;
|
|
35
|
+
/** Public Turnstile site key, when the site owner configured one. */
|
|
36
|
+
turnstileSiteKey?: string;
|
|
37
|
+
/** True when the event requires a signed waiver (T066): the booking form then asks for attendees. */
|
|
38
|
+
waiver?: boolean;
|
|
39
|
+
/** True when the core takes promo codes (Events Pro, T078): the widget then shows a code field. */
|
|
40
|
+
promoCodes?: boolean;
|
|
41
|
+
/** True when a sold-out event takes a waitlist (T081): the widget then shows a "Join the waitlist" form. */
|
|
42
|
+
waitlist?: boolean;
|
|
43
|
+
/** Custom attendee questions (Events Pro, T080): the buyer answers them once per booking. */
|
|
44
|
+
questions?: PublicQuestion[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Ticket type names by id from a `public/event` answer, for `<BookingStatus>` (T134): the booking's
|
|
49
|
+
* lines carry only ids (UUIDs for types made in the editor). A hidden type is not listed there.
|
|
50
|
+
*/
|
|
51
|
+
export function ticketNames(event: Pick<PublicEvent, "ticketTypes"> | undefined): Record<string, string> {
|
|
52
|
+
return Object.fromEntries((event?.ticketTypes ?? []).map((t) => [t.id, t.name]));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One custom question as `public/event` lists it (EV-FR-15). */
|
|
56
|
+
export interface PublicQuestion {
|
|
57
|
+
id: string;
|
|
58
|
+
label: string;
|
|
59
|
+
type: "text" | "select" | "checkbox";
|
|
60
|
+
required: boolean;
|
|
61
|
+
/** The choices of a `select` question. */
|
|
62
|
+
options?: string[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** What `POST public/booking` answers (T049), plus the optional fields later tasks add. */
|
|
66
|
+
export interface BookingView {
|
|
67
|
+
bookingRef: string;
|
|
68
|
+
status: string;
|
|
69
|
+
eventId?: string;
|
|
70
|
+
lines: Array<{ ticketTypeId: string; qty: number }>;
|
|
71
|
+
totals: { totalMinor: number; currency: string };
|
|
72
|
+
holdExpiresAt?: string;
|
|
73
|
+
/** `qr`: Events Pro's QR matrix of `code` (T082), drawn by `<TicketPass>`; Free sends none. */
|
|
74
|
+
attendees: Array<{ name: string; ticketTypeId?: string; status: string; code?: string; qr?: unknown }>;
|
|
75
|
+
canCancel: boolean;
|
|
76
|
+
cancelBy?: string;
|
|
77
|
+
/** A short-lived signed query for `public/booking/ics`, issued by the core (T050). */
|
|
78
|
+
calendar?: { query?: string };
|
|
79
|
+
/**
|
|
80
|
+
* While a waiver booking waits for signatures (T066): the refs still unsigned, and with Pro a signed
|
|
81
|
+
* receipt-lookup request for each, which the suite's reconciliation sends to e-sign.
|
|
82
|
+
*/
|
|
83
|
+
waiver?: { refs: string[]; lookups?: Record<string, string> };
|
|
84
|
+
/**
|
|
85
|
+
* Events Pro (T133): a waiver booking's attendee signing list. Each attendee then also carries
|
|
86
|
+
* `seat`, `signing`, `signs`, `email` and `minor` (`SigningAttendee` in `lib/attendees.ts`);
|
|
87
|
+
* `editable` says whether names and emails may still be changed, until `editUntil`.
|
|
88
|
+
*/
|
|
89
|
+
signingList?: { editable: boolean; editUntil?: string };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A custom question's field is `q:<question id>` (T080). */
|
|
93
|
+
export type QuestionField = `q:${string}`;
|
|
94
|
+
export type WidgetField = "occurrence" | "promo" | "tickets" | "name" | "email" | QuestionField;
|
|
95
|
+
|
|
96
|
+
/** Question ids safe in HTML ids, field names and the widget's error URL. */
|
|
97
|
+
export const QUESTION_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
98
|
+
|
|
99
|
+
/** In form order: the first one is where focus goes. */
|
|
100
|
+
export const WIDGET_FIELDS = ["occurrence", "promo", "tickets", "name", "email"] as const;
|
|
101
|
+
|
|
102
|
+
export const WIDGET_PARAMS = { error: "booking_error", fields: "booking_fields", event: "booking_event" } as const;
|
|
103
|
+
|
|
104
|
+
/** The key reserve accepts (`src/domain/checkout.ts`). */
|
|
105
|
+
export const IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9_-]{32,128}$/;
|
|
106
|
+
|
|
107
|
+
const ERROR_CODES = [
|
|
108
|
+
"BAD_REQUEST",
|
|
109
|
+
"SOLD_OUT",
|
|
110
|
+
"SALES_CLOSED",
|
|
111
|
+
"RATE_LIMITED",
|
|
112
|
+
"TURNSTILE_FAILED",
|
|
113
|
+
"HOLD_EXPIRED",
|
|
114
|
+
"NOT_FOUND",
|
|
115
|
+
"PROMO_INVALID",
|
|
116
|
+
"PROMO_EXHAUSTED",
|
|
117
|
+
"PROMO_ORDER_TOO_LARGE",
|
|
118
|
+
"PROMO_UNAVAILABLE",
|
|
119
|
+
"INTERNAL",
|
|
120
|
+
] as const;
|
|
121
|
+
|
|
122
|
+
/** A field's message in the locale's language (T102). */
|
|
123
|
+
export const fieldMessage = (field: (typeof WIDGET_FIELDS)[number], locale?: string) =>
|
|
124
|
+
t(locale, `widget.field.${field}`);
|
|
125
|
+
|
|
126
|
+
/** English field messages, by field (the 1.0 API; components use `fieldMessage`). */
|
|
127
|
+
export const FIELD_MESSAGES = Object.fromEntries(WIDGET_FIELDS.map((f) => [f, fieldMessage(f, "en")])) as Record<
|
|
128
|
+
(typeof WIDGET_FIELDS)[number],
|
|
129
|
+
string
|
|
130
|
+
>;
|
|
131
|
+
|
|
132
|
+
/** English refusal messages by code: the codes the widget knows (anything else reads as `INTERNAL`). */
|
|
133
|
+
export const ERROR_MESSAGES: Record<string, string> = Object.fromEntries(
|
|
134
|
+
ERROR_CODES.map((code) => [code, t("en", `widget.error.${code}`)]),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
/** A refusal's message in the locale's language; an unknown code gets `INTERNAL`'s. */
|
|
138
|
+
export const errorMessage = (code: string, locale?: string) =>
|
|
139
|
+
t(locale, `widget.error.${(ERROR_CODES as readonly string[]).includes(code) ? code : "INTERNAL"}` as MessageKey);
|
|
140
|
+
|
|
141
|
+
/** A refusal of an order or check with a code: Pro's licence codes (it lapsed) mean the code can't be used (T120). */
|
|
142
|
+
export const promoRefusal = (code: string) => (code.startsWith("LICENCE_") ? "PROMO_UNAVAILABLE" : code);
|
|
143
|
+
|
|
144
|
+
/** 32 random bytes as 43 unpadded base64url characters: a fresh key for each checkout attempt. */
|
|
145
|
+
export function newIdempotencyKey(): string {
|
|
146
|
+
const bytes = crypto.getRandomValues(new Uint8Array(32));
|
|
147
|
+
return btoa(String.fromCharCode(...bytes))
|
|
148
|
+
.replaceAll("+", "-")
|
|
149
|
+
.replaceAll("/", "_")
|
|
150
|
+
.replaceAll("=", "");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The widget's element id, safe in HTML ids, CSS selectors and URL fragments. */
|
|
154
|
+
export function anchorId(eventId: string): string {
|
|
155
|
+
return `semics-booking-${eventId.replace(/[^A-Za-z0-9_-]/g, "_")}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A same-site path to send the buyer back to, or `undefined`. Refuses absolute and
|
|
160
|
+
* protocol-relative URLs (no open redirect) and the checkout endpoint itself, and drops earlier
|
|
161
|
+
* widget error parameters and the fragment.
|
|
162
|
+
*/
|
|
163
|
+
export function safeReturnTo(raw: string, origin: string): string | undefined {
|
|
164
|
+
if (!raw.startsWith("/") || raw.startsWith("//") || raw.startsWith("/\\")) return undefined;
|
|
165
|
+
let url: URL;
|
|
166
|
+
try {
|
|
167
|
+
url = new URL(raw, origin);
|
|
168
|
+
} catch {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
// `/.//x` and `/%2e//x` resolve to `//x`: as a Location that is another host (T119 F11).
|
|
172
|
+
if (url.origin !== origin || url.pathname.startsWith("//") || url.pathname.startsWith("/_events/")) return undefined;
|
|
173
|
+
for (const name of Object.values(WIDGET_PARAMS)) url.searchParams.delete(name);
|
|
174
|
+
return `${url.pathname}${url.search}`;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Where a refused checkout goes: back to the widget with a code and the fields to fix. No values. */
|
|
178
|
+
export function widgetErrorLocation(
|
|
179
|
+
returnTo: string,
|
|
180
|
+
eventId: string,
|
|
181
|
+
code: string,
|
|
182
|
+
fields: readonly WidgetField[],
|
|
183
|
+
): string {
|
|
184
|
+
const url = new URL(returnTo, "http://x");
|
|
185
|
+
url.searchParams.set(WIDGET_PARAMS.error, code);
|
|
186
|
+
if (fields.length > 0) url.searchParams.set(WIDGET_PARAMS.fields, fields.join(","));
|
|
187
|
+
url.searchParams.set(WIDGET_PARAMS.event, eventId);
|
|
188
|
+
return `${url.pathname}${url.search}#${anchorId(eventId)}`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The error a widget shows after a no-JS round trip; only for the widget of `eventId`. */
|
|
192
|
+
export function readWidgetState(url: URL, eventId: string): { code?: string; fields: WidgetField[] } {
|
|
193
|
+
const raw = url.searchParams.get(WIDGET_PARAMS.error);
|
|
194
|
+
if (!raw || url.searchParams.get(WIDGET_PARAMS.event) !== eventId) return { fields: [] };
|
|
195
|
+
const code = Object.hasOwn(ERROR_MESSAGES, raw) ? raw : "INTERNAL";
|
|
196
|
+
const asked = new Set((url.searchParams.get(WIDGET_PARAMS.fields) ?? "").split(","));
|
|
197
|
+
const questions = [...asked].filter((f): f is QuestionField => f.startsWith("q:") && QUESTION_ID.test(f.slice(2)));
|
|
198
|
+
return { code, fields: [...WIDGET_FIELDS.filter((f) => asked.has(f)), ...questions] };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface CheckoutFieldValues {
|
|
202
|
+
name: string;
|
|
203
|
+
email: string;
|
|
204
|
+
/** Ticket lines with a quantity above zero. */
|
|
205
|
+
lines: number;
|
|
206
|
+
needsOccurrence: boolean;
|
|
207
|
+
occurrenceId: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
211
|
+
|
|
212
|
+
/** The fields to fix before checkout is worth a call; reserve's own validation is the authority. */
|
|
213
|
+
export function validateCheckoutFields(v: CheckoutFieldValues): WidgetField[] {
|
|
214
|
+
const name = v.name.trim();
|
|
215
|
+
const email = v.email.trim();
|
|
216
|
+
const bad: WidgetField[] = [];
|
|
217
|
+
if (v.needsOccurrence && !v.occurrenceId) bad.push("occurrence");
|
|
218
|
+
if (v.lines < 1) bad.push("tickets");
|
|
219
|
+
if (name.length < 1 || name.length > 200) bad.push("name");
|
|
220
|
+
if (email.length > 254 || !EMAIL.test(email)) bad.push("email");
|
|
221
|
+
return bad;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const ISSUE_FIELDS: Array<[RegExp, WidgetField]> = [
|
|
225
|
+
[/^occurrenceId/, "occurrence"],
|
|
226
|
+
[/^lines/, "tickets"],
|
|
227
|
+
[/^buyer\.name/, "name"],
|
|
228
|
+
[/^buyer\.email/, "email"],
|
|
229
|
+
];
|
|
230
|
+
|
|
231
|
+
/** Reserve's validation issues (`{ path, code }`) as widget fields, each once. */
|
|
232
|
+
export function fieldsFromIssues(issues: ReadonlyArray<{ path?: unknown; code?: unknown }> | undefined): WidgetField[] {
|
|
233
|
+
const found = new Set<WidgetField>();
|
|
234
|
+
for (const issue of issues ?? []) {
|
|
235
|
+
const path = typeof issue.path === "string" ? issue.path : "";
|
|
236
|
+
const hit = ISSUE_FIELDS.find(([pattern]) => pattern.test(path));
|
|
237
|
+
const question = /^answers\.([^.]+)/.exec(path)?.[1];
|
|
238
|
+
if (hit) found.add(hit[1]);
|
|
239
|
+
else if (question && QUESTION_ID.test(question)) found.add(`q:${question}`);
|
|
240
|
+
}
|
|
241
|
+
return [...found];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const TOKEN_LIKE = /^(t|.*token.*|.*receipt.*|.*secret.*|.*otp.*)$/i;
|
|
245
|
+
|
|
246
|
+
const routeBase = (pluginId: string) => `/_emdash/api/plugins/${encodeURIComponent(pluginId)}`;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The booking's calendar file link, from the signed query the core issued (`signLink`). The UI
|
|
250
|
+
* holds no secret and never signs: it only refuses a query that is unsigned, expired or carries a
|
|
251
|
+
* token, so an access token can never end up in a calendar URL.
|
|
252
|
+
*/
|
|
253
|
+
export function bookingCalendarHref(input: {
|
|
254
|
+
pluginId: string;
|
|
255
|
+
query: string | undefined;
|
|
256
|
+
now: number;
|
|
257
|
+
}): string | undefined {
|
|
258
|
+
if (!input.query) return undefined;
|
|
259
|
+
const query = new URLSearchParams(input.query);
|
|
260
|
+
const exp = query.get("exp") ?? "";
|
|
261
|
+
if (!query.get("sig") || !/^\d{1,15}$/.test(exp) || Number(exp) * 1000 <= input.now) return undefined;
|
|
262
|
+
for (const name of query.keys()) if (TOKEN_LIKE.test(name)) return undefined;
|
|
263
|
+
return `${routeBase(input.pluginId)}/public/booking/ics?${query}`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** The public event calendar file (no booking data, so no signature). */
|
|
267
|
+
export function eventCalendarHref(pluginId: string, eventId: string): string {
|
|
268
|
+
return `${routeBase(pluginId)}/public/event/ics?${new URLSearchParams({ eventId })}`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function formatMoney(minor: number, currency: string, locale = "en-GB"): string {
|
|
272
|
+
if (minor === 0) return t(locale, "money.free");
|
|
273
|
+
try {
|
|
274
|
+
return new Intl.NumberFormat(locale, { style: "currency", currency }).format(minor / 100);
|
|
275
|
+
} catch {
|
|
276
|
+
return `${(minor / 100).toFixed(2)} ${currency}`;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const STATUSES = [
|
|
281
|
+
"draft",
|
|
282
|
+
"held",
|
|
283
|
+
"awaiting_signature",
|
|
284
|
+
"awaiting_payment",
|
|
285
|
+
"awaiting_approval",
|
|
286
|
+
"confirmed",
|
|
287
|
+
"expired",
|
|
288
|
+
"cancelled",
|
|
289
|
+
"refunded",
|
|
290
|
+
"refunded_unavailable",
|
|
291
|
+
"declined",
|
|
292
|
+
] as const;
|
|
293
|
+
|
|
294
|
+
/** A booking status for people, in the locale's language; an unknown status is shown as it is. */
|
|
295
|
+
export const statusLabel = (status: string, locale?: string) =>
|
|
296
|
+
(STATUSES as readonly string[]).includes(status) ? t(locale, `status.${status}` as MessageKey) : status;
|
|
297
|
+
|
|
298
|
+
/** English status labels (the 1.0 API; components use `statusLabel`). */
|
|
299
|
+
export const STATUS_LABELS: Record<string, string> = Object.fromEntries(STATUSES.map((s) => [s, statusLabel(s, "en")]));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Injected at `/_events/promo` by the integration: the booking widget's "Apply" button (a plain form
|
|
2
|
+
// post, no JavaScript needed). Cross-site posts get a 403 before anything runs (`isCrossSiteRequest`).
|
|
3
|
+
import { pluginId } from "virtual:semics-events/config";
|
|
4
|
+
|
|
5
|
+
import { handlePromoRequest, type PromoEndpointContext } from "./lib/promo";
|
|
6
|
+
|
|
7
|
+
export const prerender = false;
|
|
8
|
+
|
|
9
|
+
export function POST(ctx: PromoEndpointContext): Promise<Response> {
|
|
10
|
+
return handlePromoRequest(ctx, { pluginId });
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Provided by the integration's Vite plugin (`eventsIntegration({ pluginId })`).
|
|
2
|
+
declare module "virtual:semics-events/config" {
|
|
3
|
+
export const pluginId: string;
|
|
4
|
+
/** The e-sign core's plugin id, or `null` when e-sign is not configured (`eventsIntegration({ esignPluginId })`). */
|
|
5
|
+
export const esignPluginId: string | null;
|
|
6
|
+
/**
|
|
7
|
+
* The suite's reconciliation of a booking that waits for signatures (T066), or `null` without the suite
|
|
8
|
+
* (`eventsIntegration({ reconcile })`). Forwards the receipts e-sign already has; resolves with how many.
|
|
9
|
+
*/
|
|
10
|
+
export const reconcile:
|
|
11
|
+
| ((
|
|
12
|
+
ctx: import("./lib/call").PublicRouteContext,
|
|
13
|
+
input: { booking: import("./lib/widget").BookingView; pluginId: string; esignPluginId: string },
|
|
14
|
+
) => Promise<{ forwarded: number }>)
|
|
15
|
+
| null;
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Injected at `/_events/waitlist` by the integration: the booking widget's "Join the waitlist" form (a
|
|
2
|
+
// plain form post, no JavaScript needed). Cross-site posts get a 403 before anything runs.
|
|
3
|
+
import { pluginId } from "virtual:semics-events/config";
|
|
4
|
+
|
|
5
|
+
import type { CheckoutEndpointContext } from "./lib/checkout-form";
|
|
6
|
+
import { handleWaitlistRequest } from "./lib/waitlist";
|
|
7
|
+
|
|
8
|
+
export const prerender = false;
|
|
9
|
+
|
|
10
|
+
export function POST(ctx: CheckoutEndpointContext): Promise<Response> {
|
|
11
|
+
return handleWaitlistRequest(ctx, { pluginId });
|
|
12
|
+
}
|