Files
sofa/lib/services/settings.ts
T
jakeandClaude Opus 4.6 db3a6cc3d4 Add auto-close registration and admin settings page
First user to register automatically becomes admin and registration
closes. Admins can re-open registration from a new settings page.
Uses Better Auth admin plugin for role management and a new appSettings
table for the registration flag. Mobile tab bar now links to settings
instead of inline logout.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 13:51:20 -05:00

33 lines
949 B
TypeScript

import { count, eq } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { appSettings, user } from "@/lib/db/schema";
export async function getSetting(key: string): Promise<string | null> {
const row = await db
.select()
.from(appSettings)
.where(eq(appSettings.key, key))
.get();
return row?.value ?? null;
}
export async function setSetting(key: string, value: string): Promise<void> {
await db
.insert(appSettings)
.values({ key, value })
.onConflictDoUpdate({ target: appSettings.key, set: { value } });
}
export async function getUserCount(): Promise<number> {
const result = await db.select({ count: count() }).from(user).get();
return result?.count ?? 0;
}
export async function isRegistrationOpen(): Promise<boolean> {
const userCount = await getUserCount();
if (userCount === 0) return true;
const setting = await getSetting("registrationOpen");
return setting === "true";
}