feat(admin-portal): event leads data layer
One proxy route for /api/admin/event-leads (dot segments rejected), service, types, and /event-leads gated by BO_EVENT_LEADS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
18672ba173
commit
90f25debc2
7 files changed
+266
-1
No files matched your search
@@ -0,0 +1,62 @@
|
||||
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, POST, PUT } from "./route";
|
||||
|
||||
type Req = Parameters<typeof GET>[0];
|
||||
const ctx = (...path: string[]) => ({ params: Promise.resolve({ path }) });
|
||||
const req = (url: string, init?: RequestInit) => new Request(`http://localhost${url}`, init) as unknown as Req;
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.callMobileApi.mockReset();
|
||||
mocks.getToken.mockReset();
|
||||
mocks.getToken.mockResolvedValue("jwt-abc");
|
||||
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { ok: 1 } });
|
||||
});
|
||||
|
||||
describe("/api/event-leads/[...path]", () => {
|
||||
it("returns 401 without a session", async () => {
|
||||
mocks.getToken.mockResolvedValue(null);
|
||||
const res = await GET(req("/api/event-leads/events"), ctx("events"));
|
||||
expect(res.status).toBe(401);
|
||||
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("forwards GET with the query string", async () => {
|
||||
const res = await GET(req("/api/event-leads/9/leads?status=OPEN&page=0&size=20"), ctx("9", "leads"));
|
||||
expect(res.status).toBe(200);
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/9/leads?status=OPEN&page=0&size=20", { token: "jwt-abc" });
|
||||
});
|
||||
|
||||
it("forwards POST and PUT bodies", async () => {
|
||||
await POST(req("/api/event-leads/9/match", { method: "POST", body: JSON.stringify({ mobile: "0123" }) }), ctx("9", "match"));
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/9/match", { method: "POST", body: { mobile: "0123" }, token: "jwt-abc" });
|
||||
await PUT(req("/api/event-leads/9/leads/7", { method: "PUT", body: JSON.stringify({ status: "WON" }) }), ctx("9", "leads", "7"));
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/9/leads/7", { method: "PUT", body: { status: "WON" }, token: "jwt-abc" });
|
||||
});
|
||||
|
||||
it("rejects dot segments so the proxy cannot leave /api/admin/event-leads", async () => {
|
||||
const res = await GET(req("/api/event-leads/x"), ctx("..", "users"));
|
||||
expect(res.status).toBe(400);
|
||||
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("encodes path segments", async () => {
|
||||
await GET(req("/api/event-leads/a"), ctx("a/b"));
|
||||
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-leads/a%2Fb", { token: "jwt-abc" });
|
||||
});
|
||||
|
||||
it("passes upstream 409 with code and details", async () => {
|
||||
mocks.callMobileApi.mockResolvedValue({
|
||||
ok: false, error: "Already captured", status: 409, code: "ALREADY_CAPTURED", details: { eventLeadUuid: "55" },
|
||||
});
|
||||
const res = await POST(req("/api/event-leads/9", { method: "POST", body: "{}" }), ctx("9"));
|
||||
expect(res.status).toBe(409);
|
||||
const json = (await res.json()) as Record<string, unknown>;
|
||||
expect(json.code).toBe("ALREADY_CAPTURED");
|
||||
expect(json.details).toEqual({ eventLeadUuid: "55" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { callMobileApi } from "@/app/lib/server/mobileApi";
|
||||
import { getToken } from "@/app/lib/server/session";
|
||||
import { badRequest, errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
|
||||
|
||||
// One proxy for every /api/admin/event-leads endpoint; mobile-api owns validation and the BO_EVENT_LEADS guard.
|
||||
type Ctx = { params: Promise<{ path: string[] }> };
|
||||
|
||||
async function forward(req: NextRequest, { params }: Ctx, method?: "POST" | "PUT") {
|
||||
const token = await getToken();
|
||||
if (!token) return unauthorized();
|
||||
const { path } = await params;
|
||||
if (path.some((s) => s === "." || s === "..")) return badRequest("Invalid path.");
|
||||
const target = `/api/admin/event-leads/${path.map(encodeURIComponent).join("/")}${new URL(req.url).search}`;
|
||||
const result = method
|
||||
? await callMobileApi<unknown>(target, { method, body: await req.json().catch(() => ({})), token })
|
||||
: await callMobileApi<unknown>(target, { token });
|
||||
if (!result.ok) return errorResponse(result);
|
||||
return NextResponse.json(result.data);
|
||||
}
|
||||
|
||||
export const GET = (req: NextRequest, ctx: Ctx) => forward(req, ctx);
|
||||
export const POST = (req: NextRequest, ctx: Ctx) => forward(req, ctx, "POST");
|
||||
export const PUT = (req: NextRequest, ctx: Ctx) => forward(req, ctx, "PUT");
|
||||
Reference in new issue
Block a user