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';
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-22 17:08:33 -08:00
|
|
|
api.post(
|
|
|
|
'/update-my-current-challenge',
|
|
|
|
ifNoUser401,
|
|
|
|
updateMyCurrentChallengeValidators,
|
|
|
|
createValidatorErrorHandler('errors'),
|
|
|
|
updateMyCurrentChallenge
|
|
|
|
);
|
|
|
|
|
2018-01-29 11:26:24 -08:00
|
|
|
const updateMyThemeValidators = [
|
|
|
|
check('theme')
|
|
|
|
.isIn(Object.keys(themes))
|
|
|
|
.withMessage('Theme is invalid.')
|
|
|
|
];
|
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:26:24 -08:00
|
|
|
return res.sendFlash('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)
|
2016-08-05 14:05:57 -07:00
|
|
|
.then(
|
2018-01-29 11:26:24 -08:00
|
|
|
() => res.sendFlash('info', 'Your theme has been updated'),
|
2016-08-05 14:05:57 -07:00
|
|
|
next
|
|
|
|
);
|
|
|
|
}
|
2018-01-29 11:26:24 -08:00
|
|
|
api.post(
|
|
|
|
'/update-my-theme',
|
|
|
|
ifNoUser401,
|
|
|
|
updateMyThemeValidators,
|
|
|
|
createValidatorErrorHandler('errors'),
|
|
|
|
updateMyTheme
|
|
|
|
);
|
2016-08-05 14:05:57 -07:00
|
|
|
|
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-05 14:05:57 -07:00
|
|
|
|
2016-07-16 10:38:06 -07:00
|
|
|
app.use(api);
|
|
|
|
}
|