61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { db } from "@/lib/db";
|
|
|
|
// ── 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,
|
|
_count: {
|
|
select: { shots: true },
|
|
},
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ projects, total: projects.length });
|
|
}
|