feat: basic admin interface
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import * as z from 'zod';
|
||||
import { INSTANCE_SETTINGS_ID, REGISTRATION_MODES, instanceSettings } from '~~/drizzle/schema';
|
||||
import { db } from '~~/server/lib/db';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await protectAdminRoute(event);
|
||||
|
||||
const result = await readValidatedBody(event, (body) =>
|
||||
z
|
||||
.object({
|
||||
registration: z.enum(REGISTRATION_MODES),
|
||||
})
|
||||
.safeParse(body),
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: result.error.issues[0]!.message,
|
||||
});
|
||||
}
|
||||
|
||||
if (isRegistrationLocked()) {
|
||||
throw createError({
|
||||
statusCode: 409,
|
||||
statusMessage: 'Registration is locked by the environment',
|
||||
data: {
|
||||
code: 'REGISTRATION_LOCKED',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const { registration } = result.data;
|
||||
|
||||
await db.insert(instanceSettings)
|
||||
.values({ id: INSTANCE_SETTINGS_ID, registration })
|
||||
.onConflictDoUpdate({
|
||||
target: instanceSettings.id,
|
||||
set: { registration, updatedAt: new Date() },
|
||||
});
|
||||
|
||||
return { registration, registrationLocked: false };
|
||||
});
|
||||
Reference in New Issue
Block a user