diff --git a/app/api/projects/[id]/route.ts b/app/api/projects/[id]/route.ts index ed1d697..b5d6d66 100644 --- a/app/api/projects/[id]/route.ts +++ b/app/api/projects/[id]/route.ts @@ -15,7 +15,7 @@ export async function PUT( if (session?.user.role !== 'admin') { return new Response(JSON.stringify({ error: 'User not admin' }), { headers: { 'Content-Type': 'application/json' }, - status: 400, // Internal Server Error + status: 403, // Forbidden }); } const body = await request.json(); @@ -46,18 +46,41 @@ export async function PUT( }) ); + // Identify users who have been added to the project + const addedUsers = updatedProject.assignedUsers.filter( + (userId: string) => !currentProject.assignedUsers.includes(userId) + ); + + // Update the assignedProjects array for each added user + await Promise.all( + addedUsers.map(async (userId: string) => { + await User.findByIdAndUpdate( + userId, + { + $push: { assignedProjects: updatedProject._id }, + }, + { new: true } + ); + }) + ); + return new Response(JSON.stringify({ project: updatedProject }), { headers: { 'Content-Type': 'application/json' }, status: 200, // OK }); } catch (error) { - return new Response(JSON.stringify({ error: 'Failed to update project' }), { - headers: { 'Content-Type': 'application/json' }, - status: 500, // Internal Server Error - }); + console.error(error); // Log the actual error to the server logs + return new Response( + JSON.stringify({ error: `Failed to update project: ${error.message}` }), + { + headers: { 'Content-Type': 'application/json' }, + status: 500, // Internal Server Error + } + ); } } + export async function DELETE( request: NextRequest, // Assuming NextApiRequest is imported from 'next' { params }: { params: { id: string } } diff --git a/app/create-project/CreateProjectComponent.tsx b/app/create-project/CreateProjectComponent.tsx index 3a4c23a..de37ab7 100644 --- a/app/create-project/CreateProjectComponent.tsx +++ b/app/create-project/CreateProjectComponent.tsx @@ -64,81 +64,82 @@ const CreateProjectComponent: React.FC = ({ setShowAllUsers(!showAllUsers); }; -return ( -
-

Create New Project

-
-
- - setName(e.target.value)} - className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" - /> -
-
- -