feat: Add user tracking to project tasks and notes
- Implemented user tracking columns in project_tasks and notes tables. - Added created_by and assigned_to fields to project_tasks. - Introduced created_by field and is_system flag in notes. - Updated API endpoints to handle user tracking during task and note creation. - Enhanced database initialization to include new columns and indexes. - Created utility functions to fetch users for task assignment. - Updated front-end components to display user information for tasks and notes. - Added tests for project-tasks API endpoints to verify functionality.
This commit is contained in:
@@ -9,14 +9,22 @@ async function createNoteHandler(req) {
|
||||
return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
||||
}
|
||||
|
||||
db.prepare(
|
||||
try {
|
||||
db.prepare(
|
||||
`
|
||||
INSERT INTO notes (project_id, task_id, note, created_by, note_date)
|
||||
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
`
|
||||
INSERT INTO notes (project_id, task_id, note)
|
||||
VALUES (?, ?, ?)
|
||||
`
|
||||
).run(project_id || null, task_id || null, note);
|
||||
).run(project_id || null, task_id || null, note, req.user?.id || null);
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Error creating note:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create note", details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteNoteHandler(_, { params }) {
|
||||
|
||||
@@ -17,11 +17,12 @@ async function updateProjectTaskHandler(req, { params }) {
|
||||
);
|
||||
}
|
||||
|
||||
updateProjectTaskStatus(params.id, status);
|
||||
updateProjectTaskStatus(params.id, status, req.user?.id || null);
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Error updating task status:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update project task" },
|
||||
{ error: "Failed to update project task", details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -43,11 +43,20 @@ async function createProjectTaskHandler(req) {
|
||||
);
|
||||
}
|
||||
|
||||
const result = createProjectTask(data);
|
||||
// Add user tracking information from authenticated session
|
||||
const taskData = {
|
||||
...data,
|
||||
created_by: req.user?.id || null,
|
||||
// If no assigned_to is specified, default to the creator
|
||||
assigned_to: data.assigned_to || req.user?.id || null,
|
||||
};
|
||||
|
||||
const result = createProjectTask(taskData);
|
||||
return NextResponse.json({ success: true, id: result.lastInsertRowid });
|
||||
} catch (error) {
|
||||
console.error("Error creating project task:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create project task" },
|
||||
{ error: "Failed to create project task", details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
50
src/app/api/project-tasks/users/route.js
Normal file
50
src/app/api/project-tasks/users/route.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
updateProjectTaskAssignment,
|
||||
getAllUsersForTaskAssignment,
|
||||
} from "@/lib/queries/tasks";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withUserAuth, withReadAuth } from "@/lib/middleware/auth";
|
||||
|
||||
// GET: Get all users for task assignment
|
||||
async function getUsersForTaskAssignmentHandler(req) {
|
||||
try {
|
||||
const users = getAllUsersForTaskAssignment();
|
||||
return NextResponse.json(users);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch users" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST: Update task assignment
|
||||
async function updateTaskAssignmentHandler(req) {
|
||||
try {
|
||||
const { taskId, assignedToUserId } = await req.json();
|
||||
|
||||
if (!taskId) {
|
||||
return NextResponse.json(
|
||||
{ error: "taskId is required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const result = updateProjectTaskAssignment(taskId, assignedToUserId);
|
||||
|
||||
if (result.changes === 0) {
|
||||
return NextResponse.json({ error: "Task not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update task assignment" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Protected routes
|
||||
export const GET = withReadAuth(getUsersForTaskAssignmentHandler);
|
||||
export const POST = withUserAuth(updateTaskAssignmentHandler);
|
||||
@@ -38,7 +38,7 @@ async function addTaskNoteHandler(req) {
|
||||
);
|
||||
}
|
||||
|
||||
addNoteToTask(task_id, note, is_system);
|
||||
addNoteToTask(task_id, note, is_system, req.user?.id || null);
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Error adding task note:", error);
|
||||
|
||||
Reference in New Issue
Block a user