From 8e991bb0958d96ffb625b1f9c5f0a5a7e64df5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Barboza=20Coelho=20de=20Souza?= Date: Tue, 23 Jul 2019 19:34:09 -0300 Subject: [PATCH] update user --- src/app/controllers/UserController.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/app/controllers/UserController.js b/src/app/controllers/UserController.js index 75b1355c..25b87bc9 100644 --- a/src/app/controllers/UserController.js +++ b/src/app/controllers/UserController.js @@ -16,7 +16,29 @@ class UserController { } async update(req, res) { - return res.json({ ok: true }); + const { email, oldPassword } = req.body; + + const user = await User.findByPk(req.userId); + + if (email !== user.email) { + const userExists = await User.findOne({ where: { email } }); + if (userExists) { + return res.status(400).json({ error: 'User already exists' }); + } + } + + if (oldPassword && !(await user.checkPassword(oldPassword))) { + return res.status(401).json({ error: 'Password does not match' }); + } + + const { id, name, provider } = await user.update(req.body); + + return res.json({ + id, + name, + email, + provider, + }); } }