80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getSessionUser } from "@/lib/session";
|
|
import { getTenant, patchTenantSpec } from "@/lib/k8s";
|
|
|
|
export async function GET(
|
|
_req: NextRequest,
|
|
{ params }: { params: Promise<{ name: string }> }
|
|
) {
|
|
const user = await getSessionUser();
|
|
if (!user)
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
const { name } = await params;
|
|
|
|
try {
|
|
const tenant = await getTenant(name);
|
|
if (!tenant)
|
|
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
|
|
if (
|
|
!user.isPlatform &&
|
|
tenant.metadata.labels?.["pieced.ch/zitadel-org-id"] !== user.orgId
|
|
) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
return NextResponse.json(tenant);
|
|
} catch (e: any) {
|
|
return NextResponse.json(
|
|
{ error: e.message },
|
|
{ status: e.statusCode || 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PATCH(
|
|
req: NextRequest,
|
|
{ params }: { params: Promise<{ name: string }> }
|
|
) {
|
|
const user = await getSessionUser();
|
|
if (!user)
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
if (!user.isPlatform && !user.roles.includes("owner")) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
const { name } = await params;
|
|
const body = await req.json();
|
|
|
|
try {
|
|
const existing = await getTenant(name);
|
|
if (!existing)
|
|
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
|
|
if (
|
|
!user.isPlatform &&
|
|
existing.metadata.labels?.["pieced.ch/zitadel-org-id"] !== user.orgId
|
|
) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
const specPatch: Record<string, any> = {};
|
|
if (body.packages !== undefined) specPatch.packages = body.packages;
|
|
if (body.workspaceFiles !== undefined)
|
|
specPatch.workspaceFiles = body.workspaceFiles;
|
|
if (body.displayName !== undefined)
|
|
specPatch.displayName = body.displayName;
|
|
if (body.agentName !== undefined) specPatch.agentName = body.agentName;
|
|
|
|
const updated = await patchTenantSpec(name, specPatch);
|
|
return NextResponse.json(updated);
|
|
} catch (e: any) {
|
|
return NextResponse.json(
|
|
{ error: e.message },
|
|
{ status: e.statusCode || 500 }
|
|
);
|
|
}
|
|
}
|