feat: Implement task editing functionality with validation and user assignment

This commit is contained in:
Chop
2025-07-10 23:35:17 +02:00
parent 92f458e59b
commit 51d37fc65a
3 changed files with 372 additions and 26 deletions

View File

@@ -192,8 +192,13 @@ export function updateProjectTaskStatus(taskId, status, userId = null) {
// Delete a project task
export function deleteProjectTask(taskId) {
const stmt = db.prepare("DELETE FROM project_tasks WHERE id = ?");
return stmt.run(taskId);
// First delete all related task notes
const deleteNotesStmt = db.prepare("DELETE FROM notes WHERE task_id = ?");
deleteNotesStmt.run(taskId);
// Then delete the task itself
const deleteTaskStmt = db.prepare("DELETE FROM project_tasks WHERE id = ?");
return deleteTaskStmt.run(taskId);
}
// Get project tasks assigned to a specific user
@@ -291,3 +296,107 @@ export function getAllUsersForTaskAssignment() {
)
.all();
}
// Update project task (general update for edit modal)
export function updateProjectTask(taskId, updates, userId = null) {
// Get current task for logging
const getCurrentTask = db.prepare(`
SELECT
pt.*,
COALESCE(pt.custom_task_name, t.name) as task_name
FROM project_tasks pt
LEFT JOIN tasks t ON pt.task_template_id = t.task_id
WHERE pt.id = ?
`);
const currentTask = getCurrentTask.get(taskId);
if (!currentTask) {
throw new Error(`Task with ID ${taskId} not found`);
}
// Build dynamic update query
const fields = [];
const values = [];
if (updates.priority !== undefined) {
fields.push("priority = ?");
values.push(updates.priority);
}
if (updates.status !== undefined) {
fields.push("status = ?");
values.push(updates.status);
// Handle status-specific timestamp updates
if (currentTask.status === "pending" && updates.status === "in_progress") {
fields.push("date_started = CURRENT_TIMESTAMP");
} else if (updates.status === "completed") {
fields.push("date_completed = CURRENT_TIMESTAMP");
}
}
if (updates.assigned_to !== undefined) {
fields.push("assigned_to = ?");
values.push(updates.assigned_to || null);
}
if (updates.date_started !== undefined) {
fields.push("date_started = ?");
values.push(updates.date_started || null);
}
// Always update the updated_at timestamp
fields.push("updated_at = CURRENT_TIMESTAMP");
values.push(taskId);
const stmt = db.prepare(`
UPDATE project_tasks
SET ${fields.join(", ")}
WHERE id = ?
`);
const result = stmt.run(...values);
// Log the update
if (userId) {
const changes = [];
if (
updates.priority !== undefined &&
updates.priority !== currentTask.priority
) {
changes.push(
`Priority: ${currentTask.priority || "None"}${
updates.priority || "None"
}`
);
}
if (updates.status !== undefined && updates.status !== currentTask.status) {
changes.push(
`Status: ${currentTask.status || "None"}${updates.status || "None"}`
);
}
if (
updates.assigned_to !== undefined &&
updates.assigned_to !== currentTask.assigned_to
) {
changes.push(`Assignment updated`);
}
if (
updates.date_started !== undefined &&
updates.date_started !== currentTask.date_started
) {
changes.push(
`Date started: ${currentTask.date_started || "None"}${
updates.date_started || "None"
}`
);
}
if (changes.length > 0) {
const logMessage = `Task updated: ${changes.join(", ")}`;
addNoteToTask(taskId, logMessage, true, userId);
}
}
return result;
}