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"; import { paging, SORT, TEXT, upstreamQuery } from "@/app/lib/server/upstreamQuery"; import type { AdminMemberPage } from "@/app/lib/types"; // GET -> /api/admin/members*. Forwards the bb_admin JWT from the session cookie // and the full query string (filters + paging) to mobile-api. Read-only — no // side effect on GET (Next.js prefetch-safe). const QUERY = { memberCode: TEXT, name: TEXT, email: TEXT, phoneNo: TEXT, status: TEXT, joinDateFrom: TEXT, joinDateTo: TEXT, gender: TEXT, bank: TEXT, sponsorId: TEXT, q: TEXT, rank: TEXT, nickname: TEXT, sponsor: TEXT, referrer: TEXT, ...paging(500), ...SORT, }; export async function GET(req: NextRequest) { const token = await getToken(); if (!token) return unauthorized(); // portal-query-allowlist (F38): only the params mobile-api accepts, validated and encoded. const qs = upstreamQuery(new URL(req.url).searchParams, QUERY); const result = await callMobileApi(`/api/admin/members${qs}`, { token }); if (!result.ok) return errorResponse(result); return NextResponse.json(result.data); } // POST -> admin "Register member" (babyboo-42). Only the registration fields are forwarded, as // strings — never an actor or a password; the actor comes from the JWT upstream (mobile-api), and a // caller without BO_MEMBERS is refused there with 403, which passes through. const REGISTRATION_FIELDS = ["name", "nickname", "email", "dob", "phoneNoPrefix", "phoneNo", "referralCode"] as const; export async function POST(req: NextRequest) { const token = await getToken(); if (!token) return unauthorized(); const raw = (await req.json().catch(() => ({}))) as Record; const body = Object.fromEntries( REGISTRATION_FIELDS.map(key => [key, typeof raw[key] === "string" ? raw[key] : ""]), ); const result = await callMobileApi<{ memberCode: string; emailSent: boolean }>("/api/admin/members", { method: "POST", body, token, }); if (!result.ok) return errorResponse(result); return NextResponse.json(result.data); }