Build errors fixed: - Fixed session field access (session.sub instead of session.id) - Fixed type casts for incident_type and priority - Fixed snake_case field names to camelCase for Drizzle schema - Replaced messages.create with chat.completions.create for DeepSeek - Updated DeepSeek client to accept apiKey parameter - Removed unused imports across multiple files Route now accessible at /bulk-import. Redirects unauthenticated users to /login?redirect=%2Fbulk-import. Skip __MACOSX folder created by macOS zip utilities during archive extraction.
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { requireAdmin } from '@/lib/auth/require-admin'
|
|
import { createBulkIncidents, type ExtractedIncidentMetadata } from '@/lib/incidents/bulk-import'
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const { session } = await requireAdmin()
|
|
if (!session?.sub) return new NextResponse('Unauthorized', { status: 401 })
|
|
const userId = session.sub
|
|
|
|
try {
|
|
const body = await request.json()
|
|
const { folders } = body as {
|
|
folders: Array<{
|
|
id: string
|
|
folder_path?: string
|
|
description: string
|
|
date: string
|
|
location: { site: string; zone: string }
|
|
incident_type?: string
|
|
priority?: string
|
|
}>
|
|
}
|
|
|
|
const metadata: ExtractedIncidentMetadata[] = folders.map((f) => ({
|
|
description: f.description,
|
|
date: f.date,
|
|
location: f.location,
|
|
incident_type: f.incident_type as 'env' | 'asset' | 'security' | 'fire' | undefined,
|
|
priority: f.priority as 'low' | 'med' | 'high' | undefined
|
|
}))
|
|
|
|
// Create incidents with user ID
|
|
const created = await createBulkIncidents(metadata, userId, [])
|
|
|
|
return NextResponse.json({
|
|
status: 'success',
|
|
created_incidents: created,
|
|
total_files_imported: 0
|
|
})
|
|
} catch (error) {
|
|
console.error('Batch creation failed:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Batch creation failed', details: String(error) },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|