36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
import { updateTask, validateTaskPayload } from "@/lib/tasks";
|
|
|
|
export async function PATCH(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
const { id } = await params;
|
|
const numericId = Number(id);
|
|
if (!Number.isInteger(numericId) || numericId <= 0) {
|
|
return NextResponse.json({ error: "invalid_task_id" }, { status: 400 });
|
|
}
|
|
|
|
const payload = (await request.json()) as Record<string, unknown>;
|
|
const errors = validateTaskPayload(payload as never, true);
|
|
if (errors.length > 0) {
|
|
return NextResponse.json({ error: "validation_error", details: errors }, { status: 400 });
|
|
}
|
|
|
|
const task = await updateTask(numericId, {
|
|
title: typeof payload.title === "string" ? payload.title : undefined,
|
|
description: typeof payload.description === "string" ? payload.description : undefined,
|
|
assignee: typeof payload.assignee === "string" ? payload.assignee : undefined,
|
|
priority: payload.priority as never,
|
|
status: payload.status as never,
|
|
tags: Array.isArray(payload.tags) ? payload.tags.filter((tag) => typeof tag === "string") : undefined,
|
|
});
|
|
|
|
if (!task) {
|
|
return NextResponse.json({ error: "task_not_found" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(task);
|
|
}
|