first commit
This commit is contained in:
38
src/app/api/income/[id]/route.ts
Normal file
38
src/app/api/income/[id]/route.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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 });
|
||||
}
|
||||
Reference in New Issue
Block a user