Fix period editing and harden app with Next.js security upgrade
This commit is contained in:
5
next-env.d.ts
vendored
5
next-env.d.ts
vendored
@@ -1,5 +1,6 @@
|
|||||||
/// <reference types="next" />
|
/// <reference types="next" />
|
||||||
/// <reference types="next/image-types/global" />
|
/// <reference types="next/image-types/global" />
|
||||||
|
/// <reference path="./.next/types/routes.d.ts" />
|
||||||
|
|
||||||
// This file is auto-generated by Next.js.
|
// NOTE: This file should not be edited
|
||||||
|
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||||
|
|||||||
2324
package-lock.json
generated
Normal file
2324
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -20,7 +20,7 @@
|
|||||||
"@mui/material": "^6.1.3",
|
"@mui/material": "^6.1.3",
|
||||||
"@prisma/client": "^5.20.0",
|
"@prisma/client": "^5.20.0",
|
||||||
"bcryptjs": "^2.4.3",
|
"bcryptjs": "^2.4.3",
|
||||||
"next": "^14.2.14",
|
"next": "^15.5.15",
|
||||||
"next-auth": "^4.24.8",
|
"next-auth": "^4.24.8",
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.3.1",
|
"react-dom": "^18.3.1",
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import prisma from "@/lib/prisma";
|
|||||||
import { getCurrentViewer } from "@/lib/session";
|
import { getCurrentViewer } from "@/lib/session";
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
function asRecord(value: unknown, label: string) {
|
function asRecord(value: unknown, label: string) {
|
||||||
@@ -65,6 +65,7 @@ function asApprovalPermissions(value: unknown) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(_: Request, { params }: Context) {
|
export async function POST(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -77,7 +78,7 @@ export async function POST(_: Request, { params }: Context) {
|
|||||||
|
|
||||||
const auditLog = await prisma.auditLog.findUnique({
|
const auditLog = await prisma.auditLog.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: params.id
|
id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -297,6 +298,22 @@ export async function POST(_: Request, { params }: Context) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "period.update": {
|
||||||
|
const previous = asRecord(rollback.previous, "Zeitraum");
|
||||||
|
|
||||||
|
await tx.accountingPeriod.update({
|
||||||
|
where: {
|
||||||
|
id: asString(previous.id, "Zeitraum-ID")
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
name: asString(previous.name, "Zeitraumname"),
|
||||||
|
startsAt: asDate(previous.startsAt, "Zeitraumstart") ?? new Date(),
|
||||||
|
endsAt: asDate(previous.endsAt, "Zeitraumende") ?? new Date()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case "period.setCurrent": {
|
case "period.setCurrent": {
|
||||||
const previousCurrentPeriodId = asNullableString(rollback.previousCurrentPeriodId);
|
const previousCurrentPeriodId = asNullableString(rollback.previousCurrentPeriodId);
|
||||||
|
|
||||||
|
|||||||
@@ -26,12 +26,13 @@ const updateBudgetSchema = z
|
|||||||
});
|
});
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function PATCH(request: Request, { params }: Context) {
|
export async function PATCH(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -43,7 +44,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const budget = await prisma.budget.findUnique({
|
const budget = await prisma.budget.findUnique({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!budget) {
|
if (!budget) {
|
||||||
@@ -61,7 +62,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
const previousBudget = budget;
|
const previousBudget = budget;
|
||||||
const nextReleasedAmount = parsed.data.releasedAmount ?? Number(previousBudget.releasedAmount);
|
const nextReleasedAmount = parsed.data.releasedAmount ?? Number(previousBudget.releasedAmount);
|
||||||
const updatedBudget = await prisma.budget.update({
|
const updatedBudget = await prisma.budget.update({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
name: parsed.data.name,
|
name: parsed.data.name,
|
||||||
totalBudget: parsed.data.totalBudget,
|
totalBudget: parsed.data.totalBudget,
|
||||||
@@ -106,6 +107,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(_: Request, { params }: Context) {
|
export async function DELETE(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -117,7 +119,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const budget = await prisma.budget.findUnique({
|
const budget = await prisma.budget.findUnique({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
_count: {
|
_count: {
|
||||||
select: {
|
select: {
|
||||||
@@ -139,7 +141,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await prisma.budget.delete({
|
await prisma.budget.delete({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
await createAuditLog(prisma, {
|
await createAuditLog(prisma, {
|
||||||
|
|||||||
@@ -18,12 +18,13 @@ const approvalSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function POST(request: Request, { params }: Context) {
|
export async function POST(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -32,7 +33,7 @@ export async function POST(request: Request, { params }: Context) {
|
|||||||
|
|
||||||
const [expense, appSettings] = await Promise.all([
|
const [expense, appSettings] = await Promise.all([
|
||||||
prisma.expense.findUnique({
|
prisma.expense.findUnique({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
approvals: true
|
approvals: true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,13 @@ const documentedSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function POST(request: Request, { params }: Context) {
|
export async function POST(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -30,7 +31,7 @@ export async function POST(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const expense = await prisma.expense.findUnique({
|
const expense = await prisma.expense.findUnique({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!expense) {
|
if (!expense) {
|
||||||
@@ -53,7 +54,7 @@ export async function POST(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatedExpense = await prisma.expense.update({
|
const updatedExpense = await prisma.expense.update({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
proofUrl: parsed.data.proofUrl ?? expense.proofUrl,
|
proofUrl: parsed.data.proofUrl ?? expense.proofUrl,
|
||||||
documentedAt: expense.documentedAt ?? new Date()
|
documentedAt: expense.documentedAt ?? new Date()
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import prisma from "@/lib/prisma";
|
|||||||
import { getCurrentViewer } from "@/lib/session";
|
import { getCurrentViewer } from "@/lib/session";
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function POST(_: Request, { params }: Context) {
|
export async function POST(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -23,7 +24,7 @@ export async function POST(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const expense = await prisma.expense.findUnique({
|
const expense = await prisma.expense.findUnique({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!expense) {
|
if (!expense) {
|
||||||
@@ -35,7 +36,7 @@ export async function POST(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatedExpense = await prisma.expense.update({
|
const updatedExpense = await prisma.expense.update({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
paidAt: expense.paidAt ?? new Date()
|
paidAt: expense.paidAt ?? new Date()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import prisma from "@/lib/prisma";
|
|||||||
import { getCurrentViewer } from "@/lib/session";
|
import { getCurrentViewer } from "@/lib/session";
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function DELETE(_: Request, { params }: Context) {
|
export async function DELETE(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -19,7 +20,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const expense = await prisma.expense.findUnique({
|
const expense = await prisma.expense.findUnique({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!expense) {
|
if (!expense) {
|
||||||
@@ -41,7 +42,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await prisma.expense.delete({
|
await prisma.expense.delete({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
await createAuditLog(prisma, {
|
await createAuditLog(prisma, {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
import { snapshotPeriod } from "@/lib/audit-snapshots";
|
import { snapshotPeriod } from "@/lib/audit-snapshots";
|
||||||
import { createAuditLog } from "@/lib/audit-log";
|
import { createAuditLog } from "@/lib/audit-log";
|
||||||
@@ -7,12 +8,101 @@ import prisma from "@/lib/prisma";
|
|||||||
import { getCurrentViewer } from "@/lib/session";
|
import { getCurrentViewer } from "@/lib/session";
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const periodSchema = z.object({
|
||||||
|
name: z.string().trim().min(2).max(80),
|
||||||
|
startsAt: z.coerce.date(),
|
||||||
|
endsAt: z.coerce.date()
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function PATCH(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
|
if (!viewer) {
|
||||||
|
return NextResponse.json({ error: "Nicht angemeldet." }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!canManageBudgets(viewer.role)) {
|
||||||
|
return NextResponse.json({ error: "Nur Vorstand oder Finanz-AG dürfen Zeiträume bearbeiten." }, { status: 403 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.json().catch(() => null);
|
||||||
|
const parsed = periodSchema.safeParse(body);
|
||||||
|
|
||||||
|
if (!parsed.success) {
|
||||||
|
return NextResponse.json({ error: "Bitte Zeitraumname sowie Start- und Enddatum korrekt angeben." }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.data.endsAt < parsed.data.startsAt) {
|
||||||
|
return NextResponse.json({ error: "Das Enddatum muss nach dem Startdatum liegen." }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const period = await prisma.accountingPeriod.findUnique({
|
||||||
|
where: { id }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!period) {
|
||||||
|
return NextResponse.json({ error: "Zeitraum nicht gefunden." }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingPeriod = await prisma.accountingPeriod.findFirst({
|
||||||
|
where: {
|
||||||
|
id: {
|
||||||
|
not: id
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
equals: parsed.data.name,
|
||||||
|
mode: "insensitive"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingPeriod) {
|
||||||
|
return NextResponse.json({ error: "Diesen Zeitraumnamen gibt es bereits." }, { status: 409 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedPeriod = await prisma.accountingPeriod.update({
|
||||||
|
where: {
|
||||||
|
id
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
name: parsed.data.name,
|
||||||
|
startsAt: parsed.data.startsAt,
|
||||||
|
endsAt: parsed.data.endsAt
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await createAuditLog(prisma, {
|
||||||
|
actorId: viewer.id,
|
||||||
|
action: "period.update",
|
||||||
|
entityType: "period",
|
||||||
|
entityId: updatedPeriod.id,
|
||||||
|
entityLabel: updatedPeriod.name,
|
||||||
|
summary:
|
||||||
|
period.name !== updatedPeriod.name
|
||||||
|
? `Zeitraum ${period.name} wurde auf ${updatedPeriod.name} aktualisiert.`
|
||||||
|
: `Zeitraum ${period.name} wurde aktualisiert.`,
|
||||||
|
metadata: {
|
||||||
|
startsAt: updatedPeriod.startsAt.toISOString(),
|
||||||
|
endsAt: updatedPeriod.endsAt.toISOString(),
|
||||||
|
rollback: {
|
||||||
|
kind: "period.update",
|
||||||
|
previous: snapshotPeriod(period),
|
||||||
|
next: snapshotPeriod(updatedPeriod)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({ period: updatedPeriod });
|
||||||
|
}
|
||||||
|
|
||||||
export async function DELETE(_: Request, { params }: Context) {
|
export async function DELETE(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -24,7 +114,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const period = await prisma.accountingPeriod.findUnique({
|
const period = await prisma.accountingPeriod.findUnique({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
_count: {
|
_count: {
|
||||||
select: {
|
select: {
|
||||||
@@ -51,7 +141,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await prisma.accountingPeriod.delete({
|
await prisma.accountingPeriod.delete({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
await createAuditLog(prisma, {
|
await createAuditLog(prisma, {
|
||||||
|
|||||||
@@ -12,12 +12,13 @@ const passwordSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function POST(request: Request, { params }: Context) {
|
export async function POST(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -36,7 +37,7 @@ export async function POST(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
select: { id: true, username: true, passwordHash: true }
|
select: { id: true, username: true, passwordHash: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -47,7 +48,7 @@ export async function POST(request: Request, { params }: Context) {
|
|||||||
const passwordHash = await bcrypt.hash(parsed.data.password, 12);
|
const passwordHash = await bcrypt.hash(parsed.data.password, 12);
|
||||||
|
|
||||||
await prisma.user.update({
|
await prisma.user.update({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
passwordHash
|
passwordHash
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,12 +49,13 @@ function serializeManagedUser(user: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function PATCH(request: Request, { params }: Context) {
|
export async function PATCH(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -73,7 +74,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
@@ -113,7 +114,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
const previousSnapshot = snapshotUser(user);
|
const previousSnapshot = snapshotUser(user);
|
||||||
|
|
||||||
const updatedUser = await prisma.user.update({
|
const updatedUser = await prisma.user.update({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
role: parsed.data.role,
|
role: parsed.data.role,
|
||||||
workingGroupId,
|
workingGroupId,
|
||||||
@@ -163,6 +164,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(_: Request, { params }: Context) {
|
export async function DELETE(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -173,12 +175,12 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
return NextResponse.json({ error: "Nur Vorstand oder Finanz-AG dürfen Nutzer löschen." }, { status: 403 });
|
return NextResponse.json({ error: "Nur Vorstand oder Finanz-AG dürfen Nutzer löschen." }, { status: 403 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (viewer.id === params.id) {
|
if (viewer.id === id) {
|
||||||
return NextResponse.json({ error: "Du kannst dein eigenes Konto hier nicht löschen." }, { status: 400 });
|
return NextResponse.json({ error: "Du kannst dein eigenes Konto hier nicht löschen." }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
_count: {
|
_count: {
|
||||||
select: {
|
select: {
|
||||||
@@ -211,7 +213,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await prisma.user.delete({
|
await prisma.user.delete({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
await createAuditLog(prisma, {
|
await createAuditLog(prisma, {
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import prisma from "@/lib/prisma";
|
|||||||
import { getCurrentViewer } from "@/lib/session";
|
import { getCurrentViewer } from "@/lib/session";
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
params: {
|
params: Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
};
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const workingGroupSchema = z.object({
|
const workingGroupSchema = z.object({
|
||||||
@@ -18,6 +18,7 @@ const workingGroupSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export async function PATCH(request: Request, { params }: Context) {
|
export async function PATCH(request: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -36,7 +37,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const workingGroup = await prisma.workingGroup.findUnique({
|
const workingGroup = await prisma.workingGroup.findUnique({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!workingGroup) {
|
if (!workingGroup) {
|
||||||
@@ -46,7 +47,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
const existingWorkingGroup = await prisma.workingGroup.findFirst({
|
const existingWorkingGroup = await prisma.workingGroup.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: {
|
id: {
|
||||||
not: params.id
|
not: id
|
||||||
},
|
},
|
||||||
name: {
|
name: {
|
||||||
equals: parsed.data.name,
|
equals: parsed.data.name,
|
||||||
@@ -61,7 +62,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
|
|
||||||
const updatedWorkingGroup = await prisma.workingGroup.update({
|
const updatedWorkingGroup = await prisma.workingGroup.update({
|
||||||
where: {
|
where: {
|
||||||
id: params.id
|
id
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
name: parsed.data.name
|
name: parsed.data.name
|
||||||
@@ -88,6 +89,7 @@ export async function PATCH(request: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(_: Request, { params }: Context) {
|
export async function DELETE(_: Request, { params }: Context) {
|
||||||
|
const { id } = await params;
|
||||||
const viewer = await getCurrentViewer();
|
const viewer = await getCurrentViewer();
|
||||||
|
|
||||||
if (!viewer) {
|
if (!viewer) {
|
||||||
@@ -99,7 +101,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const workingGroup = await prisma.workingGroup.findUnique({
|
const workingGroup = await prisma.workingGroup.findUnique({
|
||||||
where: { id: params.id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
_count: {
|
_count: {
|
||||||
select: {
|
select: {
|
||||||
@@ -126,7 +128,7 @@ export async function DELETE(_: Request, { params }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await prisma.workingGroup.delete({
|
await prisma.workingGroup.delete({
|
||||||
where: { id: params.id }
|
where: { id }
|
||||||
});
|
});
|
||||||
|
|
||||||
await createAuditLog(prisma, {
|
await createAuditLog(prisma, {
|
||||||
|
|||||||
@@ -110,6 +110,12 @@ type PeriodFormState = {
|
|||||||
copyBudgetsFromPeriodId: string;
|
copyBudgetsFromPeriodId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type PeriodEditFormState = {
|
||||||
|
name: string;
|
||||||
|
startsAt: string;
|
||||||
|
endsAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
type DashboardMessage = {
|
type DashboardMessage = {
|
||||||
type: "success" | "error";
|
type: "success" | "error";
|
||||||
text: string;
|
text: string;
|
||||||
@@ -191,6 +197,22 @@ function getSuggestedPeriodDraft(currentPeriod: DashboardAccountingPeriod | unde
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPeriodEditDraft(period: DashboardAccountingPeriod | null | undefined): PeriodEditFormState {
|
||||||
|
if (!period) {
|
||||||
|
return {
|
||||||
|
name: "",
|
||||||
|
startsAt: "",
|
||||||
|
endsAt: ""
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: period.name,
|
||||||
|
startsAt: toDateInputValue(period.startsAt),
|
||||||
|
endsAt: toDateInputValue(period.endsAt)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function generatePassword(length = 14) {
|
function generatePassword(length = 14) {
|
||||||
const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@$%";
|
const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@$%";
|
||||||
const cryptoSource = globalThis.crypto;
|
const cryptoSource = globalThis.crypto;
|
||||||
@@ -297,6 +319,7 @@ export function DashboardShell({
|
|||||||
const [userDrafts, setUserDrafts] = useState<Record<string, ManagedUserDraft>>({});
|
const [userDrafts, setUserDrafts] = useState<Record<string, ManagedUserDraft>>({});
|
||||||
const [approvalThresholdDraft, setApprovalThresholdDraft] = useState(approvalThreshold.toFixed(2));
|
const [approvalThresholdDraft, setApprovalThresholdDraft] = useState(approvalThreshold.toFixed(2));
|
||||||
const [periodForm, setPeriodForm] = useState<PeriodFormState>(getSuggestedPeriodDraft(currentPeriod));
|
const [periodForm, setPeriodForm] = useState<PeriodFormState>(getSuggestedPeriodDraft(currentPeriod));
|
||||||
|
const [periodEditForm, setPeriodEditForm] = useState<PeriodEditFormState>(getPeriodEditDraft(currentPeriod));
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (visibleGroups.length === 0) {
|
if (visibleGroups.length === 0) {
|
||||||
setSelectedMobileGroupId("");
|
setSelectedMobileGroupId("");
|
||||||
@@ -314,6 +337,11 @@ export function DashboardShell({
|
|||||||
setPeriodForm(getSuggestedPeriodDraft(currentPeriod));
|
setPeriodForm(getSuggestedPeriodDraft(currentPeriod));
|
||||||
}, [currentPeriod, currentPeriodId]);
|
}, [currentPeriod, currentPeriodId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const selectedPeriod = accountingPeriods.find((period) => period.id === selectedCurrentPeriodId) ?? currentPeriod ?? null;
|
||||||
|
setPeriodEditForm(getPeriodEditDraft(selectedPeriod));
|
||||||
|
}, [accountingPeriods, currentPeriod, selectedCurrentPeriodId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!desktopSections.some((section) => section.value === desktopSection)) {
|
if (!desktopSections.some((section) => section.value === desktopSection)) {
|
||||||
setDesktopSection("overview");
|
setDesktopSection("overview");
|
||||||
@@ -468,6 +496,11 @@ export function DashboardShell({
|
|||||||
) ?? 0;
|
) ?? 0;
|
||||||
const selectedPeriodForManagement =
|
const selectedPeriodForManagement =
|
||||||
accountingPeriods.find((period) => period.id === selectedCurrentPeriodId) ?? currentPeriod ?? null;
|
accountingPeriods.find((period) => period.id === selectedCurrentPeriodId) ?? currentPeriod ?? null;
|
||||||
|
const periodEditDirty =
|
||||||
|
selectedPeriodForManagement !== null &&
|
||||||
|
(periodEditForm.name.trim() !== selectedPeriodForManagement.name ||
|
||||||
|
periodEditForm.startsAt !== toDateInputValue(selectedPeriodForManagement.startsAt) ||
|
||||||
|
periodEditForm.endsAt !== toDateInputValue(selectedPeriodForManagement.endsAt));
|
||||||
|
|
||||||
function getManagedUserDraft(user: DashboardManagedUser): ManagedUserDraft {
|
function getManagedUserDraft(user: DashboardManagedUser): ManagedUserDraft {
|
||||||
return userDrafts[user.id] ?? {
|
return userDrafts[user.id] ?? {
|
||||||
@@ -810,6 +843,27 @@ export function DashboardShell({
|
|||||||
}, "Neuer Abrechnungszeitraum wurde angelegt.");
|
}, "Neuer Abrechnungszeitraum wurde angelegt.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleSavePeriod(event: FormEvent<HTMLFormElement>) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
if (!selectedPeriodForManagement) {
|
||||||
|
setMessage({ type: "error", text: "Bitte zuerst einen Zeitraum auswählen." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await runAction(async () => {
|
||||||
|
await parseResponse(
|
||||||
|
await fetch(`/api/periods/${selectedPeriodForManagement.id}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify(periodEditForm)
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}, `Zeitraum ${periodEditForm.name.trim() || selectedPeriodForManagement.name} wurde aktualisiert.`);
|
||||||
|
}
|
||||||
|
|
||||||
async function handleDeletePeriod(periodId: string, periodName: string) {
|
async function handleDeletePeriod(periodId: string, periodName: string) {
|
||||||
await runAction(async () => {
|
await runAction(async () => {
|
||||||
await parseResponse(
|
await parseResponse(
|
||||||
@@ -1222,6 +1276,55 @@ export function DashboardShell({
|
|||||||
: "Leere, nicht aktive Zeiträume lassen sich hier wieder entfernen."}
|
: "Leere, nicht aktive Zeiträume lassen sich hier wieder entfernen."}
|
||||||
</Typography>
|
</Typography>
|
||||||
|
|
||||||
|
<Box component="form" onSubmit={handleSavePeriod} sx={nestedPanelSx}>
|
||||||
|
<Stack spacing={1.4}>
|
||||||
|
<Typography variant="subtitle1" sx={{ fontWeight: 700 }}>
|
||||||
|
Ausgewählten Zeitraum bearbeiten
|
||||||
|
</Typography>
|
||||||
|
<TextField
|
||||||
|
label="Zeitraum-Name"
|
||||||
|
value={periodEditForm.name}
|
||||||
|
onChange={(event) => setPeriodEditForm((current) => ({ ...current, name: event.target.value }))}
|
||||||
|
required
|
||||||
|
fullWidth
|
||||||
|
disabled={!selectedPeriodForManagement}
|
||||||
|
/>
|
||||||
|
<Stack direction={{ xs: "column", sm: "row" }} gap={1.2}>
|
||||||
|
<TextField
|
||||||
|
label="Von"
|
||||||
|
type="date"
|
||||||
|
value={periodEditForm.startsAt}
|
||||||
|
onChange={(event) => setPeriodEditForm((current) => ({ ...current, startsAt: event.target.value }))}
|
||||||
|
InputLabelProps={{ shrink: true }}
|
||||||
|
required
|
||||||
|
fullWidth
|
||||||
|
disabled={!selectedPeriodForManagement}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
label="Bis"
|
||||||
|
type="date"
|
||||||
|
value={periodEditForm.endsAt}
|
||||||
|
onChange={(event) => setPeriodEditForm((current) => ({ ...current, endsAt: event.target.value }))}
|
||||||
|
InputLabelProps={{ shrink: true }}
|
||||||
|
required
|
||||||
|
fullWidth
|
||||||
|
disabled={!selectedPeriodForManagement}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
{"Abo-Berechnungen nutzen danach direkt den neuen Zeitraum."}
|
||||||
|
</Typography>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
variant="outlined"
|
||||||
|
startIcon={<EditRoundedIcon />}
|
||||||
|
disabled={busy || !selectedPeriodForManagement || !periodEditDirty}
|
||||||
|
>
|
||||||
|
Zeitraum speichern
|
||||||
|
</Button>
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
|
||||||
<Box component="form" onSubmit={handleCreatePeriod} sx={nestedPanelSx}>
|
<Box component="form" onSubmit={handleCreatePeriod} sx={nestedPanelSx}>
|
||||||
<Stack spacing={1.4}>
|
<Stack spacing={1.4}>
|
||||||
<Typography variant="subtitle1" sx={{ fontWeight: 700 }}>
|
<Typography variant="subtitle1" sx={{ fontWeight: 700 }}>
|
||||||
@@ -2412,5 +2515,3 @@ export function DashboardShell({
|
|||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user