24 lines
1.0 KiB
TypeScript
24 lines
1.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireSessionUser } from "@/lib/server/session";
|
|
import { joinGroup } from "@/lib/server/groups";
|
|
import { toErrorResponse } from "@/lib/server/errors";
|
|
import { getRequestMeta } from "@/lib/server/request";
|
|
|
|
export async function POST(req: Request) {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const body = await req.json().catch(() => null);
|
|
const inviteCode = String(body?.inviteCode || "").trim().toUpperCase();
|
|
if (!inviteCode)
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "MISSING_INVITE_CODE", message: "Invite code is required" } }, { status: 400 });
|
|
|
|
const group = await joinGroup(user.id, inviteCode);
|
|
return NextResponse.json({ requestId, request_id: requestId, group });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "POST /api/groups/join", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|
|
|