Organize/lib/settings.ts

30 lines
983 B
TypeScript

import "server-only";
import { prisma } from "@/lib/db";
import { SignupMode } from "@/lib/generated/prisma/enums";
// Fixed id of the one-and-only SiteSettings row (see prisma/schema.prisma).
const SETTINGS_ID = "singleton";
/**
* Reads the site's sign-up mode. No row means the settings were never
* touched, which is the same as OPEN -- today's default behavior -- so
* this never creates a row just to read it.
*/
export async function getSignupMode(): Promise<SignupMode> {
const settings = await prisma.siteSettings.findUnique({
where: { id: SETTINGS_ID },
select: { signupMode: true },
});
return settings?.signupMode ?? SignupMode.OPEN;
}
/** Creates the settings row on first write, updates it on every write after. */
export async function setSignupMode(mode: SignupMode): Promise<void> {
await prisma.siteSettings.upsert({
where: { id: SETTINGS_ID },
update: { signupMode: mode },
create: { id: SETTINGS_ID, signupMode: mode },
});
}