summary refs log tree commit diff
path: root/api/src/routes/users/@me/guilds/settings.ts
blob: 03029fccc3588bb7e8c76c2102a201e38da34b6a (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
import { Router, Response, Request } from "express";
import { Member, UserGuildSettings } from "@fosscord/util";
import { route } from "@fosscord/api";

const router = Router();

export interface UserGuildSettingsSchema extends Partial<UserGuildSettings> { }

// GET doesn't exist on discord.com
router.get("/", route({}), async (req: Request, res: Response) => {
	const user = await Member.findOneOrFail({
		where: { id: req.user_id },
		select: ["settings"]
	});
	return res.json(user.settings);
});

router.patch("/", route({ body: "UserSettingsSchema" }), async (req: Request, res: Response) => {
	const body = req.body as UserGuildSettings;

	const user = await Member.findOneOrFail({ where: { id: req.user_id } });
	user.settings = { ...user.settings, ...body };
	await user.save();

	res.json(user.settings);
});

export default router;