feat(tickets): ticket page /t/<code> with public and staff views, proxies and labels

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-04 17:38:07 +08:00
1 parent 031ec8449e
commit 2b8b3d92bc
20 files changed
+698 -7

No files matched your search

@@ -0,0 +1,11 @@
import { NextResponse, type NextRequest } from "next/server";
import { forwardPublic } from "@/app/lib/server/publicProxy";
type Ctx = { params: Promise<{ token: string; ticketId: string }> };
// add-event-tickets: the buyer names a ticket's attendee from the order page.
export async function POST(req: NextRequest, { params }: Ctx) {
const { token, ticketId } = await params;
if (!/^\d{1,19}$/.test(ticketId)) return NextResponse.json({ error: "Not found" }, { status: 404 });
return forwardPublic(req, `/api/public/orders/${encodeURIComponent(token)}/tickets/${encodeURIComponent(ticketId)}/attendee`, "POST");
}
@@ -0,0 +1,59 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), callMobileApiBinary: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi, callMobileApiBinary: mocks.callMobileApiBinary }));
import { GET as ticketGet } from "./tickets/[token]/route";
import { POST as consentPost } from "./tickets/[token]/consent/route";
import { GET as qrGet } from "./tickets/[token]/qr.png/route";
import { POST as attendeePost } from "./orders/[token]/tickets/[ticketId]/attendee/route";
type Handler = (req: never, ctx: { params: Promise<Record<string, string>> }) => Promise<Response>;
const xff = { "x-forwarded-for": "6.6.6.6, 203.0.113.7" };
const call = (h: unknown, params: Record<string, string>, init: RequestInit = {}) =>
(h as Handler)(new Request("http://localhost/x", init) as never, { params: Promise.resolve(params) });
beforeEach(() => {
mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { state: "VALID" } });
mocks.callMobileApiBinary.mockReset().mockResolvedValue({ ok: true, data: { body: new Uint8Array([137, 80, 78, 71]).buffer, contentType: "image/png" } });
});
// add-event-tickets: the ticket page, its consent, its QR PNG and naming an attendee, signed out, with only the last X-Forwarded-For hop.
describe("public ticket code proxies", () => {
it("GET a ticket by its code", async () => {
const res = await call(ticketGet, { token: "c/1" }, { headers: xff });
expect(await res.json()).toEqual({ state: "VALID" });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/tickets/c%2F1", { headers: { "X-Forwarded-For": "203.0.113.7" } });
});
it("POST a consent", async () => {
await call(consentPost, { token: "c1" }, { method: "POST", body: '{"consent":true}', headers: xff });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/tickets/c1/consent",
{ method: "POST", body: { consent: true }, headers: { "X-Forwarded-For": "203.0.113.7" } });
});
it("POST an attendee to an order's ticket", async () => {
await call(attendeePost, { token: "t/1", ticketId: "7" }, { method: "POST", body: '{"name":"Tan"}', headers: xff });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/orders/t%2F1/tickets/7/attendee",
{ method: "POST", body: { name: "Tan" }, headers: { "X-Forwarded-For": "203.0.113.7" } });
});
it("refuses a ticket id that is not a number without calling upstream", async () => {
const res = await call(attendeePost, { token: "t1", ticketId: "7/x" }, { method: "POST", body: "{}" });
expect(res.status).toBe(404);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("passes the QR PNG bytes and type, private, with the client hop", async () => {
const res = await call(qrGet, { token: "c1" }, { headers: xff });
expect(res.headers.get("Content-Type")).toBe("image/png");
expect(res.headers.get("Cache-Control")).toBe("private, max-age=300");
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([137, 80, 78, 71]));
expect(mocks.callMobileApiBinary).toHaveBeenCalledWith("/api/public/tickets/c1/qr.png", { headers: { "X-Forwarded-For": "203.0.113.7" } });
});
it("answers 404 for a QR that is not there", async () => {
mocks.callMobileApiBinary.mockResolvedValue({ ok: false, status: 404, error: "x" });
expect((await call(qrGet, { token: "c1" })).status).toBe(404);
});
});
@@ -0,0 +1,4 @@
import { publicRoute } from "@/app/lib/server/publicProxy";
// add-event-tickets: the attendee's "keep me updated" on their ticket page.
export const POST = publicRoute("tickets", "/consent", "POST");
@@ -0,0 +1,19 @@
import { NextResponse, type NextRequest } from "next/server";
import { callMobileApiBinary } from "@/app/lib/server/mobileApi";
import { clientIp } from "@/app/lib/server/clientContext";
import type { TokenCtx } from "@/app/lib/server/publicProxy";
// add-event-tickets: a valid ticket's QR as a PNG, for the page and the emails. Rate-limited per client, so the client hop goes along.
export async function GET(req: NextRequest, { params }: TokenCtx) {
const xff = clientIp(req);
const result = await callMobileApiBinary(`/api/public/tickets/${encodeURIComponent((await params).token)}/qr.png`,
{ headers: xff ? { "X-Forwarded-For": xff } : {} });
if (!result.ok) return new NextResponse(null, { status: result.status === 404 ? 404 : 502 });
return new NextResponse(result.data.body, {
headers: {
"Content-Type": result.data.contentType ?? "image/png",
"Cache-Control": "private, max-age=300",
"X-Content-Type-Options": "nosniff",
},
});
}
@@ -0,0 +1,4 @@
import { publicRoute } from "@/app/lib/server/publicProxy";
// add-event-tickets: the public ticket page reads its ticket by the code.
export const GET = publicRoute("tickets", "", "GET");
@@ -0,0 +1,16 @@
import { NextResponse, type NextRequest } from "next/server";
import { callMobileApi } from "@/app/lib/server/mobileApi";
import { getToken } from "@/app/lib/server/session";
import { errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
type Ctx = { params: Promise<{ code: string }> };
// add-event-tickets: door check-in from the staff view.
export async function POST(req: NextRequest, { params }: Ctx) {
const token = await getToken();
if (!token) return unauthorized();
const result = await callMobileApi<unknown>(`/api/admin/tickets/${encodeURIComponent((await params).code)}/checkin`,
{ method: "POST", body: await req.json().catch(() => ({})), token });
if (!result.ok) return errorResponse(result);
return NextResponse.json(result.data ?? {});
}
@@ -0,0 +1,15 @@
import { NextResponse, type NextRequest } from "next/server";
import { callMobileApi } from "@/app/lib/server/mobileApi";
import { getToken } from "@/app/lib/server/session";
import { errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
type Ctx = { params: Promise<{ code: string }> };
// add-event-tickets: the staff view of a scanned ticket. mobile-api checks BO_EVENTS and the event's company.
export async function GET(_req: NextRequest, { params }: Ctx) {
const token = await getToken();
if (!token) return unauthorized();
const result = await callMobileApi<unknown>(`/api/admin/tickets/${encodeURIComponent((await params).code)}`, { token });
if (!result.ok) return errorResponse(result);
return NextResponse.json(result.data ?? {});
}
@@ -0,0 +1,42 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
import { GET } from "./tickets/[code]/route";
import { POST } from "./tickets/[code]/checkin/route";
const req = (init?: RequestInit) => new Request("http://localhost/x", init) as never;
const p = (code: string) => ({ params: Promise.resolve({ code }) });
beforeEach(() => {
mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { state: "VALID" } });
mocks.getToken.mockReset().mockResolvedValue("jwt");
});
// add-event-tickets: the staff ticket view and door check-in go to mobile-api with the session token.
describe("staff ticket proxies", () => {
it("forward the view and the check-in with the token", async () => {
await GET(req(), p("c/1"));
await POST(req({ method: "POST", body: '{"name":"Lim Wei"}' }), p("c1"));
expect(mocks.callMobileApi.mock.calls).toEqual([
["/api/admin/tickets/c%2F1", { token: "jwt" }],
["/api/admin/tickets/c1/checkin", { method: "POST", body: { name: "Lim Wei" }, token: "jwt" }],
]);
});
it("answer 401 without a session and never call upstream", async () => {
mocks.getToken.mockResolvedValue(undefined);
expect((await GET(req(), p("c1"))).status).toBe(401);
expect((await POST(req({ method: "POST", body: "{}" }), p("c1"))).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("pass a refusal with its status and code", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 409, error: "NOT_EVENT_DAY", code: "NOT_EVENT_DAY" });
const res = await POST(req({ method: "POST", body: "{}" }), p("c1"));
expect(res.status).toBe(409);
expect(((await res.json()) as Record<string, unknown>).code).toBe("NOT_EVENT_DAY");
});
});