- 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.
41 lines
1000 B
JavaScript
41 lines
1000 B
JavaScript
import { NextResponse } from "next/server";
|
|
import db from "@/lib/db";
|
|
import { withAuth } from "@/lib/middleware/auth";
|
|
|
|
// GET /api/contacts/[id]/projects - Get all projects linked to a contact
|
|
export const GET = withAuth(async (request, { params }) => {
|
|
try {
|
|
const contactId = params.id;
|
|
|
|
// Get all projects linked to this contact with relationship details
|
|
const projects = db
|
|
.prepare(
|
|
`
|
|
SELECT
|
|
p.project_id,
|
|
p.project_name,
|
|
p.project_status,
|
|
pc.relationship_type,
|
|
pc.is_primary,
|
|
pc.added_at
|
|
FROM projects p
|
|
INNER JOIN project_contacts pc ON p.project_id = pc.project_id
|
|
WHERE pc.contact_id = ?
|
|
ORDER BY pc.is_primary DESC, p.project_name ASC
|
|
`
|
|
)
|
|
.all(contactId);
|
|
|
|
return NextResponse.json({
|
|
projects,
|
|
count: projects.length,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching contact projects:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch projects" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
});
|