| 
									
										
										
										
											2018-01-22 17:08:33 -08:00
										 |  |  | import { check } from 'express-validator/check'; | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 17:08:33 -08:00
										 |  |  | import { | 
					
						
							|  |  |  |   ifNoUser401, | 
					
						
							|  |  |  |   createValidatorErrorHandler | 
					
						
							|  |  |  | } from '../utils/middleware'; | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  | import supportedLanguages from '../../common/utils/supported-languages.js'; | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  | import { themes } from '../../common/utils/themes.js'; | 
					
						
							| 
									
										
										
										
											2018-01-29 11:34:44 -08:00
										 |  |  | import { alertTypes } from '../../common/utils/flash.js'; | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default function settingsController(app) { | 
					
						
							|  |  |  |   const api = app.loopback.Router(); | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |   const toggleUserFlag = (flag, req, res, next) => { | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |     const { user } = req; | 
					
						
							|  |  |  |     const currentValue = user[ flag ]; | 
					
						
							|  |  |  |     return user | 
					
						
							|  |  |  |       .update$({ [ flag ]: !currentValue }) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         () => res.status(200).json({ | 
					
						
							|  |  |  |           flag, | 
					
						
							|  |  |  |           value: !currentValue | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |   function refetchChallengeMap(req, res, next) { | 
					
						
							|  |  |  |     const { user } = req; | 
					
						
							|  |  |  |     return user.requestChallengeMap() | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         challengeMap => res.json({ challengeMap }), | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 18:53:33 -08:00
										 |  |  |   const updateMyEmailValidators = [ | 
					
						
							|  |  |  |     check('email') | 
					
						
							|  |  |  |       .isEmail() | 
					
						
							|  |  |  |       .withMessage('Email format is invalid.') | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |   function updateMyEmail(req, res, next) { | 
					
						
							|  |  |  |     const { user, body: { email } } = req; | 
					
						
							| 
									
										
										
										
											2017-10-21 04:58:53 +05:30
										 |  |  |     return user.requestUpdateEmail(email) | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |       .subscribe( | 
					
						
							| 
									
										
										
										
											2018-01-29 18:53:33 -08:00
										 |  |  |         message => res.sendFlash(alertTypes.info, message), | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function updateMyLang(req, res, next) { | 
					
						
							|  |  |  |     const { user, body: { lang } = {} } = req; | 
					
						
							|  |  |  |     const langName = supportedLanguages[lang]; | 
					
						
							|  |  |  |     const update = { languageTag: lang }; | 
					
						
							|  |  |  |     if (!supportedLanguages[lang]) { | 
					
						
							|  |  |  |       return res.json({ | 
					
						
							|  |  |  |         message: `${lang} is currently unsupported` | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-06 13:12:22 -05:00
										 |  |  |     if (user.languageTag === lang) { | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |       return res.json({ | 
					
						
							|  |  |  |         message: `Your language is already set to ${langName}` | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return user.update$(update) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         () => res.json({ | 
					
						
							|  |  |  |           message: `Your language has been updated to '${langName}'` | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 17:08:33 -08:00
										 |  |  |   const updateMyCurrentChallengeValidators = [ | 
					
						
							|  |  |  |     check('currentChallengeId') | 
					
						
							|  |  |  |       .isMongoId() | 
					
						
							|  |  |  |       .withMessage('currentChallengeId is not a valid challenge ID') | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:26:05 -07:00
										 |  |  |   function updateMyCurrentChallenge(req, res, next) { | 
					
						
							|  |  |  |     const { user, body: { currentChallengeId } } = req; | 
					
						
							|  |  |  |     return user.update$({ currentChallengeId }).subscribe( | 
					
						
							|  |  |  |       () => res.json({ | 
					
						
							|  |  |  |         message: | 
					
						
							|  |  |  |           `your current challenge has been updated to ${currentChallengeId}` | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  |   const updateMyThemeValidators = [ | 
					
						
							|  |  |  |     check('theme') | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     .isIn(Object.keys(themes)) | 
					
						
							|  |  |  |     .withMessage('Theme is invalid.') | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  |   ]; | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  |   function updateMyTheme(req, res, next) { | 
					
						
							|  |  |  |     const { body: { theme } } = req; | 
					
						
							|  |  |  |     if (req.user.theme === theme) { | 
					
						
							| 
									
										
										
										
											2018-01-29 11:34:44 -08:00
										 |  |  |       return res.sendFlash(alertTypes.info, 'Theme already set'); | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  |     return req.user.updateTheme(theme) | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     .then( | 
					
						
							|  |  |  |       () => res.sendFlash(alertTypes.info, 'Your theme has been updated'), | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function updateFlags(req, res, next) { | 
					
						
							|  |  |  |     const { user, body: { values } } = req; | 
					
						
							|  |  |  |     const keys = Object.keys(values); | 
					
						
							|  |  |  |     if ( | 
					
						
							|  |  |  |       keys.length === 1 && | 
					
						
							|  |  |  |       typeof keys[0] === 'boolean' | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |       return toggleUserFlag(keys[0], req, res, next); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return user.requestUpdateFlags(values) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         message => res.json({ message }), | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function updateMyPortfolio(req, res, next) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       user, | 
					
						
							|  |  |  |       body: { portfolio } | 
					
						
							|  |  |  |     } = req; | 
					
						
							|  |  |  |     // if we only have one key, it should be the id
 | 
					
						
							|  |  |  |     // user cannot send only one key to this route
 | 
					
						
							|  |  |  |     // other than to remove a portfolio item
 | 
					
						
							|  |  |  |     const requestDelete = Object.keys(portfolio).length === 1; | 
					
						
							|  |  |  |     return user.updateMyPortfolio(portfolio, requestDelete) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         message => res.json({ message }), | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function updateMyProjects(req, res, next) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       user, | 
					
						
							|  |  |  |       body: { projects: project } | 
					
						
							|  |  |  |     } = req; | 
					
						
							|  |  |  |     return user.updateMyProjects(project) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         message => res.json({ message }), | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function updateMyUsername(req, res, next) { | 
					
						
							|  |  |  |     const { user, body: { username } } = req; | 
					
						
							|  |  |  |     return user.updateMyUsername(username) | 
					
						
							|  |  |  |     .subscribe( | 
					
						
							|  |  |  |       message => res.json({ message }), | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/refetch-user-challenge-map', | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     refetchChallengeMap | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/update-flags', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     updateFlags | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/update-my-email', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     updateMyEmailValidators, | 
					
						
							| 
									
										
										
										
											2018-01-29 11:34:44 -08:00
										 |  |  |     createValidatorErrorHandler(alertTypes.danger), | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyEmail | 
					
						
							| 
									
										
										
										
											2018-01-29 11:26:24 -08:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2017-06-17 18:09:43 -04:00
										 |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/update-my-current-challenge', | 
					
						
							| 
									
										
										
										
											2017-06-17 18:09:43 -04:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyCurrentChallengeValidators, | 
					
						
							|  |  |  |     createValidatorErrorHandler(alertTypes.danger), | 
					
						
							|  |  |  |     updateMyCurrentChallenge | 
					
						
							| 
									
										
										
										
											2017-06-17 18:09:43 -04:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/update-my-lang', | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyLang | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/update-my-portfolio', | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyPortfolio | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/update-my-projects', | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyProjects | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/update-my-theme', | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyThemeValidators, | 
					
						
							|  |  |  |     createValidatorErrorHandler(alertTypes.danger), | 
					
						
							|  |  |  |     updateMyTheme | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |   api.post( | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     '/update-my-username', | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2018-02-16 23:18:53 +00:00
										 |  |  |     updateMyUsername | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   app.use(api); | 
					
						
							|  |  |  | } |