import { NextResponse } from "next/server"; import { getProjectContacts, linkContactToProject, unlinkContactFromProject, setPrimaryContact, } from "@/lib/queries/contacts"; import { withAuth } from "@/lib/middleware/auth"; // GET: Get all contacts for a project async function getProjectContactsHandler(req, { params }) { try { const projectId = parseInt(params.id); const contacts = getProjectContacts(projectId); return NextResponse.json(contacts); } catch (error) { console.error("Error fetching project contacts:", error); return NextResponse.json( { error: "Failed to fetch project contacts" }, { status: 500 } ); } } // POST: Link contact to project async function linkContactHandler(req, { params }) { try { const projectId = parseInt(params.id); const { contactId, relationshipType, isPrimary } = await req.json(); const userId = req.user?.id; if (!contactId) { return NextResponse.json( { error: "Contact ID is required" }, { status: 400 } ); } linkContactToProject( projectId, contactId, relationshipType || "general", isPrimary || false, userId ); const contacts = getProjectContacts(projectId); return NextResponse.json(contacts); } catch (error) { console.error("Error linking contact to project:", error); return NextResponse.json( { error: "Failed to link contact" }, { status: 500 } ); } } // DELETE: Unlink contact from project async function unlinkContactHandler(req, { params }) { try { const projectId = parseInt(params.id); const { searchParams } = new URL(req.url); const contactId = parseInt(searchParams.get("contactId")); if (!contactId) { return NextResponse.json( { error: "Contact ID is required" }, { status: 400 } ); } unlinkContactFromProject(projectId, contactId); return NextResponse.json({ message: "Contact unlinked successfully" }); } catch (error) { console.error("Error unlinking contact from project:", error); return NextResponse.json( { error: "Failed to unlink contact" }, { status: 500 } ); } } // PATCH: Set primary contact async function setPrimaryContactHandler(req, { params }) { try { const projectId = parseInt(params.id); const { contactId } = await req.json(); if (!contactId) { return NextResponse.json( { error: "Contact ID is required" }, { status: 400 } ); } setPrimaryContact(projectId, contactId); const contacts = getProjectContacts(projectId); return NextResponse.json(contacts); } catch (error) { console.error("Error setting primary contact:", error); return NextResponse.json( { error: "Failed to set primary contact" }, { status: 500 } ); } } export const GET = withAuth(getProjectContactsHandler); export const POST = withAuth(linkContactHandler); export const DELETE = withAuth(unlinkContactHandler); export const PATCH = withAuth(setPrimaryContactHandler);