diff --git a/src/controller/userController.ts b/src/controller/userController.ts index 603b782..2bfc0f2 100644 --- a/src/controller/userController.ts +++ b/src/controller/userController.ts @@ -4,14 +4,30 @@ import QRCode from "qrcode"; import InternalException from "../exceptions/internalException"; import { CLUB_NAME } from "../env.defaults"; import UserService from "../service/userService"; +import UserFactory from "../factory/admin/user"; +import { UpdateUserCommand } from "../command/userCommand"; +import UserCommandHandler from "../command/userCommandHandler"; /** - * @description get user totp + * @description get my by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function getUserTotp(req: Request, res: Response): Promise { +export async function getMeById(req: Request, res: Response): Promise { + const id = parseInt(req.userId); + let user = await UserService.getById(id); + + res.json(UserFactory.mapToSingle(user)); +} + +/** + * @description get my totp + * @param req {Request} Express req object + * @param res {Response} Express res object + * @returns {Promise<*>} + */ +export async function getMyTotp(req: Request, res: Response): Promise { const userId = parseInt(req.userId); let { secret } = await UserService.getById(userId); @@ -31,12 +47,12 @@ export async function getUserTotp(req: Request, res: Response): Promise { } /** - * @description verify user totp + * @description verify my totp * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ -export async function verifyUserTotp(req: Request, res: Response): Promise { +export async function verifyMyTotp(req: Request, res: Response): Promise { const userId = parseInt(req.userId); let totp = req.body.totp; @@ -53,3 +69,28 @@ export async function verifyUserTotp(req: Request, res: Response): Promise } res.sendStatus(204); } + +/** + * @description update my data + * @param req {Request} Express req object + * @param res {Response} Express res object + * @returns {Promise<*>} + */ +export async function updateMe(req: Request, res: Response): Promise { + const id = parseInt(req.userId); + let mail = req.body.mail; + let firstname = req.body.firstname; + let lastname = req.body.lastname; + let username = req.body.username; + + let updateUser: UpdateUserCommand = { + id: id, + mail: mail, + firstname: firstname, + lastname: lastname, + username: username, + }; + await UserCommandHandler.update(updateUser); + + res.sendStatus(204); +} diff --git a/src/routes/user.ts b/src/routes/user.ts index 40cabae..47e8e7c 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -1,14 +1,22 @@ import express from "express"; -import { getUserTotp, verifyUserTotp } from "../controller/userController"; +import { getMeById, getMyTotp, updateMe, verifyMyTotp } from "../controller/userController"; var router = express.Router({ mergeParams: true }); +router.get("/me", async (req, res) => { + await getMeById(req, res); +}); + router.get("/totp", async (req, res) => { - await getUserTotp(req, res); + await getMyTotp(req, res); }); router.post("/verify", async (req, res) => { - await verifyUserTotp(req, res); + await verifyMyTotp(req, res); +}); + +router.patch("/me", async (req, res) => { + await updateMe(req, res); }); export default router;