← back to Omega Watches 2

src/app/api/sources/route.ts

34 lines

import { NextRequest, NextResponse } from "next/server";
import { query } from "@/lib/db";
import { getSession } from "@/lib/auth";

export const dynamic = "force-dynamic";

export async function GET(request: NextRequest) {
  const session = await getSession(request);
  if (!session) {
    return NextResponse.json({ success: false, error: { code: "UNAUTHORIZED", message: "Login required" } }, { status: 401 });
  }

  const sources = await query(`
    SELECT
      ds.*,
      sh.total_runs,
      sh.completed_runs,
      sh.failed_runs,
      sh.last_completed,
      sh.success_rate_pct,
      sh.total_records_inserted,
      sh.total_records_quarantined
    FROM data_source ds
    LEFT JOIN source_health sh ON ds.id = sh.source_id
    ORDER BY ds.reliability_score DESC, ds.name
  `);

  return NextResponse.json({
    success: true,
    data: sources,
    meta: { total: sources.length, timestamp: new Date().toISOString() },
  });
}