| 
									
										
										
										
											2016-08-03 15:26:05 -07:00
										 |  |  | import { isMongoId } from 'validator'; | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { ifNoUser401 } from '../utils/middleware'; | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  | import supportedLanguages from '../../common/utils/supported-languages.js'; | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default function settingsController(app) { | 
					
						
							|  |  |  |   const api = app.loopback.Router(); | 
					
						
							|  |  |  |   const toggleUserFlag = flag => (req, res, next) => { | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							|  |  |  |   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( | 
					
						
							|  |  |  |         (message) => res.json({ message }), | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 15:26:05 -07:00
										 |  |  |   function updateMyCurrentChallenge(req, res, next) { | 
					
						
							|  |  |  |     const { user, body: { currentChallengeId } } = req; | 
					
						
							|  |  |  |     if (!isMongoId('' + currentChallengeId)) { | 
					
						
							|  |  |  |       return next(new Error(`${currentChallengeId} is not a valid ObjectId`)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return user.update$({ currentChallengeId }).subscribe( | 
					
						
							|  |  |  |       () => res.json({ | 
					
						
							|  |  |  |         message: | 
					
						
							|  |  |  |           `your current challenge has been updated to ${currentChallengeId}` | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |       next | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  |   function updateMyTheme(req, res, next) { | 
					
						
							|  |  |  |     req.checkBody('theme', 'Theme is invalid.').isLength({ min: 4 }); | 
					
						
							|  |  |  |     const { body: { theme } } = req; | 
					
						
							|  |  |  |     const errors = req.validationErrors(true); | 
					
						
							|  |  |  |     if (errors) { | 
					
						
							|  |  |  |       return res.status(403).json({ errors }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (req.user.theme === theme) { | 
					
						
							|  |  |  |       return res.json({ msg: 'Theme already set' }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return req.user.updateTheme('' + theme) | 
					
						
							|  |  |  |       .then( | 
					
						
							|  |  |  |         data => res.json(data), | 
					
						
							|  |  |  |         next | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 18:09:43 -04:00
										 |  |  |   api.post( | 
					
						
							|  |  |  |     '/toggle-available-for-hire', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     toggleUserFlag('isAvailableForHire') | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   api.post( | 
					
						
							|  |  |  |     '/toggle-lockdown', | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  |     ifNoUser401, | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |     toggleUserFlag('isLocked') | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/toggle-announcement-email', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     toggleUserFlag('sendMonthlyEmail') | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/toggle-notification-email', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     toggleUserFlag('sendNotificationEmail') | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/toggle-quincy-email', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     toggleUserFlag('sendQuincyEmail') | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-07-19 16:36:34 -07:00
										 |  |  |   api.post( | 
					
						
							|  |  |  |     '/update-my-email', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     updateMyEmail | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/update-my-lang', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     updateMyLang | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-08-03 15:26:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/update-my-current-challenge', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     updateMyCurrentChallenge | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-08-05 14:05:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   api.post( | 
					
						
							|  |  |  |     '/update-my-theme', | 
					
						
							|  |  |  |     ifNoUser401, | 
					
						
							|  |  |  |     updateMyTheme | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 10:38:06 -07:00
										 |  |  |   app.use(api); | 
					
						
							|  |  |  | } |