Refactor financial transaction handling: Consolidate Einnahmen and Ausgaben into Buchung model, update routes and UI components, and add new migration scripts for database schema changes.

This commit is contained in:
hwinkel
2026-03-24 21:06:07 +01:00
parent d582c748a2
commit 1ec15600b5
18 changed files with 928 additions and 358 deletions
+14 -19
View File
@@ -1,10 +1,9 @@
import { getApiUser } from "@/session.server";
import prisma from "@/lib/prisma.server";
import { z } from "zod";
import { EinnahmeKategorie } from "@prisma/client";
const updateSchema = z.object({
kategorie: z.nativeEnum(EinnahmeKategorie),
kategorie: z.string().min(1),
betrag: z.number().positive(),
steuersatz: z.number().min(0).default(0),
zahlungsart: z.enum(["KASSE", "BANK"]).default("BANK"),
@@ -13,29 +12,25 @@ const updateSchema = z.object({
});
/**
* Handles an API request to create, update or delete a einnahme.
* Handles an API request to update or delete a einnahme (Buchung).
*
* @param {Request} request - The request object.
* @param {Object} params - The route parameters.
* @param {string} params.id - The id of the einnahme to update or delete.
* @param {string} params.id - The id of the Buchung (einnahme) to update or delete.
*
* @returns {Promise<Response>} - A promise resolving to a Response object.
*
* @throws {Response} - If the request is unauthorized, returns a 401 response with an error message.
* @throws {Response} - If the einnahme is not found, returns a 404 response with an error message.
* @throws {Response} - If the request body is invalid, returns a 400 response with an error message containing the validation errors.
*/
export async function action({ request, params }: { request: Request; params: { id: string } }) {
const user = await getApiUser(request);
if (!user) return Response.json({ error: "Unauthorized" }, { status: 401 });
const einnahme = await prisma.betriebseinnahme.findFirst({
where: { id: params.id, company: { userId: user.id } },
const buchung = await prisma.buchung.findFirst({
where: { id: params.id, company: { userId: user.id }, type: "EINLAGE", isBusinessRecord: true },
});
if (!einnahme) return Response.json({ error: "Not found" }, { status: 404 });
if (!buchung) return Response.json({ error: "Not found" }, { status: 404 });
if (request.method === "DELETE") {
await prisma.betriebseinnahme.delete({ where: { id: params.id } });
await prisma.buchung.delete({ where: { id: params.id } });
return Response.json({ ok: true });
}
@@ -43,22 +38,22 @@ export async function action({ request, params }: { request: Request; params: {
const parsed = updateSchema.safeParse(body);
if (!parsed.success) return Response.json({ error: parsed.error.issues }, { status: 400 });
const updated = await prisma.betriebseinnahme.update({
const updated = await prisma.buchung.update({
where: { id: params.id },
data: {
kategorie: parsed.data.kategorie,
betrag: parsed.data.betrag,
amount: parsed.data.betrag,
steuersatz: parsed.data.steuersatz,
zahlungsart: parsed.data.zahlungsart,
datum: new Date(parsed.data.datum),
beschreibung: parsed.data.beschreibung,
account: parsed.data.zahlungsart === "KASSE" ? "KASSE" : "BANK",
date: new Date(parsed.data.datum),
description: parsed.data.beschreibung,
},
});
return Response.json({
...updated,
betrag: Number(updated.betrag),
steuersatz: Number(updated.steuersatz),
datum: updated.datum.toISOString(),
amount: Number(updated.amount),
date: updated.date.toISOString(),
});
}