Files
whyrating-engine-legacy/web/app/api/jobs/[jobId]/compare/route.ts
Alejandro Gutiérrez 47bb032011 Clean up project root - remove 51 obsolete files
Deleted:
- 26 old markdown summary/documentation files
- 16 debug/test Python scripts (debug_*, test_*, diagnose_*)
- 10 untracked JSON files from api_response_samples
- terms-of-usage.md, pane_not_found.png

Also includes pending web app changes:
- Jobs management UI (JobsView, Sidebar components)
- API routes for job streaming and comparison
- Enhanced ReviewAnalytics and ScraperTest components

Final clean structure:
├── api_server_production.py  (main entry)
├── modules/                  (core Python)
├── web/                      (Next.js frontend)
├── tests/                    (test suite)
├── docs/                     (documentation)
└── examples/                 (usage examples)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 17:31:53 +00:00

87 lines
2.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
// GET /api/jobs/[jobId]/compare?previous=<previousJobId>
// Returns reviews from current job with a flag indicating if they're new
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ jobId: string }> }
) {
try {
const { jobId } = await params;
const { searchParams } = new URL(request.url);
const previousJobId = searchParams.get('previous');
// Fetch current job reviews
const currentResponse = await fetch(`${API_BASE_URL}/jobs/${jobId}/reviews?limit=10000`);
if (!currentResponse.ok) {
return NextResponse.json(
{ error: 'Failed to get current job reviews' },
{ status: currentResponse.status }
);
}
const currentData = await currentResponse.json();
const currentReviews = currentData.reviews || [];
// If no previous job to compare, all reviews are "new"
if (!previousJobId) {
const reviewsWithStatus = currentReviews.map((review: Record<string, unknown>) => ({
...review,
is_new: true,
}));
return NextResponse.json({
reviews: reviewsWithStatus,
total_count: reviewsWithStatus.length,
new_count: reviewsWithStatus.length,
previous_job_id: null,
});
}
// Fetch previous job reviews
const previousResponse = await fetch(`${API_BASE_URL}/jobs/${previousJobId}/reviews?limit=10000`);
if (!previousResponse.ok) {
// Previous job not found, treat all as new
const reviewsWithStatus = currentReviews.map((review: Record<string, unknown>) => ({
...review,
is_new: true,
}));
return NextResponse.json({
reviews: reviewsWithStatus,
total_count: reviewsWithStatus.length,
new_count: reviewsWithStatus.length,
previous_job_id: previousJobId,
});
}
const previousData = await previousResponse.json();
const previousReviews = previousData.reviews || [];
// Create a Set of previous review IDs for O(1) lookup
const previousReviewIds = new Set(
previousReviews.map((r: { review_id: string }) => r.review_id)
);
// Mark reviews as new if they weren't in the previous job
const reviewsWithStatus = currentReviews.map((review: { review_id: string }) => ({
...review,
is_new: !previousReviewIds.has(review.review_id),
}));
const newCount = reviewsWithStatus.filter((r: { is_new: boolean }) => r.is_new).length;
return NextResponse.json({
reviews: reviewsWithStatus,
total_count: reviewsWithStatus.length,
new_count: newCount,
previous_job_id: previousJobId,
});
} catch (error) {
console.error('Compare API error:', error);
return NextResponse.json(
{ error: 'Failed to compare reviews' },
{ status: 500 }
);
}
}