2018-01-22 17:08:33 -08:00
|
|
|
import { check } from 'express-validator/check';
|
2018-08-25 00:17:40 +01:00
|
|
|
import { ifNoUser401, createValidatorErrorHandler } from '../utils/middleware';
|
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;
|
2018-08-25 00:17:40 +01:00
|
|
|
const currentValue = user[flag];
|
|
|
|
return user.update$({ [flag]: !currentValue }).subscribe(
|
|
|
|
() =>
|
|
|
|
res.status(200).json({
|
2016-07-16 10:38:06 -07:00
|
|
|
flag,
|
|
|
|
value: !currentValue
|
|
|
|
}),
|
2018-08-25 00:17:40 +01:00
|
|
|
next
|
|
|
|
);
|
2016-07-16 10:38:06 -07:00
|
|
|
};
|
2016-07-19 16:36:34 -07:00
|
|
|
|
2018-05-15 14:56:26 +01:00
|
|
|
function refetchCompletedChallenges(req, res, next) {
|
2018-02-16 23:18:53 +00:00
|
|
|
const { user } = req;
|
2018-08-25 00:17:40 +01:00
|
|
|
return user
|
|
|
|
.requestCompletedChallenges()
|
|
|
|
.subscribe(completedChallenges => res.json({ completedChallenges }), next);
|
2018-02-16 23:18:53 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-08-25 00:17:40 +01:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
body: { email }
|
|
|
|
} = req;
|
|
|
|
return user
|
|
|
|
.requestUpdateEmail(email)
|
|
|
|
.subscribe(message => res.json({ message }), next);
|
2016-07-19 16:36:34 -07:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-08-25 00:17:40 +01:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
body: { currentChallengeId }
|
|
|
|
} = req;
|
2016-08-03 15:26:05 -07:00
|
|
|
return user.update$({ currentChallengeId }).subscribe(
|
2018-08-25 00:17:40 +01:00
|
|
|
() =>
|
|
|
|
res.json({
|
|
|
|
message: `your current challenge has been updated to ${currentChallengeId}`
|
|
|
|
}),
|
2016-08-03 15:26:05 -07:00
|
|
|
next
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-29 11:26:24 -08:00
|
|
|
const updateMyThemeValidators = [
|
|
|
|
check('theme')
|
2018-08-25 00:17:40 +01: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) {
|
2018-08-25 00:17:40 +01:00
|
|
|
const {
|
|
|
|
body: { theme }
|
|
|
|
} = req;
|
2016-08-05 14:05:57 -07:00
|
|
|
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-08-25 00:17:40 +01:00
|
|
|
return req.user
|
|
|
|
.updateTheme(theme)
|
|
|
|
.then(
|
|
|
|
() => res.sendFlash(alertTypes.info, 'Your theme has been updated'),
|
|
|
|
next
|
|
|
|
);
|
2018-02-16 23:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateFlags(req, res, next) {
|
2018-08-25 00:17:40 +01:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
body: { values }
|
|
|
|
} = req;
|
2018-02-16 23:18:53 +00:00
|
|
|
const keys = Object.keys(values);
|
2018-08-25 00:17:40 +01:00
|
|
|
if (keys.length === 1 && typeof keys[0] === 'boolean') {
|
2018-02-16 23:18:53 +00:00
|
|
|
return toggleUserFlag(keys[0], req, res, next);
|
|
|
|
}
|
2018-08-25 00:17:40 +01:00
|
|
|
return user
|
|
|
|
.requestUpdateFlags(values)
|
|
|
|
.subscribe(message => res.json({ message }), next);
|
2016-08-05 14:05:57 -07:00
|
|
|
}
|
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;
|
2018-08-25 00:17:40 +01:00
|
|
|
return user
|
|
|
|
.updateMyPortfolio(portfolio, requestDelete)
|
|
|
|
.subscribe(message => res.json({ message }), next);
|
|
|
|
}
|
2018-02-16 23:18:53 +00:00
|
|
|
|
2018-05-20 04:07:41 +01:00
|
|
|
function updateMyProfileUI(req, res, next) {
|
|
|
|
const {
|
|
|
|
user,
|
|
|
|
body: { profileUI }
|
|
|
|
} = req;
|
2018-08-25 00:17:40 +01:00
|
|
|
return user
|
|
|
|
.updateMyProfileUI(profileUI)
|
|
|
|
.subscribe(message => res.json({ message }), next);
|
2018-05-20 04:07:41 +01:00
|
|
|
}
|
|
|
|
|
2018-02-16 23:18:53 +00:00
|
|
|
function updateMyProjects(req, res, next) {
|
|
|
|
const {
|
|
|
|
user,
|
|
|
|
body: { projects: project }
|
|
|
|
} = req;
|
2018-08-25 00:17:40 +01:00
|
|
|
return user
|
|
|
|
.updateMyProjects(project)
|
|
|
|
.subscribe(message => res.json({ message }), next);
|
2018-02-16 23:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateMyUsername(req, res, next) {
|
2018-08-25 00:17:40 +01:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
body: { username }
|
|
|
|
} = req;
|
|
|
|
return user
|
|
|
|
.updateMyUsername(username)
|
|
|
|
.subscribe(message => res.json({ message }), next);
|
2018-02-16 23:18:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 00:17:40 +01:00
|
|
|
const updatePrivacyTerms = (req, res) => {
|
2018-05-27 17:29:04 +05:30
|
|
|
const {
|
|
|
|
user,
|
2018-08-25 00:17:40 +01:00
|
|
|
body: { quincyEmails }
|
2018-05-27 17:29:04 +05:30
|
|
|
} = req;
|
|
|
|
const update = {
|
|
|
|
acceptedPrivacyTerms: true,
|
2018-08-25 00:17:40 +01:00
|
|
|
sendQuincyEmail: !!quincyEmails
|
2018-05-27 17:29:04 +05:30
|
|
|
};
|
2018-08-25 00:17:40 +01:00
|
|
|
return user.updateAttributes(update, err => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).json({
|
|
|
|
type: 'warning',
|
|
|
|
message:
|
|
|
|
'Something went wrong updating your preferences. ' +
|
|
|
|
'Please try again.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return res.status(200).json({
|
|
|
|
type: 'success',
|
|
|
|
message:
|
|
|
|
'We have updated your preferences. ' +
|
|
|
|
'You can now continue using freeCodeCamp.'
|
|
|
|
});
|
|
|
|
});
|
2018-05-27 17:29:04 +05:30
|
|
|
};
|
|
|
|
|
2018-08-25 00:17:40 +01:00
|
|
|
api.put('/update-privacy-terms', ifNoUser401, updatePrivacyTerms);
|
2018-05-27 17:29:04 +05:30
|
|
|
|
2018-01-29 11:26:24 -08:00
|
|
|
api.post(
|
2018-05-15 14:56:26 +01:00
|
|
|
'/refetch-user-completed-challenges',
|
2018-01-29 11:26:24 -08:00
|
|
|
ifNoUser401,
|
2018-05-15 14:56:26 +01:00
|
|
|
refetchCompletedChallenges
|
2018-02-16 23:18:53 +00:00
|
|
|
);
|
2018-08-25 00:17:40 +01:00
|
|
|
api.post('/update-flags', ifNoUser401, updateFlags);
|
2018-08-29 20:52:41 +01:00
|
|
|
api.put(
|
2018-02-16 23:18:53 +00:00
|
|
|
'/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
|
|
|
);
|
2018-05-26 10:11:47 +01:00
|
|
|
api.post(
|
2018-08-29 20:52:41 +01:00
|
|
|
'/update-my-current-challenge',
|
2018-05-26 10:11:47 +01:00
|
|
|
ifNoUser401,
|
|
|
|
updateMyCurrentChallengeValidators,
|
|
|
|
createValidatorErrorHandler(alertTypes.danger),
|
|
|
|
updateMyCurrentChallenge
|
|
|
|
);
|
2018-08-25 00:17:40 +01:00
|
|
|
api.post('/update-my-portfolio', ifNoUser401, updateMyPortfolio);
|
|
|
|
api.post('/update-my-profile-ui', ifNoUser401, updateMyProfileUI);
|
|
|
|
api.post('/update-my-projects', ifNoUser401, 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
|
|
|
);
|
2018-08-25 00:17:40 +01:00
|
|
|
api.post('/update-my-username', ifNoUser401, updateMyUsername);
|
2016-08-05 14:05:57 -07:00
|
|
|
|
2018-08-25 00:17:40 +01:00
|
|
|
app.use('/external', api);
|
2018-08-29 20:52:41 +01:00
|
|
|
app.use('/internal', api);
|
2016-07-16 10:38:06 -07:00
|
|
|
app.use(api);
|
|
|
|
}
|