diff --git a/README.md b/README.md index d9db46e..6e4dc12 100644 --- a/README.md +++ b/README.md @@ -167,24 +167,27 @@ // Geburtstag im YYYY-MM-DD Format "birthday": string, // Telefonnummer des Spielers - "phone": string, + "phone": string | null, // Minecraft-Username "username": string, // Minecraft-UUID mit Bindestrichen "uuid": string, // Minecraft-Edition - "edition": "java" | "bedrock" + "edition": "java" | "bedrock", + // Einladungscode einer direkten Einladung (optional) + "code": string | null | undefined } ``` ##### Response Codes -| http code | beschreibung | -| --------- | ------------------------------------------ | -| 201 | Der User wurde erstellt | -| 400 | Der Request Body ist falsch | -| 401 | Es wurde ein falsches API Secret angegeben | -| 409 | Username oder UUID existiert bereits | +| http code | beschreibung | +| --------- | ----------------------------------------------------------------------------------------------- | +| 201 | Der User wurde erstellt | +| 400 | Der Request Body ist falsch | +| 401 | Es wurde ein falsches API Secret angegeben | +| 404 | Der Einladungscode existiert nicht | +| 409 | Der Einladungscode wurde bereits verwendet oder der User (Username oder UUID) existiert bereits | diff --git a/src/pages/api/users/index.ts b/src/pages/api/users/index.ts index 733c152..49db8ba 100644 --- a/src/pages/api/users/index.ts +++ b/src/pages/api/users/index.ts @@ -5,37 +5,46 @@ import { externalApi } from '../_api.ts'; export const POST = externalApi({ auth: true, input: z.object({ - firstname: z.string().trim().min(1), - lastname: z.string().trim().min(1), + firstname: z.string().trim(), + lastname: z.string().trim(), birthday: z.string().date(), - phone: z.string().trim(), - username: z.string().trim().min(1), + phone: z.string().trim().nullable(), + username: z.string().trim(), uuid: z.string().uuid(), - edition: z.enum(['java', 'bedrock']) + 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 }); - if (existing) { - return new Response(JSON.stringify({ error: 'User with this username or uuid already exists' }), { - status: 409, - headers: { - 'Content-Type': 'application/json' - } + 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 }); - } - await db.addUser({ - firstname: input.firstname, - lastname: input.lastname, - birthday: input.birthday, - telephone: input.phone || null, - 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 });