95 lines
2.4 KiB
JavaScript
95 lines
2.4 KiB
JavaScript
import db from "@/lib/db";
|
|
import { NextResponse } from "next/server";
|
|
import { withReadAuth, withUserAuth } from "@/lib/middleware/auth";
|
|
|
|
// GET: Get a specific task template
|
|
async function getTaskHandler(req, { params }) {
|
|
const { id } = await params;
|
|
try {
|
|
const template = db
|
|
.prepare("SELECT * FROM tasks WHERE task_id = ? AND is_standard = 1")
|
|
.get(id);
|
|
|
|
if (!template) {
|
|
return NextResponse.json(
|
|
{ error: "Task template not found" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(template);
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch task template" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// PUT: Update a task template
|
|
async function updateTaskHandler(req, { params }) {
|
|
const { id } = await params;
|
|
try {
|
|
const { name, max_wait_days, description, task_category } = await req.json();
|
|
|
|
if (!name) {
|
|
return NextResponse.json({ error: "Name is required" }, { status: 400 });
|
|
}
|
|
|
|
if (task_category && !['design', 'construction'].includes(task_category)) {
|
|
return NextResponse.json({ error: "Invalid task_category (must be design or construction)" }, { status: 400 });
|
|
}
|
|
|
|
const result = db
|
|
.prepare(
|
|
`UPDATE tasks
|
|
SET name = ?, max_wait_days = ?, description = ?, task_category = ?
|
|
WHERE task_id = ? AND is_standard = 1`
|
|
)
|
|
.run(name, max_wait_days || 0, description || null, task_category, id);
|
|
|
|
if (result.changes === 0) {
|
|
return NextResponse.json(
|
|
{ error: "Task template not found" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: "Failed to update task template" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// DELETE: Delete a task template
|
|
async function deleteTaskHandler(req, { params }) {
|
|
const { id } = await params;
|
|
try {
|
|
const result = db
|
|
.prepare("DELETE FROM tasks WHERE task_id = ? AND is_standard = 1")
|
|
.run(id);
|
|
|
|
if (result.changes === 0) {
|
|
return NextResponse.json(
|
|
{ error: "Task template not found" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: "Failed to delete task template" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// Protected routes - require authentication
|
|
export const GET = withReadAuth(getTaskHandler);
|
|
export const PUT = withUserAuth(updateTaskHandler);
|
|
export const DELETE = withUserAuth(deleteTaskHandler);
|