- Implemented ContactForm component for creating and editing contacts. - Added ProjectContactSelector component to manage project-specific contacts. - Updated ProjectForm to include ProjectContactSelector for associating contacts with projects. - Enhanced Card component with a new CardTitle subcomponent for better structure. - Updated Navigation to include a link to the contacts page. - Added translations for contact-related terms in the i18n module. - Initialized contacts database schema and created necessary tables for contact management. - Developed queries for CRUD operations on contacts, including linking and unlinking contacts to projects. - Created a test script to validate contact queries against the database.
112 lines
2.8 KiB
JavaScript
112 lines
2.8 KiB
JavaScript
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);
|