- Updated all packages to latest versions (React 19, Next.js 14.2.32) - Replaced sqlite3 with pg and drizzle-orm dependencies - Created complete PostgreSQL schema with relationships and indexes - Migrated all API endpoints from SQLite to Drizzle queries - Added database seeding with sample data - Updated authentication to use bcrypt instead of pbkdf2 - Configured connection pooling for PostgreSQL - Updated app version to 1.0.0 - All endpoints tested and working correctly
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next"
|
|
import { db, schema } from "../../../lib/db/connection"
|
|
import { eq } from "drizzle-orm"
|
|
|
|
interface BulkImportItem {
|
|
domain: string
|
|
riskLevel: number
|
|
categories: string[]
|
|
description?: string
|
|
}
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method !== "POST") return res.status(405).json({ error: "Method not allowed" })
|
|
|
|
const { sources } = req.body as { sources: BulkImportItem[] }
|
|
|
|
if (!sources || !Array.isArray(sources)) {
|
|
return res.status(400).json({ error: "Sources array required" })
|
|
}
|
|
|
|
try {
|
|
let imported = 0
|
|
let skipped = 0
|
|
|
|
for (const source of sources) {
|
|
if (!source.domain || !source.riskLevel) {
|
|
skipped++
|
|
continue
|
|
}
|
|
|
|
// Check if source already exists
|
|
const existing = await db.select()
|
|
.from(schema.sources)
|
|
.where(eq(schema.sources.domain, source.domain))
|
|
.limit(1)
|
|
|
|
if (existing.length > 0) {
|
|
skipped++
|
|
continue
|
|
}
|
|
|
|
// Insert new source
|
|
try {
|
|
const url = `https://${source.domain}`
|
|
await db.insert(schema.sources).values({
|
|
url,
|
|
domain: source.domain,
|
|
title: source.domain,
|
|
description: source.description || `Imported source: ${source.domain}`,
|
|
type: 'website',
|
|
status: 'pending',
|
|
riskLevel: source.riskLevel,
|
|
language: 'sk',
|
|
reportedBy: 'bulk-import'
|
|
})
|
|
imported++
|
|
} catch (error) {
|
|
console.error('Failed to import source:', source.domain, error)
|
|
skipped++
|
|
}
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
imported,
|
|
skipped,
|
|
message: `Imported ${imported} sources, skipped ${skipped}`
|
|
})
|
|
|
|
} catch (error: any) {
|
|
console.error('Bulk import error:', error)
|
|
res.status(500).json({ error: "Import failed" })
|
|
}
|
|
} |