79 lines
4.1 KiB
TypeScript
79 lines
4.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireSessionUser } from "@/lib/server/session";
|
|
import { requireActiveGroup, updateEntry, deleteEntry } from "@/lib/server/entries";
|
|
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 PATCH(req: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const groupId = await requireActiveGroup(user.id);
|
|
const { id: idParam } = await params;
|
|
const id = Number(idParam || 0);
|
|
if (!id) return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_ID", message: "Invalid id" } }, { status: 400 });
|
|
|
|
const body = await req.json().catch(() => null);
|
|
const amountDollars = Number(body?.amountDollars || 0);
|
|
const occurredAt = String(body?.occurredAt || "");
|
|
const necessity = String(body?.necessity || "");
|
|
const tags = parseTags(body?.tags);
|
|
const purchaseType = String(body?.purchaseType || tags[0] || "General").trim();
|
|
const notes = String(body?.notes || "").trim();
|
|
const entryType = String(body?.entryType || "SPENDING").toUpperCase();
|
|
const bucketId = body?.bucketId != null ? Number(body.bucketId) : null;
|
|
|
|
if (!Number.isFinite(amountDollars) || amountDollars <= 0)
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_AMOUNT", message: "Invalid amount" } }, { status: 400 });
|
|
if (!occurredAt) return NextResponse.json({ requestId, request_id: requestId, error: { code: "MISSING_OCCURRED_AT", message: "occurredAt is required" } }, { status: 400 });
|
|
if (!purchaseType) return NextResponse.json({ requestId, request_id: requestId, error: { code: "MISSING_PURCHASE_TYPE", message: "purchaseType is required" } }, { status: 400 });
|
|
if (!['NECESSARY', 'BOTH', 'UNNECESSARY'].includes(necessity))
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_NECESSITY", message: "Invalid necessity" } }, { status: 400 });
|
|
if (!['SPENDING', 'INCOME'].includes(entryType))
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_ENTRY_TYPE", message: "Invalid entryType" } }, { status: 400 });
|
|
const entry = await updateEntry({
|
|
id,
|
|
groupId,
|
|
userId: user.id,
|
|
entryType: entryType as "SPENDING" | "INCOME",
|
|
amountDollars,
|
|
occurredAt,
|
|
necessity: necessity as "NECESSARY" | "BOTH" | "UNNECESSARY",
|
|
purchaseType,
|
|
notes: notes || undefined,
|
|
tags,
|
|
bucketId
|
|
});
|
|
|
|
if (!entry) return NextResponse.json({ requestId, request_id: requestId, error: { code: "NOT_FOUND", message: "Not found" } }, { status: 404 });
|
|
|
|
return NextResponse.json({ requestId, request_id: requestId, entry });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "PATCH /api/entries/[id]", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
const { requestId } = await getRequestMeta();
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const groupId = await requireActiveGroup(user.id);
|
|
const { id: idParam } = await params;
|
|
const id = Number(idParam || 0);
|
|
if (!id) return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_ID", message: "Invalid id" } }, { status: 400 });
|
|
|
|
await deleteEntry({ id, groupId, userId: user.id });
|
|
return NextResponse.json({ requestId, request_id: requestId, ok: true });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "DELETE /api/entries/[id]", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|