summary refs log tree commit diff
path: root/src/api/routes/users/@me/pomelo-attempt.ts
blob: d4327f618ad4e7b676c290459d1fbbde72fb2d29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { route } from "@spacebar/api";
import { Config, UniqueUsernameAttemptSchema, User } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router = Router();

// https://discord-userdoccers.vercel.app/resources/user#get-pomelo-eligibility
router.post(
	"/",
	route({
		requestBody: "UniqueUsernameAttemptSchema",
		responses: {
			200: { body: "UniqueUsernameAttemptResponse" },
			400: { body: "APIErrorResponse" },
		},
		description:
			"Checks whether a unique username is available for the user to claim.",
	}),
	async (req: Request, res: Response) => {
		const body = req.body as UniqueUsernameAttemptSchema;
		const { uniqueUsernames } = Config.get().general;
		if (!uniqueUsernames) {
			throw new HTTPError(
				"Unique Usernames feature is not enabled on this instance.",
				400,
			);
		}

		res.json({
			taken: !(await User.isUsernameAvailable(body.username)),
		});
	},
);

export default router;