feat: Implement user tracking in projects
- Added user tracking features to the projects module, including: - Database schema updates to track project creator and assignee. - API enhancements for user management and project filtering by user. - UI components for user assignment in project forms and listings. - New query functions for retrieving users and filtering projects. - Security integration with role-based access and authentication requirements. chore: Create utility scripts for database checks and project testing - Added scripts to check the structure of the projects table. - Created tests for project creation and user tracking functionality. - Implemented API tests to verify project retrieval and user assignment. fix: Update project creation and update functions to include user tracking - Modified createProject and updateProject functions to handle user IDs for creator and assignee. - Ensured that project updates reflect the correct user assignments and timestamps.
This commit is contained in:
@@ -3,22 +3,41 @@ import {
|
||||
updateProject,
|
||||
deleteProject,
|
||||
} from "@/lib/queries/projects";
|
||||
import initializeDatabase from "@/lib/init-db";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withReadAuth, withUserAuth } from "@/lib/middleware/auth";
|
||||
|
||||
async function getProjectHandler(_, { params }) {
|
||||
const project = getProjectById(params.id);
|
||||
// Make sure the DB is initialized before queries run
|
||||
initializeDatabase();
|
||||
|
||||
async function getProjectHandler(req, { params }) {
|
||||
const { id } = await params;
|
||||
const project = getProjectById(parseInt(id));
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: "Project not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(project);
|
||||
}
|
||||
|
||||
async function updateProjectHandler(req, { params }) {
|
||||
const { id } = await params;
|
||||
const data = await req.json();
|
||||
updateProject(params.id, data);
|
||||
return NextResponse.json({ success: true });
|
||||
|
||||
// Get user ID from authenticated request
|
||||
const userId = req.user?.id;
|
||||
|
||||
updateProject(parseInt(id), data, userId);
|
||||
|
||||
// Return the updated project
|
||||
const updatedProject = getProjectById(parseInt(id));
|
||||
return NextResponse.json(updatedProject);
|
||||
}
|
||||
|
||||
async function deleteProjectHandler(_, { params }) {
|
||||
deleteProject(params.id);
|
||||
async function deleteProjectHandler(req, { params }) {
|
||||
const { id } = await params;
|
||||
deleteProject(parseInt(id));
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { getAllProjects, createProject } from "@/lib/queries/projects";
|
||||
import {
|
||||
getAllProjects,
|
||||
createProject,
|
||||
getAllUsersForAssignment,
|
||||
} from "@/lib/queries/projects";
|
||||
import initializeDatabase from "@/lib/init-db";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withReadAuth, withUserAuth } from "@/lib/middleware/auth";
|
||||
@@ -9,15 +13,37 @@ initializeDatabase();
|
||||
async function getProjectsHandler(req) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const contractId = searchParams.get("contract_id");
|
||||
const assignedTo = searchParams.get("assigned_to");
|
||||
const createdBy = searchParams.get("created_by");
|
||||
|
||||
let projects;
|
||||
|
||||
if (assignedTo) {
|
||||
const { getProjectsByAssignedUser } = await import(
|
||||
"@/lib/queries/projects"
|
||||
);
|
||||
projects = getProjectsByAssignedUser(assignedTo);
|
||||
} else if (createdBy) {
|
||||
const { getProjectsByCreator } = await import("@/lib/queries/projects");
|
||||
projects = getProjectsByCreator(createdBy);
|
||||
} else {
|
||||
projects = getAllProjects(contractId);
|
||||
}
|
||||
|
||||
const projects = getAllProjects(contractId);
|
||||
return NextResponse.json(projects);
|
||||
}
|
||||
|
||||
async function createProjectHandler(req) {
|
||||
const data = await req.json();
|
||||
createProject(data);
|
||||
return NextResponse.json({ success: true });
|
||||
|
||||
// Get user ID from authenticated request
|
||||
const userId = req.user?.id;
|
||||
|
||||
const result = createProject(data, userId);
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
projectId: result.lastInsertRowid,
|
||||
});
|
||||
}
|
||||
|
||||
// Protected routes - require authentication
|
||||
|
||||
33
src/app/api/projects/users/route.js
Normal file
33
src/app/api/projects/users/route.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import {
|
||||
getAllUsersForAssignment,
|
||||
updateProjectAssignment,
|
||||
} from "@/lib/queries/projects";
|
||||
import initializeDatabase from "@/lib/init-db";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withUserAuth } from "@/lib/middleware/auth";
|
||||
|
||||
// Make sure the DB is initialized before queries run
|
||||
initializeDatabase();
|
||||
|
||||
async function getUsersHandler(req) {
|
||||
const users = getAllUsersForAssignment();
|
||||
return NextResponse.json(users);
|
||||
}
|
||||
|
||||
async function updateAssignmentHandler(req) {
|
||||
const { projectId, assignedToUserId } = await req.json();
|
||||
|
||||
if (!projectId) {
|
||||
return NextResponse.json(
|
||||
{ error: "Project ID is required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
updateProjectAssignment(projectId, assignedToUserId);
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
||||
// Protected routes - require authentication
|
||||
export const GET = withUserAuth(getUsersHandler);
|
||||
export const POST = withUserAuth(updateAssignmentHandler);
|
||||
@@ -1,17 +1,52 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useParams } from "next/navigation";
|
||||
import ProjectForm from "@/components/ProjectForm";
|
||||
import PageContainer from "@/components/ui/PageContainer";
|
||||
import PageHeader from "@/components/ui/PageHeader";
|
||||
import Button from "@/components/ui/Button";
|
||||
import Link from "next/link";
|
||||
import { LoadingState } from "@/components/ui/States";
|
||||
|
||||
export default async function EditProjectPage({ params }) {
|
||||
const { id } = await params;
|
||||
const res = await fetch(`http://localhost:3000/api/projects/${id}`, {
|
||||
cache: "no-store",
|
||||
});
|
||||
const project = await res.json();
|
||||
export default function EditProjectPage() {
|
||||
const params = useParams();
|
||||
const id = params.id;
|
||||
const [project, setProject] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
if (!project) {
|
||||
useEffect(() => {
|
||||
const fetchProject = async () => {
|
||||
try {
|
||||
const res = await fetch(`/api/projects/${id}`);
|
||||
if (res.ok) {
|
||||
const projectData = await res.json();
|
||||
setProject(projectData);
|
||||
} else {
|
||||
setError("Project not found");
|
||||
}
|
||||
} catch (err) {
|
||||
setError("Failed to load project");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (id) {
|
||||
fetchProject();
|
||||
}
|
||||
}, [id]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<PageContainer>
|
||||
<LoadingState />
|
||||
</PageContainer>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !project) {
|
||||
return (
|
||||
<PageContainer>
|
||||
<div className="text-center py-12">
|
||||
|
||||
@@ -195,7 +195,13 @@ export default function ProjectListPage() {
|
||||
</th>
|
||||
<th className="text-left px-2 py-3 font-semibold text-xs text-gray-700 w-24">
|
||||
Status
|
||||
</th>{" "}
|
||||
</th>
|
||||
<th className="text-left px-2 py-3 font-semibold text-xs text-gray-700 w-24">
|
||||
Created By
|
||||
</th>
|
||||
<th className="text-left px-2 py-3 font-semibold text-xs text-gray-700 w-24">
|
||||
Assigned To
|
||||
</th>
|
||||
<th className="text-left px-2 py-3 font-semibold text-xs text-gray-700 w-20">
|
||||
Actions
|
||||
</th>
|
||||
@@ -275,6 +281,18 @@ export default function ProjectListPage() {
|
||||
? "Zakończony"
|
||||
: "-"}
|
||||
</td>
|
||||
<td
|
||||
className="px-2 py-3 text-xs text-gray-600 truncate"
|
||||
title={project.created_by_name || "Unknown"}
|
||||
>
|
||||
{project.created_by_name || "Unknown"}
|
||||
</td>
|
||||
<td
|
||||
className="px-2 py-3 text-xs text-gray-600 truncate"
|
||||
title={project.assigned_to_name || "Unassigned"}
|
||||
>
|
||||
{project.assigned_to_name || "Unassigned"}
|
||||
</td>
|
||||
<td className="px-2 py-3">
|
||||
<Link href={`/projects/${project.project_id}`}>
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user