- Build complete Next.js CRM for commercial real estate - Add authentication with JWT sessions and role-based access - Add GoHighLevel API integration for contacts, conversations, opportunities - Add AI-powered Control Center with tool calling - Add Setup page with onboarding checklist (/setup) - Add sidebar navigation with Setup menu item - Fix type errors in onboarding API, GHL services, and control center tools - Add Prisma schema with SQLite for local development - Add UI components with clay morphism design system Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
116 lines
2.9 KiB
TypeScript
116 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { getSession } from '@/lib/auth';
|
|
import { getGHLClient } from '@/lib/api';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ contactId: string }> }
|
|
) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClient(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json(
|
|
{ error: 'GHL not configured' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const { contactId } = await params;
|
|
const contact = await ghl.contacts.getById(contactId);
|
|
return NextResponse.json(contact);
|
|
} catch (error) {
|
|
console.error('Failed to get contact:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Contact not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
}
|
|
|
|
const updateContactSchema = z.object({
|
|
firstName: z.string().optional(),
|
|
lastName: z.string().optional(),
|
|
email: z.string().email().optional(),
|
|
phone: z.string().optional(),
|
|
tags: z.array(z.string()).optional(),
|
|
customFields: z.array(z.object({
|
|
key: z.string(),
|
|
value: z.any(),
|
|
})).optional(),
|
|
});
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ contactId: string }> }
|
|
) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClient(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json(
|
|
{ error: 'GHL not configured' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const { contactId } = await params;
|
|
const body = await request.json();
|
|
const validated = updateContactSchema.parse(body);
|
|
|
|
const contact = await ghl.contacts.update(contactId, validated);
|
|
return NextResponse.json(contact);
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json(
|
|
{ error: 'Validation failed', details: error.issues },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
console.error('Failed to update contact:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to update contact' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ contactId: string }> }
|
|
) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClient(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json(
|
|
{ error: 'GHL not configured' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const { contactId } = await params;
|
|
await ghl.contacts.delete(contactId);
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Failed to delete contact:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to delete contact' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|