36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireSessionUser } from "@/lib/server/session";
|
|
import { getActiveGroupId, listGroups, setActiveGroupForUser } from "@/lib/server/groups";
|
|
import { toErrorResponse } from "@/lib/server/errors";
|
|
import { getRequestMeta } from "@/lib/server/request";
|
|
|
|
export async function GET() {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const groups = await listGroups(user.id);
|
|
const activeGroupId = await getActiveGroupId(user.id);
|
|
const active = groups.find(group => Number(group.id) === activeGroupId) || null;
|
|
return NextResponse.json({ requestId, active });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "GET /api/groups/active", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const body = await req.json().catch(() => null);
|
|
const groupId = Number(body?.groupId || 0);
|
|
if (!groupId) return NextResponse.json({ requestId, error: { code: "MISSING_GROUP_ID", message: "groupId is required" } }, { status: 400 });
|
|
|
|
await setActiveGroupForUser(user.id, groupId);
|
|
return NextResponse.json({ requestId, ok: true });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "POST /api/groups/active", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|