- 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>
97 lines
3.5 KiB
TypeScript
97 lines
3.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { prisma } from '@/lib/db';
|
|
import { getSession } from '@/lib/auth';
|
|
import { adminTaggingService } from '@/lib/ghl';
|
|
|
|
const onboardingSchema = z.object({
|
|
yearsInBusiness: z.string().optional(),
|
|
gciLast12Months: z.string().optional(),
|
|
usingOtherCrm: z.boolean().optional(),
|
|
currentCrmName: z.string().optional(),
|
|
crmPainPoints: z.array(z.string()).optional(),
|
|
goalsSelected: z.array(z.string()).optional(),
|
|
hasLeadSource: z.boolean().optional(),
|
|
leadSourceName: z.string().optional(),
|
|
wantsMoreLeads: z.boolean().optional(),
|
|
leadTypesDesired: z.array(z.string()).optional(),
|
|
leadsPerMonthTarget: z.string().optional(),
|
|
channelsSelected: z.array(z.string()).optional(),
|
|
systemsToConnect: z.array(z.string()).optional(),
|
|
});
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const onboarding = await prisma.onboardingData.findUnique({
|
|
where: { userId: session.user.id },
|
|
});
|
|
|
|
return NextResponse.json({ onboarding });
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const session = await getSession();
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const validated = onboardingSchema.parse(body);
|
|
|
|
// Convert arrays to JSON strings for Prisma (schema stores arrays as JSON strings)
|
|
const prismaData = {
|
|
...validated,
|
|
crmPainPoints: validated.crmPainPoints ? JSON.stringify(validated.crmPainPoints) : undefined,
|
|
goalsSelected: validated.goalsSelected ? JSON.stringify(validated.goalsSelected) : undefined,
|
|
leadTypesDesired: validated.leadTypesDesired ? JSON.stringify(validated.leadTypesDesired) : undefined,
|
|
channelsSelected: validated.channelsSelected ? JSON.stringify(validated.channelsSelected) : undefined,
|
|
systemsToConnect: validated.systemsToConnect ? JSON.stringify(validated.systemsToConnect) : undefined,
|
|
};
|
|
|
|
// Upsert onboarding data
|
|
const onboarding = await prisma.onboardingData.upsert({
|
|
where: { userId: session.user.id },
|
|
update: prismaData,
|
|
create: {
|
|
userId: session.user.id,
|
|
...prismaData,
|
|
},
|
|
});
|
|
|
|
// Update user profile with brokerage if provided
|
|
if (body.brokerageName) {
|
|
await prisma.user.update({
|
|
where: { id: session.user.id },
|
|
data: { brokerage: body.brokerageName },
|
|
});
|
|
}
|
|
|
|
// Tag user in owner's account for onboarding complete
|
|
adminTaggingService.tagOnboardingComplete(session.user.id)
|
|
.catch(err => console.error('Failed to tag onboarding complete:', err));
|
|
|
|
// If high GCI, tag them
|
|
const gci = validated.gciLast12Months || '';
|
|
if (gci.includes('100') || gci.includes('250')) {
|
|
adminTaggingService.tagHighGCI(session.user.id, gci)
|
|
.catch(err => console.error('Failed to tag high GCI:', err));
|
|
}
|
|
|
|
// Sync all user data to owner
|
|
adminTaggingService.syncUserToOwner(session.user.id)
|
|
.catch(err => console.error('Failed to sync user to owner:', err));
|
|
|
|
return NextResponse.json({ success: true, onboarding });
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json({ error: 'Validation failed' }, { status: 400 });
|
|
}
|
|
return NextResponse.json({ error: 'Failed to save onboarding' }, { status: 500 });
|
|
}
|
|
}
|