mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 01:35:39 -04:00
Convert settings page to server component with server actions
Replace client-side data fetching with server-side queries and server actions, eliminating 3 API route files. Extract page into granular client components (account, integrations, server, webhook card) with optimistic updates. Rename sections: Media Servers → Integrations, Administration → Server. Add admin badge to account section. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { getSetting, setSetting } from "@/lib/services/settings";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session)
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
if (session.user.role !== "admin")
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
|
||||
const registrationOpen = (await getSetting("registrationOpen")) === "true";
|
||||
return NextResponse.json({ registrationOpen });
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session)
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
if (session.user.role !== "admin")
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
|
||||
const body = await request.json();
|
||||
if (typeof body.registrationOpen === "boolean") {
|
||||
await setSetting("registrationOpen", String(body.registrationOpen));
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import crypto from "node:crypto";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { webhookConnections } from "@/lib/db/schema";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session)
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const body = await request.json();
|
||||
const { provider } = body;
|
||||
|
||||
if (!provider || !["plex", "jellyfin"].includes(provider)) {
|
||||
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
|
||||
}
|
||||
|
||||
const newToken = crypto.randomBytes(32).toString("hex");
|
||||
|
||||
const connection = await db
|
||||
.update(webhookConnections)
|
||||
.set({ token: newToken })
|
||||
.where(
|
||||
and(
|
||||
eq(webhookConnections.userId, session.user.id),
|
||||
eq(webhookConnections.provider, provider),
|
||||
),
|
||||
)
|
||||
.returning()
|
||||
.get();
|
||||
|
||||
if (!connection) {
|
||||
return NextResponse.json(
|
||||
{ error: "Connection not found" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ connection });
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
import crypto from "node:crypto";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { webhookConnections, webhookEventLog } from "@/lib/db/schema";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session)
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const connections = await db
|
||||
.select()
|
||||
.from(webhookConnections)
|
||||
.where(eq(webhookConnections.userId, session.user.id))
|
||||
.all();
|
||||
|
||||
// Fetch recent events for each connection
|
||||
const connectionsWithEvents = await Promise.all(
|
||||
connections.map(async (conn) => {
|
||||
const events = await db
|
||||
.select()
|
||||
.from(webhookEventLog)
|
||||
.where(eq(webhookEventLog.connectionId, conn.id))
|
||||
.orderBy(desc(webhookEventLog.receivedAt))
|
||||
.limit(10)
|
||||
.all();
|
||||
return { ...conn, recentEvents: events };
|
||||
}),
|
||||
);
|
||||
|
||||
return NextResponse.json({ connections: connectionsWithEvents });
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session)
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const body = await request.json();
|
||||
const { provider, mediaServerUsername, enabled } = body;
|
||||
|
||||
if (!provider || !["plex", "jellyfin"].includes(provider)) {
|
||||
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
|
||||
}
|
||||
if (!mediaServerUsername || typeof mediaServerUsername !== "string") {
|
||||
return NextResponse.json(
|
||||
{ error: "Media server username is required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Check if connection already exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(webhookConnections)
|
||||
.where(
|
||||
and(
|
||||
eq(webhookConnections.userId, session.user.id),
|
||||
eq(webhookConnections.provider, provider),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
if (existing) {
|
||||
// Update existing — preserve token, update username and enabled
|
||||
const connection = await db
|
||||
.update(webhookConnections)
|
||||
.set({
|
||||
mediaServerUsername: mediaServerUsername.trim(),
|
||||
enabled: typeof enabled === "boolean" ? enabled : existing.enabled,
|
||||
})
|
||||
.where(eq(webhookConnections.id, existing.id))
|
||||
.returning()
|
||||
.get();
|
||||
return NextResponse.json({ connection });
|
||||
}
|
||||
|
||||
// Create new connection
|
||||
const token = crypto.randomBytes(32).toString("hex");
|
||||
const now = new Date();
|
||||
|
||||
const connection = await db
|
||||
.insert(webhookConnections)
|
||||
.values({
|
||||
userId: session.user.id,
|
||||
provider,
|
||||
token,
|
||||
mediaServerUsername: mediaServerUsername.trim(),
|
||||
enabled: true,
|
||||
createdAt: now,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
|
||||
return NextResponse.json({ connection });
|
||||
}
|
||||
|
||||
export async function DELETE(request: Request) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session)
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const body = await request.json();
|
||||
const { provider } = body;
|
||||
|
||||
if (!provider || !["plex", "jellyfin"].includes(provider)) {
|
||||
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
|
||||
}
|
||||
|
||||
await db
|
||||
.delete(webhookConnections)
|
||||
.where(
|
||||
and(
|
||||
eq(webhookConnections.userId, session.user.id),
|
||||
eq(webhookConnections.provider, provider),
|
||||
),
|
||||
)
|
||||
.run();
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
Reference in New Issue
Block a user