39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { requireUser } from "@/lib/auth";
|
|
import {
|
|
databaseConfig,
|
|
deleteDocument,
|
|
getOwnedDocument,
|
|
updateDocument,
|
|
} from "@/lib/database/server";
|
|
import { parseBody, updatedField } from "@/lib/validation/common";
|
|
import { parseIncomeInput } from "@/lib/validation/income";
|
|
import type { Income } from "@/types/finance";
|
|
|
|
type Ctx = { params: Promise<{ id: string }> };
|
|
|
|
export async function PATCH(request: Request, ctx: Ctx) {
|
|
const auth = await requireUser();
|
|
if ("response" in auth) return auth.response;
|
|
const { id } = await ctx.params;
|
|
const parsed = await parseBody(request, parseIncomeInput);
|
|
if (!parsed.ok)
|
|
return Response.json({ error: parsed.error }, { status: 400 });
|
|
const { collections } = databaseConfig();
|
|
await getOwnedDocument<Income>(collections.income, id, auth.user.id);
|
|
const document = await updateDocument<Income>(collections.income, id, {
|
|
...parsed.data,
|
|
...updatedField(),
|
|
});
|
|
return Response.json(document);
|
|
}
|
|
|
|
export async function DELETE(_request: Request, ctx: Ctx) {
|
|
const auth = await requireUser();
|
|
if ("response" in auth) return auth.response;
|
|
const { id } = await ctx.params;
|
|
const { collections } = databaseConfig();
|
|
await getOwnedDocument<Income>(collections.income, id, auth.user.id);
|
|
await deleteDocument(collections.income, id);
|
|
return Response.json({ ok: true });
|
|
}
|