feat(portal): landing pages list, editor, preview and public page
Pages list and Tiptap editor (ported from babyboo) with image upload on save, the FORM event picker, field errors at their fields, delete confirmation, unsaved-edits guard and a draft preview. The public page /p/<company>/<slug> renders on the server with metadata, sanitize-html, the company's logo and accent, and the sign-up form with UTM tags. Companies get branding; the event lead detail shows the web page and campaign. Labels in English and Chinese. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
ab9d3f4942
commit
bb2c5c5db7
52 files changed
+3527
-22
No files matched your search
@@ -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<{ id: string }> };
|
||||
|
||||
export async function PUT(req: NextRequest, { params }: Ctx) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const { id } = await params;
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const result = await callMobileApi<Record<string, unknown>>(`/api/admin/companies/${encodeURIComponent(id)}/branding`, { method: "PUT", body, token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
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<{ id: string }> };
|
||||
|
||||
export async function GET(_req: NextRequest, { params }: Ctx) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const { id } = await params;
|
||||
const result = await callMobileApi<Record<string, unknown>>(`/api/admin/pages/${encodeURIComponent(id)}`, { token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
|
||||
export async function PUT(req: NextRequest, { params }: Ctx) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const { id } = await params;
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const result = await callMobileApi<Record<string, unknown>>(`/api/admin/pages/${encodeURIComponent(id)}`, { method: "PUT", body, token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
|
||||
export async function DELETE(_req: NextRequest, { params }: Ctx) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const { id } = await params;
|
||||
const result = await callMobileApi<unknown>(`/api/admin/pages/${encodeURIComponent(id)}`, { method: "DELETE", token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
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";
|
||||
|
||||
// Under /images so bodyLimit gives it the upload size (a 2 MB image as base64 JSON).
|
||||
export async function POST(req: NextRequest) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const result = await callMobileApi<{ id: number }>("/api/admin/page-images", { method: "POST", body, token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
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";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const params = new URL(req.url).searchParams;
|
||||
const qs = new URLSearchParams();
|
||||
const q = params.get("q");
|
||||
if (q) qs.set("q", q);
|
||||
const companyId = params.get("companyId");
|
||||
if (companyId && /^\d+$/.test(companyId)) qs.set("companyId", companyId);
|
||||
for (const k of ["page", "size"]) {
|
||||
const v = params.get(k);
|
||||
if (v && /^\d+$/.test(v)) qs.set(k, v);
|
||||
}
|
||||
const result = await callMobileApi<Record<string, unknown>>(`/api/admin/pages?${qs.toString()}`, { token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const result = await callMobileApi<Record<string, unknown>>("/api/admin/pages", { method: "POST", body, token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), callMobileApiBinary: vi.fn(), getToken: vi.fn() }));
|
||||
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi, callMobileApiBinary: mocks.callMobileApiBinary }));
|
||||
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
||||
vi.mock("@/app/lib/server/clientContext", () => ({ clientIp: () => "203.0.113.9" }));
|
||||
|
||||
import * as list from "./route";
|
||||
import * as one from "./[id]/route";
|
||||
import * as images from "./images/route";
|
||||
import * as form from "../public/pages/[company]/[slug]/form/[block]/route";
|
||||
import * as image from "../public/page-images/[id]/route";
|
||||
import * as branding from "../companies/[id]/branding/route";
|
||||
|
||||
type Req = Parameters<typeof list.GET>[0];
|
||||
const req = (url: string, init: RequestInit = {}) => new Request(`http://localhost${url}`, init) as unknown as Req;
|
||||
const ctx = <T,>(p: T) => ({ params: Promise.resolve(p) });
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.getToken.mockResolvedValue("jwt");
|
||||
mocks.callMobileApi.mockResolvedValue({ ok: true, data: {} });
|
||||
});
|
||||
|
||||
describe("admin page routes", () => {
|
||||
it("refuse without a session", async () => {
|
||||
mocks.getToken.mockResolvedValue(null);
|
||||
expect((await list.GET(req("/api/pages"))).status).toBe(401);
|
||||
expect((await one.PUT(req("/api/pages/1", { method: "PUT", body: "{}" }), ctx({ id: "1" }))).status).toBe(401);
|
||||
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("list forwards only the allowed query values", async () => {
|
||||
await list.GET(req("/api/pages?q=a%26b&page=2&size=50&companyId=x&evil=1"));
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/pages?q=a%26b&page=2&size=50", { token: "jwt" });
|
||||
});
|
||||
|
||||
it("encodes the id and passes create, update and delete", async () => {
|
||||
await one.GET(req("/api/pages/x"), ctx({ id: "1/../2" }));
|
||||
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1%2F..%2F2", { token: "jwt" });
|
||||
await one.PUT(req("/api/pages/1", { method: "PUT", body: JSON.stringify({ title: "T" }) }), ctx({ id: "1" }));
|
||||
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1", { method: "PUT", body: { title: "T" }, token: "jwt" });
|
||||
await one.DELETE(req("/api/pages/1", { method: "DELETE" }), ctx({ id: "1" }));
|
||||
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1", { method: "DELETE", token: "jwt" });
|
||||
await list.POST(req("/api/pages", { method: "POST", body: JSON.stringify({ title: "T" }) }));
|
||||
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages", { method: "POST", body: { title: "T" }, token: "jwt" });
|
||||
});
|
||||
|
||||
it("uploads an image and saves branding", async () => {
|
||||
await images.POST(req("/api/pages/images", { method: "POST", body: JSON.stringify({ companyId: 2, data: "aGk=" }) }));
|
||||
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/page-images", { method: "POST", body: { companyId: 2, data: "aGk=" }, token: "jwt" });
|
||||
await branding.PUT(req("/api/companies/2/branding", { method: "PUT", body: JSON.stringify({ accentColor: "#112233" }) }), ctx({ id: "2" }));
|
||||
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/companies/2/branding", { method: "PUT", body: { accentColor: "#112233" }, token: "jwt" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("public page routes", () => {
|
||||
it("form submit is forwarded with no session, encoded, with the visitor's address", async () => {
|
||||
mocks.getToken.mockResolvedValue(null);
|
||||
await form.POST(req("/x", { method: "POST", body: JSON.stringify({ name: "A" }) }), ctx({ company: "INS", slug: "a/b", block: "2" }));
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/pages/INS/a%2Fb/form/2",
|
||||
{ method: "POST", body: { name: "A" }, headers: { "X-Forwarded-For": "203.0.113.9" } });
|
||||
});
|
||||
|
||||
it("image is served with its type and a long cache, and a miss is 404", async () => {
|
||||
mocks.callMobileApiBinary.mockResolvedValueOnce({ ok: true, data: { body: new Uint8Array([1, 2]).buffer, contentType: "image/png", contentDisposition: null } });
|
||||
const res = await image.GET(req("/api/public/page-images/7"), ctx({ id: "7" }));
|
||||
expect(mocks.callMobileApiBinary).toHaveBeenCalledWith("/api/public/page-images/7");
|
||||
expect(res.headers.get("Content-Type")).toBe("image/png");
|
||||
expect(res.headers.get("Cache-Control")).toContain("max-age=31536000");
|
||||
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([1, 2]));
|
||||
|
||||
expect((await image.GET(req("/x"), ctx({ id: "abc" }))).status).toBe(404);
|
||||
mocks.callMobileApiBinary.mockResolvedValueOnce({ ok: false, status: 404, error: "x" });
|
||||
expect((await image.GET(req("/x"), ctx({ id: "8" }))).status).toBe(404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { callMobileApiBinary } from "@/app/lib/server/mobileApi";
|
||||
|
||||
type Ctx = { params: Promise<{ id: string }> };
|
||||
|
||||
// Landing-page images, public by id. An image never changes once uploaded, so it caches for a year.
|
||||
export async function GET(_req: NextRequest, { params }: Ctx) {
|
||||
const { id } = await params;
|
||||
if (!/^\d{1,19}$/.test(id)) return new NextResponse(null, { status: 404 });
|
||||
const result = await callMobileApiBinary(`/api/public/page-images/${encodeURIComponent(id)}`);
|
||||
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 ?? "application/octet-stream",
|
||||
"Cache-Control": "public, max-age=31536000, immutable",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { forwardPublic } from "@/app/lib/server/publicProxy";
|
||||
|
||||
type Ctx = { params: Promise<{ company: string; slug: string; block: string }> };
|
||||
|
||||
// Public (signed-out) landing-page form sign-up; see publicProxy for the X-Forwarded-For rule.
|
||||
export async function POST(req: NextRequest, { params }: Ctx) {
|
||||
const { company, slug, block } = await params;
|
||||
return forwardPublic(req, `/api/public/pages/${encodeURIComponent(company)}/${encodeURIComponent(slug)}/form/${encodeURIComponent(block)}`, "POST");
|
||||
}
|
||||
Reference in new issue
Block a user