daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Mar 17th, 2018 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const User = require('../model/index').User;
  2.  
  3. const ResponseModel = require('../util/response-model');
  4. const VerifyUtils = require('../util/verify-request');
  5. const NumberUtils = require('../util/number-utils');
  6. const MessageHelper = require('../util/message/message-helper');
  7. const Handler = require('./handling-helper');
  8.  
  9.  
  10. const UserController = {};
  11. UserController.changeProfile = changeProfile;
  12. UserController.changePassword = changePassword;
  13.  
  14. module.exports = UserController;
  15.  
  16. async function changeProfile(req, res) {
  17.     try {
  18.         let isAccess = await VerifyUtils.verifyProtectRequest(req);
  19.         let userIdFromHeader = isAccess.user.id;
  20.         let userIdFromPathParam = req.params.id;
  21.         let user = req.body;
  22.  
  23.         if (userIdFromHeader !== userIdFromPathParam) {
  24.             handlingInvalidUpdateRequest(req, res);
  25.             return;
  26.         }
  27.  
  28.         let query = { id: userIdFromPathParam };
  29.  
  30.         delete user.id;
  31.         delete user.status;
  32.         delete user.ping_number;
  33.         delete user.role;
  34.         delete user.password;
  35.  
  36.         let updated = await User.update(user, { where: query });
  37.  
  38.         if (updated) {
  39.             res.status(200).json(new ResponseModel({
  40.                 code: 200,
  41.                 status_text: 'OK',
  42.                 success: true,
  43.                 data: getMessage(req, 'update_user_successful')
  44.             }));
  45.         }
  46.     } catch (error) {
  47.         if (error.constructor.name === 'ConnectionRefusedError') {
  48.             Handler.cannotConnectDatabase(req, res);
  49.         } else if (error.constructor.name === 'ValidationError' ||
  50.             error.constructor.name === 'UniqueConstraintError') {
  51.             Handler.validateError(req, res, error);
  52.         } else if (error.constructor.name === 'ErrorModel') {
  53.             Handler.invalidAccessToken(req, res);
  54.         } else {
  55.             handlingCannotUpdateUser(req, res);
  56.         }
  57.     }
  58. }
  59.  
  60. async function changePassword(req, res) {
  61.     try {
  62.         let isAccess = await VerifyUtils.verifyProtectRequest(req);
  63.         let user = req.body;
  64.         let userIdFromHeader = isAccess.user.id;
  65.  
  66.         if (user.id !== userIdFromHeader) {
  67.             handlingInvalidUpdateRequest(req, res);
  68.             return;
  69.         }
  70.  
  71.  
  72.         let oldPassword = user.old_password;
  73.         let newPassword = user.new_password;
  74.  
  75.         let userExists = await User.findById(user.id);
  76.         if (userExists.status === 'disabled') {
  77.             handlingUserIsDisabled(req, res);
  78.             return;
  79.         }
  80.  
  81.         let isMatchPassword = await userExists.comparePassword(oldPassword, userExists.password);
  82.         if (isMatchPassword) {
  83.             let query = { id: user.id };
  84.             let update = { password: newPassword, ping_number: NumberUtils.random4Digit() };
  85.  
  86.             let updated = await userExists.update(update, { where: query });
  87.  
  88.             if (updated) {
  89.                 res.status(200).json(new ResponseModel({
  90.                     code: 200,
  91.                     status_text: 'OK',
  92.                     success: true,
  93.                     data: getMessage(req, 'update_user_successful')
  94.                 }));
  95.             } else {
  96.                 handlingCannotUpdateUser(req, res);
  97.             }
  98.         } else {
  99.             handlingCannotUpdateUser(req, res);
  100.         }
  101.  
  102.     } catch (error) {
  103.         if (error.constructor.name === 'ConnectionRefusedError') {
  104.             Handler.cannotConnectDatabase(req, res);
  105.         } else if (error.constructor.name === 'ValidationError' ||
  106.             error.constructor.name === 'UniqueConstraintError') {
  107.             Handler.validateError(req, res, error);
  108.         } else if (error.constructor.name == 'ErrorModel') {
  109.             Handler.handlingErrorModel(res, error);
  110.         } else {
  111.             handlingCannotUpdateUser(req, res);
  112.         }
  113.     }
  114. }
  115.  
  116. function handlingCannotUpdateUser(req, res) {
  117.     res.status(503).json(new ResponseModel({
  118.         code: 503,
  119.         status_text: 'SERVICE UNAVAILABLE',
  120.         success: false,
  121.         data: null,
  122.         errors: [getMessage(req, 'update_user_fail')]
  123.     }));
  124. }
  125.  
  126. function handlingInvalidUpdateRequest(req, res) {
  127.     res.status(503).json(new ResponseModel({
  128.         code: 503,
  129.         status_text: 'SERVICE UNAVAILABLE',
  130.         success: false,
  131.         data: null,
  132.         errors: [getMessage(req, 'update_user_request_invalid')]
  133.     }));
  134. }
  135.  
  136. function handlingUserIsDisabled(req, res) {
  137.     res.status(503).json(new ResponseModel({
  138.         code: 503,
  139.         status_text: 'SERVICE UNAVAILABLE',
  140.         success: false,
  141.         data: null,
  142.         errors: [getMessage(req, 'user_is_disabled')]
  143.     }));
  144. }
  145.  
  146. function getMessage(req, errorText) {
  147.     let lang = req.query.lang || 'vi';
  148.     return MessageHelper.getMessage(lang, errorText);
  149. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top