← back to Goodquestion
src/app/api/analyze/route.ts
68 lines
import { NextRequest, NextResponse } from 'next/server'
import {
swotWorkflow,
historicalWorkflow,
startupCostsWorkflow,
marketAnalysisWorkflow,
actionPlanWorkflow
} from '@/lib/ai-workflows'
import { z } from 'zod'
// Request validation schema
const AnalysisRequestSchema = z.object({
businessName: z.string().min(1),
businessType: z.string().min(1),
location: z.string().min(1),
radiusMiles: z.number().min(1).max(100).optional().default(10),
})
export async function POST(request: NextRequest) {
try {
// Parse and validate request body
const body = await request.json()
const validatedData = AnalysisRequestSchema.parse(body)
// OPTIMIZATION: Only generate SWOT analysis by default
// Other sections will be loaded on-demand
const swotAnalysis = await swotWorkflow.generateAnalysis(
validatedData.businessName,
validatedData.businessType,
validatedData.location
)
return NextResponse.json({
success: true,
data: {
swotAnalysis,
// Other sections will be loaded via separate API calls
historicalContext: null,
startupCosts: null,
marketAnalysis: null,
actionPlan: null,
},
})
} catch (error) {
console.error('Analysis generation error:', error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{
success: false,
error: 'Invalid request data',
details: error.errors,
},
{ status: 400 }
)
}
return NextResponse.json(
{
success: false,
error: 'Failed to generate analysis',
message: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
}
}