Files
nebula/src/app/api/projects/[id]/route.ts
T
florian.wildeandClaude Opus 5 5b40d5e4ff feat(api): progressFromTasks im Projekt-Endpunkt
Der Schalter laesst sich ueber PATCH /api/projects/:id setzen. Beim
Einschalten wird der abgeleitete Wert sofort berechnet, damit er nicht bis
zur naechsten Aufgabenaenderung falsch steht.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 09:30:04 +02:00

129 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextResponse } from "next/server";
import { requireUserApi } from "@/lib/auth/guard";
import { prisma } from "@/lib/db";
import { parseRepoUrl } from "@/lib/git";
import { projectDetailInclude, projectListInclude, uniqueSlug } from "@/lib/projects";
import { jsonError, readJsonBody } from "@/lib/security/request";
import { refreshDerivedProgress } from "@/lib/tasks";
import { formatZodError, projectUpdateSchema } from "@/lib/validation";
export const dynamic = "force-dynamic";
type Params = { params: Promise<{ id: string }> };
export async function GET(_request: Request, { params }: Params) {
const auth = await requireUserApi();
if (!auth.ok) return auth.response;
const { id } = await params;
const project = await prisma.project.findFirst({
where: { id, ownerId: auth.user.id },
include: projectDetailInclude,
});
// Fremde Projekte werden wie nicht vorhandene behandelt – die Antwort
// verraet also nicht, dass es die ID gibt.
if (!project) return jsonError("Projekt nicht gefunden", 404);
return NextResponse.json({ project });
}
export async function PATCH(request: Request, { params }: Params) {
const auth = await requireUserApi();
if (!auth.ok) return auth.response;
const { id } = await params;
const body = await readJsonBody(request);
if (!body.ok) return body.response;
const parsed = projectUpdateSchema.safeParse(body.data);
if (!parsed.success) {
return NextResponse.json(
{ error: "Validierung fehlgeschlagen", fields: formatZodError(parsed.error) },
{ status: 422 },
);
}
const existing = await prisma.project.findFirst({
where: { id, ownerId: auth.user.id },
});
if (!existing) return jsonError("Projekt nicht gefunden", 404);
const data = parsed.data;
if (data.connectionId) {
const connection = await prisma.gitConnection.findFirst({
where: { id: data.connectionId, ownerId: auth.user.id },
select: { id: true },
});
if (!connection) return jsonError("Git-Verbindung nicht gefunden.", 404);
}
const update: Record<string, unknown> = {};
if (data.name !== undefined && data.name !== existing.name) {
update.name = data.name;
update.slug = await uniqueSlug(data.name, auth.user.id, id);
}
for (const key of [
"summary",
"description",
"status",
"priority",
"progress",
"accent",
"tags",
"favorite",
"autoSync",
"progressFromTasks",
"connectionId",
] as const) {
if (data[key] !== undefined) update[key] = data[key];
}
if (data.repoUrl !== undefined) {
update.repoUrl = data.repoUrl;
const parsedUrl = data.repoUrl ? parseRepoUrl(data.repoUrl) : null;
if (parsedUrl) {
update.repoOwner = data.repoOwner ?? parsedUrl.owner;
update.repoName = data.repoName ?? parsedUrl.name;
} else if (!data.repoUrl) {
update.repoOwner = null;
update.repoName = null;
}
}
if (data.repoOwner !== undefined) update.repoOwner = data.repoOwner;
if (data.repoName !== undefined) update.repoName = data.repoName;
await prisma.project.update({ where: { id }, data: update });
// Wird die Ableitung eingeschaltet, soll der Wert sofort stimmen und nicht
// erst beim naechsten Aufgabenwechsel.
if (data.progressFromTasks === true) {
await refreshDerivedProgress(id);
}
const project = await prisma.project.findUniqueOrThrow({
where: { id },
include: projectListInclude,
});
return NextResponse.json({ project });
}
export async function DELETE(_request: Request, { params }: Params) {
const auth = await requireUserApi();
if (!auth.ok) return auth.response;
const { id } = await params;
// deleteMany mit Besitzerbedingung: trifft fremde Projekte gar nicht erst.
const result = await prisma.project.deleteMany({
where: { id, ownerId: auth.user.id },
});
if (result.count === 0) return jsonError("Projekt nicht gefunden", 404);
return NextResponse.json({ ok: true });
}