99 lines
5.7 KiB
TypeScript
99 lines
5.7 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireSessionUser } from "@/lib/server/session";
|
|
import { deleteRecurringEntry, requireActiveGroup, updateRecurringEntry } from "@/lib/server/recurring-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 frequency = body?.frequency ? String(body.frequency).toUpperCase() : null;
|
|
const intervalCount = Number(body?.intervalCount || 1);
|
|
const endCondition = body?.endCondition ? String(body.endCondition).toUpperCase() : null;
|
|
const endCount = body?.endCount != null ? Number(body.endCount) : null;
|
|
const endDate = body?.endDate ? String(body.endDate) : null;
|
|
const nextRunAt = body?.nextRunAt ? String(body.nextRunAt) : occurredAt;
|
|
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 });
|
|
if (!Number.isFinite(intervalCount) || intervalCount <= 0)
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_INTERVAL", message: "Invalid intervalCount" } }, { status: 400 });
|
|
if (frequency && !['DAILY', 'WEEKLY', 'BIWEEKLY', 'MONTHLY', 'QUARTERLY', 'YEARLY'].includes(frequency))
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_FREQUENCY", message: "Invalid frequency" } }, { status: 400 });
|
|
if (endCondition && !['NEVER', 'AFTER_COUNT', 'BY_DATE'].includes(endCondition))
|
|
return NextResponse.json({ requestId, request_id: requestId, error: { code: "INVALID_END_CONDITION", message: "Invalid endCondition" } }, { status: 400 });
|
|
|
|
const entry = await updateRecurringEntry({
|
|
id,
|
|
groupId,
|
|
userId: user.id,
|
|
entryType: entryType as "SPENDING" | "INCOME",
|
|
amountDollars,
|
|
occurredAt,
|
|
necessity: necessity as "NECESSARY" | "BOTH" | "UNNECESSARY",
|
|
purchaseType,
|
|
notes: notes || undefined,
|
|
tags,
|
|
isRecurring: true,
|
|
frequency: frequency as "DAILY" | "WEEKLY" | "BIWEEKLY" | "MONTHLY" | "QUARTERLY" | "YEARLY" | null,
|
|
intervalCount,
|
|
endCondition: endCondition as "NEVER" | "AFTER_COUNT" | "BY_DATE" | null,
|
|
endCount,
|
|
endDate,
|
|
nextRunAt,
|
|
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/recurring-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 deleteRecurringEntry({ id, groupId, userId: user.id });
|
|
return NextResponse.json({ requestId, request_id: requestId, ok: true });
|
|
} catch (e) {
|
|
const { status, body } = toErrorResponse(e, "DELETE /api/recurring-entries/[id]", requestId);
|
|
return NextResponse.json(body, { status });
|
|
}
|
|
}
|