feat(api): require session authentication for admin routes and improve error handling fix(api): streamline project image generation by fetching data directly from the database fix(api): optimize project import/export functionality with session validation and improved error handling fix(api): enhance analytics dashboard and email manager with session token for admin requests fix(components): improve loading states and dynamic imports for better user experience chore(security): update Content Security Policy to avoid unsafe-eval in production chore(deps): update package.json scripts for consistent environment handling in linting and testing
100 lines
3.5 KiB
TypeScript
100 lines
3.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { projectService } from '@/lib/prisma';
|
|
import { requireSessionAuth } from '@/lib/auth';
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const isAdminRequest = request.headers.get('x-admin-request') === 'true';
|
|
if (!isAdminRequest) return NextResponse.json({ error: 'Admin access required' }, { status: 403 });
|
|
const authError = requireSessionAuth(request);
|
|
if (authError) return authError;
|
|
|
|
const body = await request.json();
|
|
|
|
// Validate import data structure
|
|
if (!body.projects || !Array.isArray(body.projects)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid import data format' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const results = {
|
|
imported: 0,
|
|
skipped: 0,
|
|
errors: [] as string[]
|
|
};
|
|
|
|
// Preload existing titles once (avoid O(n^2) DB reads during import)
|
|
const existingProjectsResult = await projectService.getAllProjects({ limit: 10000 });
|
|
const existingProjects = existingProjectsResult.projects || existingProjectsResult;
|
|
const existingTitles = new Set(existingProjects.map(p => p.title));
|
|
|
|
// Process each project
|
|
for (const projectData of body.projects) {
|
|
try {
|
|
// Check if project already exists (by title)
|
|
const exists = existingTitles.has(projectData.title);
|
|
|
|
if (exists) {
|
|
results.skipped++;
|
|
results.errors.push(`Project "${projectData.title}" already exists`);
|
|
continue;
|
|
}
|
|
|
|
// Create new project
|
|
await projectService.createProject({
|
|
title: projectData.title,
|
|
description: projectData.description,
|
|
content: projectData.content,
|
|
tags: projectData.tags || [],
|
|
category: projectData.category,
|
|
featured: projectData.featured || false,
|
|
github: projectData.github,
|
|
live: projectData.live,
|
|
published: projectData.published !== false, // Default to true
|
|
imageUrl: projectData.imageUrl,
|
|
difficulty: projectData.difficulty || 'Intermediate',
|
|
timeToComplete: projectData.timeToComplete,
|
|
technologies: projectData.technologies || [],
|
|
challenges: projectData.challenges || [],
|
|
lessonsLearned: projectData.lessonsLearned || [],
|
|
futureImprovements: projectData.futureImprovements || [],
|
|
demoVideo: projectData.demoVideo,
|
|
screenshots: projectData.screenshots || [],
|
|
colorScheme: projectData.colorScheme || 'Dark',
|
|
accessibility: projectData.accessibility !== false, // Default to true
|
|
performance: projectData.performance || {
|
|
lighthouse: 0,
|
|
bundleSize: '0KB',
|
|
loadTime: '0s'
|
|
},
|
|
analytics: projectData.analytics || {
|
|
views: 0,
|
|
likes: 0,
|
|
shares: 0
|
|
}
|
|
});
|
|
|
|
results.imported++;
|
|
existingTitles.add(projectData.title);
|
|
} catch (error) {
|
|
results.skipped++;
|
|
results.errors.push(`Failed to import "${projectData.title}": ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `Import completed: ${results.imported} imported, ${results.skipped} skipped`,
|
|
results
|
|
});
|
|
} catch (error) {
|
|
console.error('Import error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to import projects' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|