From 4e8d7281421e84510890c01919c52200d719bff4 Mon Sep 17 00:00:00 2001 From: Le Viet Date: Tue, 15 Mar 2022 16:53:48 +0700 Subject: [PATCH] update user --- src/server/controllers/member.js | 30 ++- src/server/routes/index.js | 229 +------------------ src/server/routes/member.js | 323 +++++++++++++++++++++++++++ src/server/services/member/member.js | 46 ++++ 4 files changed, 386 insertions(+), 242 deletions(-) create mode 100644 src/server/routes/member.js diff --git a/src/server/controllers/member.js b/src/server/controllers/member.js index 06dafe6..5e0296c 100755 --- a/src/server/controllers/member.js +++ b/src/server/controllers/member.js @@ -84,26 +84,22 @@ module.exports = { }, updateProfile(req, res) { - return memberServices - .updateProfile({ - name: req.body.name, - size: req.body.size, - price: req.body.price, - status: req.body.status - }, { returning: true, where: { id: req.params.id } }) - .then(([rowsUpdate, [updatedRow]]) => res.status(200).send(updatedRow)) + const username = req.user.username; + const { firstname, lastname, phone } = req.body; + const data = { + firstname: firstname, + lastname: lastname, + phone: phone + } + return memberServices.updateProfile(username, data) + .then(([rowsUpdate, [updatedRow]]) => res.status(200).send("Update profile successfully")) .catch(error => res.status(400).send(error)); }, - updatePassword(req, res) { - return memberServices - .updatePassword({ - name: req.body.name, - size: req.body.size, - price: req.body.price, - status: req.body.status - }, { returning: true, where: { id: req.params.id } }) - .then(([rowsUpdate, [updatedRow]]) => res.status(200).send(updatedRow)) + changePassword(req, res) { + memberServices + .updatePassword(req) + .then(([rowsUpdate, [updatedRow]]) => res.status(200).send("Update password successfully")) .catch(error => res.status(400).send(error)); }, diff --git a/src/server/routes/index.js b/src/server/routes/index.js index 16883cc..5794f2a 100755 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -1,227 +1,6 @@ -const memberController = require("../controllers").member; -const auth = require("../middleware/auth"); - -module.exports = app => { - /** - * @swagger - * /v1/user/register: - * post: - * tags: - * - Users - * summary: Create a JSONPlaceholder user. - * requestBody: - * required: true - * content: - * application/json: - * schema: - * type: object - * properties: - * username: - * type: string - * description: The user's username. - * example: test@float.sg - * email: - * type: string - * description: The user's email. - * example: test@float.sg - * password: - * type: string - * description: The user's password. - * example: test@float.sg - * firstname: - * type: string - * description: The user's firstname. - * example: Float - * lastname: - * type: string - * description: The user's lastname. - * example: Mobility - * phone: - * type: string - * description: The user's phone. - * example: 123456789 - * responses: - * 200: - * description: Created - * content: - * application/json: - * schema: - * type: object - * properties: - * message: - * type: striing - * description: Message - * example: Register successfully - * 400: - * description: Invalid - * content: - * application/json: - * schema: - * type: object - * properties: - * message: - * type: striing - * description: Message - * example: Failed - */ - app.post("/api/v1/user/register", memberController.validate('register'), memberController.register); - /** - * @swagger - * /v1/user/login: - * post: - * tags: - * - Users - * summary: Create a JSONPlaceholder user. - * requestBody: - * required: true - * content: - * application/json: - * schema: - * type: object - * properties: - * username: - * type: string - * description: The user's username. - * example: test@float.sg - * password: - * type: string - * description: The user's password. - * example: test@float.sg - * responses: - * 200: - * description: Login success - * content: - * application/json: - * schema: - * type: object - * properties: - * accessToken: - * type: string - * description: Message - * example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3RAZmxvYXQuc2ciLCJpYXQiOjE2NDcwODIxNDIsImV4cCI6MTY0NzA4Mzk0Mn0.fMQYB4ielKPh21QcZFwOJSjlGNCAg8rkvEcKWctxdX8 - * 400: - * description: Invalid - * content: - * application/json: - * schema: - * type: object - * properties: - * message: - * type: string - * description: Message - * example: Wrong username or password - */ - app.post("/api/v1/user/login", memberController.login); - /** - * @swagger - * /v1/user/forgot-password: - * post: - * tags: - * - Users - * summary: Create a JSONPlaceholder user. - * requestBody: - * required: true - * content: - * application/json: - * schema: - * type: object - * properties: - * email: - * type: string - * description: The user's username. - * example: test@float.sg - * responses: - * 200: - * description: Login success - * content: - * application/json: - * schema: - * type: object - * properties: - * message: - * type: string - * description: Message - * example: Email has been sent - * 400: - * description: Invalid - * content: - * application/json: - * schema: - * type: object - * properties: - * message: - * type: string - * description: Message - * example: Wrong email - */ - app.post("/api/v1/user/forgot-password", memberController.forgotPassword); - /** - * @swagger - * /v1/user/reset-password: - * post: - * tags: - * - Users - * summary: Create a JSONPlaceholder user. - * requestBody: - * required: true - * content: - * application/json: - * schema: - * type: object - * properties: - * email: - * type: string - * description: The user's username. - * example: test@float.sg - * mode: - * type: integer - * description: The reset mode (100: start, 200:confirm, 300:complete) - * example: 100 - * responses: - * 200: - * description: Login success - * content: - * application/json: - * schema: - * type: object - * properties: - * accessToken: - * type: string - * description: Message - * example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3RAZmxvYXQuc2ciLCJpYXQiOjE2NDcwODIxNDIsImV4cCI6MTY0NzA4Mzk0Mn0.fMQYB4ielKPh21QcZFwOJSjlGNCAg8rkvEcKWctxdX8 - * 400: - * description: Invalid - * content: - * application/json: - * schema: - * type: object - * properties: - * message: - * type: string - * description: Message - * example: Wrong username or password - */ - app.post("/api/v1/user/reset-password", memberController.resetPassword); - /** - * @swagger - * /v1/user/profile: - * get: - * tags: - * - Users - * summary: Retrieve a single JSONPlaceholder user. - * description: Retrieve a single JSONPlaceholder user. Can be used to populate a user profile when prototyping or testing an API. - * security: # <--- ADD THIS - * - bearerAuth: [] # <--- ADD THIS - * responses: - * 200: - * description: A single user. - * content: - * application/json: - * schema: - * type: object - * $ref: '#/components/schemas/Member' - */ - - app.get("/api/v1/user/profile", auth, memberController.profile); +const member = require("./member"); +const url = '/api/v1/'; +module.exports = (app) => { + member(app, url) }; diff --git a/src/server/routes/member.js b/src/server/routes/member.js new file mode 100644 index 0000000..e59bd5c --- /dev/null +++ b/src/server/routes/member.js @@ -0,0 +1,323 @@ +const memberController = require("../controllers").member; +const auth = require("../middleware/auth"); + +module.exports = (app, url) => { + /** + * @swagger + * /v1/user/register: + * post: + * tags: + * - Users + * summary: Create a JSONPlaceholder user. + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * username: + * type: string + * description: The user's username. + * example: test@float.sg + * email: + * type: string + * description: The user's email. + * example: test@float.sg + * password: + * type: string + * description: The user's password. + * example: test@float.sg + * firstname: + * type: string + * description: The user's firstname. + * example: Float + * lastname: + * type: string + * description: The user's lastname. + * example: Mobility + * phone: + * type: string + * description: The user's phone. + * example: 123456789 + * responses: + * 200: + * description: Created + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: striing + * description: Message + * example: Register successfully + * 400: + * description: Invalid + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: striing + * description: Message + * example: Failed + */ + app.post(url + "user/register", memberController.validate('register'), memberController.register); + /** + * @swagger + * /v1/user/login: + * post: + * tags: + * - Users + * summary: Create a JSONPlaceholder user. + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * username: + * type: string + * description: The user's username. + * example: test@float.sg + * password: + * type: string + * description: The user's password. + * example: test@float.sg + * responses: + * 200: + * description: Login success + * content: + * application/json: + * schema: + * type: object + * properties: + * accessToken: + * type: string + * description: Message + * example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3RAZmxvYXQuc2ciLCJpYXQiOjE2NDcwODIxNDIsImV4cCI6MTY0NzA4Mzk0Mn0.fMQYB4ielKPh21QcZFwOJSjlGNCAg8rkvEcKWctxdX8 + * 400: + * description: Invalid + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * description: Message + * example: Wrong username or password + */ + app.post(url + "user/login", memberController.login); + /** + * @swagger + * /v1/user/forgot-password: + * post: + * tags: + * - Users + * summary: Create a JSONPlaceholder user. + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * email: + * type: string + * description: The user's username. + * example: test@float.sg + * responses: + * 200: + * description: Login success + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * description: Message + * example: Email has been sent + * 400: + * description: Invalid + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * description: Message + * example: Wrong email + */ + app.post(url + "user/forgot-password", memberController.forgotPassword); + /** + * @swagger + * /v1/user/reset-password: + * post: + * tags: + * - Users + * summary: Create a JSONPlaceholder user. + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * email: + * type: string + * description: The user's username. + * example: test@float.sg + * mode: + * type: integer + * description: The reset mode + * example: 100 + * responses: + * 200: + * description: Login success + * content: + * application/json: + * schema: + * type: object + * properties: + * accessToken: + * type: string + * description: Message + * example: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3RAZmxvYXQuc2ciLCJpYXQiOjE2NDcwODIxNDIsImV4cCI6MTY0NzA4Mzk0Mn0.fMQYB4ielKPh21QcZFwOJSjlGNCAg8rkvEcKWctxdX8 + * 400: + * description: Invalid + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * description: Message + * example: Wrong username or password + */ + app.post(url + "user/reset-password", memberController.resetPassword); + /** + * @swagger + * /v1/user/profile: + * get: + * tags: + * - Users + * summary: Retrieve a single JSONPlaceholder user. + * description: Retrieve a single JSONPlaceholder user. Can be used to populate a user profile when prototyping or testing an API. + * security: + * - bearerAuth: [] + * responses: + * 200: + * description: A single user. + * content: + * application/json: + * schema: + * type: object + * $ref: '#/components/schemas/Member' + */ + + app.get(url + "user/profile", auth, memberController.profile); + /** + * @swagger + * /v1/user/update: + * put: + * tags: + * - Users + * summary: Update User profile + * description: update user's profile + * security: + * - bearerAuth: [] + * responses: + * 200: + * description: A single user. + * content: + * application/json: + * schema: + * type: object + * $ref: '#/components/schemas/Member' + */ + app.put(url + "user/update", auth, memberController.updateProfile); + /** + * @swagger + * /v1/user/change-password: + * put: + * tags: + * - Users + * summary: Update User profile + * description: update user's profile + * security: + * - bearerAuth: [] + * responses: + * 200: + * description: Login success + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * description: Message + * example: Email has been sent + * 400: + * description: Invalid + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * description: Message + * example: Wrong email + */ + app.put(url + "user/change-password", auth, memberController.changePassword); + + /** + * app.post("tracklocation"); + * app.post("savecardpayment"); + * app.post("getcardpaymentlist"); + * app.post("managepaymentlist"); + * app.post("verifysession"); + * app.post("refreshsession"); + * app.post("linkemail"); + * app.post("refreshlinkemail"); + * app.post("usertransportlist"); + * app.post("usertransportprofile"); + * app.post("getdashcarddata"); + * app.post("loadsavedcards"); + * app.post("loadsavedcards"); + * app.post("savedashcard"); + * app.post("saveuserbudget"); + * app.post("resetpass"); + * app.post("settingsarray"); + * app.post("loadsubsription"); + * app.post("subscriptionstatus"); + * app.post("getapplist"); + * app.post("getslidecarddata"); + * app.post("subscribedcarddata"); + * app.post("subscribecard"); + * app.post("savesurvey"); + * app.post("pointsdetail"); + * app.post("loadredeemabale"); + * app.post("redeempoints"); + * app.post("managefeature"); + * app.post("carpool"); + * app.post("logout"); + * app.post("logout"); + * app.post("carpoolstatus"); + * app.post("getsavedtrips"); + * app.post("savedtrip"); + * app.post("removesavedtrip"); + * app.post("trackcardclick"); + * app.post("membersettings"); + * app.post("persnoality"); + */ + +}; diff --git a/src/server/services/member/member.js b/src/server/services/member/member.js index 696a4a3..5e2431b 100755 --- a/src/server/services/member/member.js +++ b/src/server/services/member/member.js @@ -213,6 +213,52 @@ module.exports = { }); }, + updateProfile(username, data) { + return Member + .update(data, + { + returning: true, + where: { + username: username + } + } + ) + .then(([rowsUpdate, [updatedRow]]) => { + return updatedRow + }) + .catch(error => { + console.log(error) + }); + }, + + async updatePassword(req) { + const username = req.user.username; + const user = await Member.findOne({ + where: { username: username}, + attributes: ['password', 'username'] + }); + const cmp = await bcrypt.compare(req.body.oldPassword, user.password); + + console.log(cmp); + if (cmp) { + const salt = bcrypt.genSaltSync(10); + const hashpassword = bcrypt.hashSync(req.body.newPassword, salt); + return Member + .update( + { + password: hashpassword + }, + { + returning: true, + where: { + username: username + } + } + ); + } + return "Wrong password" + }, + async deactivateAccount(req) { return Member .destroy({