mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 11:12:11 +01:00
Security Enhancements: - Implemented proper rate limiting with automatic cleanup for /register and /forgot-password endpoints - Added memory usage protection with MAX_ENTRIES limit (10000) - Fixed rate limiter memory leaks by adding cleanup intervals - Improved IP extraction with x-real-ip and x-client-ip header support Code Quality Improvements: - Refactored ProcessingStatusManager from individual functions to class-based architecture - Maintained backward compatibility with singleton instance pattern - Fixed TypeScript strict mode violations across the codebase - Resolved all build errors and type mismatches UI Component Fixes: - Removed unused chart components (Charts.tsx, DonutChart.tsx) - Fixed calendar component type issues by removing unused custom implementations - Resolved theme provider type imports - Fixed confetti component default options handling - Corrected pointer component coordinate type definitions Type System Improvements: - Extended NextAuth types to support dual auth systems (regular and platform users) - Fixed nullable type handling throughout the codebase - Resolved Prisma JSON field type compatibility issues - Corrected SessionMessage and ImportRecord interface definitions - Fixed ES2015 iteration compatibility issues Database & Performance: - Updated database pool configuration for Prisma adapter compatibility - Fixed pagination response structure in user management endpoints - Improved error handling with proper error class usage Testing & Build: - All TypeScript compilation errors resolved - ESLint warnings remain but no errors - Build completes successfully with proper static generation
126 lines
3.6 KiB
TypeScript
126 lines
3.6 KiB
TypeScript
import { ProcessingStage } from "@prisma/client";
|
|
import { type NextRequest, NextResponse } from "next/server";
|
|
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "../../../../lib/auth";
|
|
import { prisma } from "../../../../lib/prisma";
|
|
import { processUnprocessedSessions } from "../../../../lib/processingScheduler";
|
|
import { getSessionsNeedingProcessing } from "../../../../lib/processingStatusManager";
|
|
|
|
interface SessionUser {
|
|
email: string;
|
|
name?: string;
|
|
}
|
|
|
|
interface SessionData {
|
|
user: SessionUser;
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const session = (await getServerSession(authOptions)) as SessionData | null;
|
|
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Not logged in" }, { status: 401 });
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: session.user.email },
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
role: true,
|
|
companyId: true,
|
|
company: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
status: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: "No user found" }, { status: 401 });
|
|
}
|
|
|
|
// Check if user has ADMIN role
|
|
if (user.role !== "ADMIN") {
|
|
return NextResponse.json(
|
|
{ error: "Admin access required" },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
// Get optional parameters from request body
|
|
const body = await request.json();
|
|
const { batchSize, maxConcurrency } = body;
|
|
|
|
// Validate parameters
|
|
const validatedBatchSize =
|
|
batchSize && batchSize > 0 ? Number.parseInt(batchSize) : null;
|
|
const validatedMaxConcurrency =
|
|
maxConcurrency && maxConcurrency > 0
|
|
? Number.parseInt(maxConcurrency)
|
|
: 5;
|
|
|
|
// Check how many sessions need AI processing using the new status system
|
|
const sessionsNeedingAI = await getSessionsNeedingProcessing(
|
|
ProcessingStage.AI_ANALYSIS,
|
|
1000 // Get count only
|
|
);
|
|
|
|
// Filter to sessions for this company
|
|
const companySessionsNeedingAI = sessionsNeedingAI.filter(
|
|
(statusRecord) => statusRecord.session.companyId === user.companyId
|
|
);
|
|
|
|
const unprocessedCount = companySessionsNeedingAI.length;
|
|
|
|
if (unprocessedCount === 0) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: "No sessions requiring AI processing found",
|
|
unprocessedCount: 0,
|
|
processedCount: 0,
|
|
});
|
|
}
|
|
|
|
// Start processing (this will run asynchronously)
|
|
const _startTime = Date.now();
|
|
|
|
// Note: We're calling the function but not awaiting it to avoid timeout
|
|
// The processing will continue in the background
|
|
processUnprocessedSessions(validatedBatchSize, validatedMaxConcurrency)
|
|
.then(() => {
|
|
console.log(
|
|
`[Manual Trigger] Processing completed for company ${user.companyId}`
|
|
);
|
|
})
|
|
.catch((error) => {
|
|
console.error(
|
|
`[Manual Trigger] Processing failed for company ${user.companyId}:`,
|
|
error
|
|
);
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `Started processing ${unprocessedCount} unprocessed sessions`,
|
|
unprocessedCount,
|
|
batchSize: validatedBatchSize || unprocessedCount,
|
|
maxConcurrency: validatedMaxConcurrency,
|
|
startedAt: new Date().toISOString(),
|
|
});
|
|
} catch (error) {
|
|
console.error("[Manual Trigger] Error:", error);
|
|
return NextResponse.json(
|
|
{
|
|
error: "Failed to trigger processing",
|
|
details: error instanceof Error ? error.message : String(error),
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|