Files
website/src/pages/api/users/index.ts
T
bytedream 99eed4ca89
deploy / build-and-deploy (push) Successful in 42s
so not requred uuid to be in zod uuid format
2026-11-06 20:59:37 +01:00

53 lines
1.5 KiB
TypeScript

import { z } from 'astro:schema';
import { db } from '@db/database.ts';
import { externalApi } from '../_api.ts';
export const POST = externalApi({
auth: true,
input: z.object({
firstname: z.string().trim(),
lastname: z.string().trim(),
birthday: z.string().date(),
phone: z.string().trim().nullable(),
username: z.string().trim(),
uuid: z.string(),
edition: z.enum(['java', 'bedrock']),
code: z.string().nullish()
}),
handler: async ({ input }) => {
if (input.code) {
const directSignup = await db.getDirectSignupByCode({ code: input.code });
// check if direct signup code exists
if (!directSignup) return new Response(null, { status: 404 });
// check if direct signup code was already used
else if (directSignup.user) return new Response(null, { status: 409 });
}
const existing = await db.existsUser({
username: input.username,
uuid: input.uuid
});
// check if user is already registered
if (existing) return new Response(null, { status: 409 });
await db.transaction(async (tx) => {
const user = await tx.addUser({
firstname: input.firstname,
lastname: input.lastname,
birthday: input.birthday,
telephone: input.phone,
username: input.username,
edition: input.edition,
uuid: input.uuid
});
if (input.code) {
await tx.setDirectSignupUser({ code: input.code, userId: user.id });
}
});
return new Response(null, { status: 201 });
}
});