import { NextRequest, NextResponse } from "next/server"; import { db } from "@/lib/db"; import { resolveProjectTemplates } from "@/lib/render-pipeline/project-templates"; // ── Auth ───────────────────────────────────────────────────────────────────── function isAuthorized(req: NextRequest): boolean { const apiKey = process.env.API_SECRET_KEY; if (!apiKey) return false; const authHeader = req.headers.get("authorization") ?? ""; if (authHeader.startsWith("Bearer ")) return authHeader.slice(7) === apiKey; return (req.headers.get("x-api-key") ?? "") === apiKey; } // ── GET /api/ext/projects ───────────────────────────────────────────────────── // // Returns all projects with their showIds. Useful for pipeline tools that need // to enumerate available projects and resolve a showId → projectCode mapping. // // Query params (all optional): // status Filter by project status: ACTIVE (default) | ARCHIVED | ALL // // Example: // GET /api/ext/projects // GET /api/ext/projects?status=ALL export async function GET(req: NextRequest) { if (!isAuthorized(req)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const { searchParams } = new URL(req.url); const statusParam = searchParams.get("status")?.toUpperCase() ?? "ACTIVE"; const whereStatus = statusParam === "ALL" ? undefined : statusParam === "ARCHIVED" ? { status: "ARCHIVED" as const } : { status: "ACTIVE" as const }; const projects = await db.project.findMany({ where: whereStatus, orderBy: [{ showId: "asc" }, { code: "asc" }], select: { id: true, name: true, code: true, showId: true, projectType: true, status: true, startDate: true, dueDate: true, pipelineConfig: true, _count: { select: { shots: true }, }, }, }); // Templates are resolved here rather than in the panel so that the panel and // the headless preview build cannot disagree about which template, or which // show LUT, a shot was built with. const withTemplates = await Promise.all( projects.map(async ({ pipelineConfig, ...project }) => ({ ...project, templates: await resolveProjectTemplates({ ...project, pipelineConfig }), })) ); return NextResponse.json({ projects: withTemplates, total: withTemplates.length }); }