- 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>
90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { getSession } from '@/lib/auth';
|
|
import { getGHLClientForUser } from '@/lib/ghl/helpers';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ opportunityId: string }> }
|
|
) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClientForUser(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json({ error: 'GHL not configured' }, { status: 400 });
|
|
}
|
|
|
|
const { opportunityId } = await params;
|
|
|
|
try {
|
|
const opportunity = await ghl.opportunities.getById(opportunityId);
|
|
return NextResponse.json(opportunity);
|
|
} catch (error) {
|
|
return NextResponse.json({ error: 'Opportunity not found' }, { status: 404 });
|
|
}
|
|
}
|
|
|
|
const updateOpportunitySchema = z.object({
|
|
name: z.string().optional(),
|
|
pipelineStageId: z.string().optional(),
|
|
monetaryValue: z.number().optional(),
|
|
status: z.enum(['open', 'won', 'lost', 'abandoned']).optional(),
|
|
});
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ opportunityId: string }> }
|
|
) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClientForUser(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json({ error: 'GHL not configured' }, { status: 400 });
|
|
}
|
|
|
|
const { opportunityId } = await params;
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const validated = updateOpportunitySchema.parse(body);
|
|
|
|
const opportunity = await ghl.opportunities.update(opportunityId, validated);
|
|
return NextResponse.json(opportunity);
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json({ error: 'Validation failed' }, { status: 400 });
|
|
}
|
|
return NextResponse.json({ error: 'Failed to update opportunity' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ opportunityId: string }> }
|
|
) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const ghl = await getGHLClientForUser(session.user.id);
|
|
if (!ghl) {
|
|
return NextResponse.json({ error: 'GHL not configured' }, { status: 400 });
|
|
}
|
|
|
|
const { opportunityId } = await params;
|
|
|
|
try {
|
|
await ghl.opportunities.delete(opportunityId);
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
return NextResponse.json({ error: 'Failed to delete opportunity' }, { status: 500 });
|
|
}
|
|
}
|