32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { apiJson, withApiHandler } from "@/lib/api-handler";
|
|
import { addBangForScope } from "@/modules/bangs/server/actions";
|
|
import { addBangInput } from "@/modules/bangs/server/schemas";
|
|
import { getBangStatsForScope } from "@/modules/bangs/server/queries";
|
|
|
|
export async function GET(request: Request) {
|
|
return withApiHandler(request, async (scope, req) => {
|
|
const url = new URL(req.url);
|
|
const limitParam = url.searchParams.get("limit");
|
|
const limit = limitParam ? Math.min(Math.max(Number(limitParam) || 10, 1), 100) : 10;
|
|
const stats = await getBangStatsForScope(scope.householdId, limit);
|
|
return apiJson(stats);
|
|
});
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
return withApiHandler(request, async (scope, req) => {
|
|
const body: unknown = await req.json().catch(() => ({}));
|
|
const parsed = addBangInput.parse(body);
|
|
const bang = await addBangForScope(scope, parsed);
|
|
return apiJson(
|
|
{
|
|
id: bang.id,
|
|
occurredOn: bang.occurredOn,
|
|
recordedBy: bang.recordedBy,
|
|
createdAt: bang.createdAt.toISOString(),
|
|
},
|
|
201,
|
|
);
|
|
});
|
|
}
|