fiddy/apps/web/app/api/buckets/route.ts
2026-02-11 23:45:15 -08:00

68 lines
3.1 KiB
TypeScript

import { NextResponse } from "next/server";
import { requireSessionUser } from "@/lib/server/session";
import { createBucket, listBuckets, requireActiveGroup } from "@/lib/server/buckets";
import { toErrorResponse } from "@/lib/server/errors";
import { getRequestMeta } from "@/lib/server/request";
function parseTags(value: unknown) {
if (Array.isArray(value)) return value.map(tag => String(tag));
if (typeof value === "string") return value.split(",");
return [] as string[];
}
export async function GET() {
const { requestId } = await getRequestMeta();
try {
const user = await requireSessionUser();
const groupId = await requireActiveGroup(user.id);
const buckets = await listBuckets(groupId);
return NextResponse.json({ requestId, buckets });
} catch (e) {
const { status, body } = toErrorResponse(e, "GET /api/buckets", requestId);
return NextResponse.json(body, { status });
}
}
export async function POST(req: Request) {
const { requestId } = await getRequestMeta();
try {
const user = await requireSessionUser();
const groupId = await requireActiveGroup(user.id);
const body = await req.json().catch(() => null);
const name = String(body?.name || "").trim();
const description = String(body?.description || "").trim();
const iconKey = body?.iconKey ? String(body.iconKey) : null;
const budgetLimitDollars = body?.budgetLimitDollars != null ? Number(body.budgetLimitDollars) : null;
const position = body?.position != null ? Number(body.position) : 0;
const tags = parseTags(body?.tags);
const necessity = String(body?.necessity || "BOTH").toUpperCase();
const windowDays = body?.windowDays != null ? Number(body.windowDays) : 30;
if (!name) return NextResponse.json({ requestId, error: { code: "MISSING_NAME", message: "name is required" } }, { status: 400 });
if (budgetLimitDollars != null && (!Number.isFinite(budgetLimitDollars) || budgetLimitDollars < 0))
return NextResponse.json({ requestId, error: { code: "INVALID_BUDGET", message: "Invalid budgetLimitDollars" } }, { status: 400 });
if (!['NECESSARY', 'BOTH', 'UNNECESSARY'].includes(necessity))
return NextResponse.json({ requestId, error: { code: "INVALID_NECESSITY", message: "Invalid necessity" } }, { status: 400 });
if (!Number.isFinite(windowDays) || windowDays < 1 || windowDays > 365)
return NextResponse.json({ requestId, error: { code: "INVALID_WINDOW_DAYS", message: "Invalid windowDays" } }, { status: 400 });
const bucket = await createBucket({
groupId,
userId: user.id,
name,
description: description || undefined,
iconKey,
budgetLimitDollars,
position,
tags,
necessity: necessity as "NECESSARY" | "BOTH" | "UNNECESSARY",
windowDays
});
return NextResponse.json({ requestId, bucket });
} catch (e) {
const { status, body } = toErrorResponse(e, "POST /api/buckets", requestId);
return NextResponse.json(body, { status });
}
}