Feature(challenges): save users current challenge to db

This allows us to automatically load their current challenge
This commit is contained in:
Berkeley Martinez
2016-08-03 15:26:05 -07:00
parent 42de7c57ef
commit 2b32fb3633
6 changed files with 83 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import { ifNoUser401 } from '../utils/middleware';
import { isMongoId } from 'validator';
import supportedLanguages from '../../common/utils/supported-languages.js';
export default function settingsController(app) {
@@ -49,6 +50,20 @@ export default function settingsController(app) {
);
}
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
);
}
api.post(
'/toggle-lockdown',
toggleUserFlag('isLocked')
@@ -78,5 +93,11 @@ export default function settingsController(app) {
ifNoUser401,
updateMyLang
);
api.post(
'/update-my-current-challenge',
ifNoUser401,
updateMyCurrentChallenge
);
app.use(api);
}