From e136c9f0e88cb31953b7639c21abc49256c75ded Mon Sep 17 00:00:00 2001 From: Chop <28534054+RChopin@users.noreply.github.com> Date: Tue, 3 Jun 2025 21:28:33 +0200 Subject: [PATCH] feat: Add migration functionality to update template-based tasks and set custom_max_wait_days to NULL --- src/app/api/project-tasks/route.js | 63 ++++++++++++++++++++++++++++++ src/lib/queries/tasks.js | 6 +-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/app/api/project-tasks/route.js b/src/app/api/project-tasks/route.js index 784279d..fd3c93b 100644 --- a/src/app/api/project-tasks/route.js +++ b/src/app/api/project-tasks/route.js @@ -4,6 +4,7 @@ import { createProjectTask, } from "@/lib/queries/tasks"; import { NextResponse } from "next/server"; +import db from "@/lib/db"; // GET: Get all project tasks or task templates based on query params export async function GET(req) { @@ -50,3 +51,65 @@ export async function POST(req) { ); } } + +// PATCH: Handle special operations like migration +export async function PATCH(req) { + try { + const data = await req.json(); + + if (data.action === "fix_max_wait_days") { + console.log( + "Running migration to fix template-based tasks max_wait_days..." + ); + + // Update existing template-based tasks to set custom_max_wait_days to NULL + const result = db + .prepare( + ` + UPDATE project_tasks + SET custom_max_wait_days = NULL + WHERE task_template_id IS NOT NULL + AND custom_max_wait_days = 0 + ` + ) + .run(); + + console.log( + `Migration completed. Updated ${result.changes} template-based tasks.` + ); + + // Return updated tasks to verify + const updatedTasks = db + .prepare( + ` + SELECT + pt.id, + pt.task_template_id, + pt.custom_max_wait_days, + t.max_wait_days as template_max, + t.name as template_name, + COALESCE(pt.custom_max_wait_days, t.max_wait_days) as calculated_max + FROM project_tasks pt + LEFT JOIN tasks t ON pt.task_template_id = t.task_id + WHERE pt.task_template_id IS NOT NULL + ` + ) + .all(); + + return NextResponse.json({ + success: true, + message: `Updated ${result.changes} template-based tasks`, + updatedTasksCount: result.changes, + updatedTasks, + }); + } + + return NextResponse.json({ error: "Unknown action" }, { status: 400 }); + } catch (error) { + console.error("Migration failed:", error); + return NextResponse.json( + { error: "Migration failed", details: error.message }, + { status: 500 } + ); + } +} diff --git a/src/lib/queries/tasks.js b/src/lib/queries/tasks.js index 6640f6a..5914674 100644 --- a/src/lib/queries/tasks.js +++ b/src/lib/queries/tasks.js @@ -60,10 +60,10 @@ export function getProjectTasks(projectId) { // Create a new project task export function createProjectTask(data) { if (data.task_template_id) { - // Creating from template + // Creating from template - explicitly set custom_max_wait_days to NULL so COALESCE uses template value const stmt = db.prepare(` - INSERT INTO project_tasks (project_id, task_template_id, status, priority) - VALUES (?, ?, ?, ?) + INSERT INTO project_tasks (project_id, task_template_id, custom_max_wait_days, status, priority) + VALUES (?, ?, NULL, ?, ?) `); return stmt.run( data.project_id,