From 8d077690d659032cfc6b1308fb7858174b8fecf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Intevel=20=E3=83=84?= <59223342+Intevel@users.noreply.github.com> Date: Thu, 6 May 2021 22:30:47 +0200 Subject: [PATCH] [Route] PATCH /users/:id --- src/routes/users/@me/index.ts | 18 ++++++++++++++++-- src/schema/User.ts | 10 ++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/routes/users/@me/index.ts b/src/routes/users/@me/index.ts index e4d9588f..d139203d 100644 --- a/src/routes/users/@me/index.ts +++ b/src/routes/users/@me/index.ts @@ -1,12 +1,13 @@ import { Router, Request, Response } from "express"; -import { UserModel } from "@fosscord/server-util"; +import { UserModel, toObject } from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; import { getPublicUser } from "../../../util/User"; +import { UserModifySchema } from "../../../schema/User" +import { check } from "../../../util/instanceOf"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - // TODO: user projection const user = await UserModel.findOne({ id: req.user_id }).exec(); if (!user) throw new HTTPError("User not found", 404); @@ -15,4 +16,17 @@ router.get("/", async (req: Request, res: Response) => { res.json(publicUser); }); +router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { + const body = req.body as UserModifySchema; + + const user = await UserModel.findOne({ id: req.user_id }).exec(); + if (!user) throw new HTTPError("User not found", 404); + + var newuser = await UserModel.findOneAndUpdate({ id: req.user_id }, { + ...body + }).exec(); + + res.json(newuser); +}); + export default router; diff --git a/src/schema/User.ts b/src/schema/User.ts index 2b74a433..15f27088 100644 --- a/src/schema/User.ts +++ b/src/schema/User.ts @@ -41,3 +41,13 @@ export interface UserUpdateSchema { public_flags: bigint; guilds: string[]; } + +export const UserModifySchema = { + username: String, + avatar: String || null, +}; + +export interface UserModifySchema { + username: string; + avatar: string | null; +}