Add methods to get challengeMap

Update boot/challenge.js to use new methods
Update boot/user.js to use new methods
This commit is contained in:
Berkeley Martinez
2016-04-06 21:08:19 -07:00
parent 7737bfedc1
commit 1308d312a4
5 changed files with 132 additions and 97 deletions

View File

@ -441,4 +441,28 @@ module.exports = function(User) {
} }
return this.constructor.update$({ id }, updateData, updateOptions); return this.constructor.update$({ id }, updateData, updateOptions);
}; };
User.prototype.getPoints$ = function getPoints$() {
const id = this.getId();
const filter = {
where: { id },
fields: { progressTimestamps: true }
};
return this.constructor.findOne$(filter)
.map(user => {
this.progressTimestamps = user.progressTimestamps;
return user.progressTimestamps;
});
};
User.prototype.getChallengeMap$ = function getChallengeMap$() {
const id = this.getId();
const filter = {
where: { id },
fields: { challengeMap: true }
};
return this.constructor.findOne$(filter)
.map(user => {
this.challengeMap = user.challengeMap;
return user.challengeMap;
});
};
}; };

View File

@ -84,7 +84,8 @@ export default function certificate(app) {
function verifyCert(certType, req, res, next) { function verifyCert(certType, req, res, next) {
const { user } = req; const { user } = req;
return certTypeIds[certType] return user.getChallengeMap()
.flatMap(() => certTypeIds[certType])
.flatMap(challenge => { .flatMap(challenge => {
const { const {
id, id,

View File

@ -142,7 +142,7 @@ function getRenderData$(user, challenge$, origChallengeName, solution) {
return challenge$ return challenge$
.map(challenge => challenge.toJSON()) .map(challenge => challenge.toJSON())
.filter((challenge) => { .filter(challenge => {
return shouldNotFilterComingSoon(challenge) && return shouldNotFilterComingSoon(challenge) &&
challenge.type !== 'hike' && challenge.type !== 'hike' &&
testChallengeName.test(challenge.name); testChallengeName.test(challenge.name);
@ -500,8 +500,17 @@ module.exports = function(app) {
function showChallenge(req, res, next) { function showChallenge(req, res, next) {
const solution = req.query.solution; const solution = req.query.solution;
const challengeName = req.params.challengeName.replace(challengesRegex, ''); const challengeName = req.params.challengeName.replace(challengesRegex, '');
const { user } = req;
getRenderData$(req.user, challenge$, challengeName, solution) Observable.defer(() => {
if (user && user.getChallengeMap$) {
return user.getChallengeMap$().map(user);
}
return Observable.just(null);
})
.flatMap(user => {
return getRenderData$(user, challenge$, challengeName, solution);
})
.subscribe( .subscribe(
({ type, redirectUrl, message, data }) => { ({ type, redirectUrl, message, data }) => {
if (message) { if (message) {
@ -546,48 +555,46 @@ module.exports = function(app) {
return res.sendStatus(403); return res.sendStatus(403);
} }
const completedDate = Date.now(); return req.user.getChallengeMap$()
const { .flatMap(() => {
id, const completedDate = Date.now();
name, const {
challengeType, id,
solution, name,
timezone challengeType,
} = req.body; solution,
timezone
} = req.body;
const { alreadyCompleted, updateData } = buildUserUpdate( const { alreadyCompleted, updateData } = buildUserUpdate(
req.user, req.user,
id, id,
{ {
id, id,
challengeType, challengeType,
solution, solution,
name, name,
completedDate completedDate
}, },
timezone timezone
); );
const user = req.user; const user = req.user;
const points = alreadyCompleted ? const points = alreadyCompleted ? user.points : user.points + 1;
user.progressTimestamps.length :
user.progressTimestamps.length + 1;
return user.update$(updateData) return user.update$(updateData)
.doOnNext(({ count }) => log('%s documents updated', count)) .doOnNext(({ count }) => log('%s documents updated', count))
.subscribe( .map(() => {
() => {}, if (type === 'json') {
next, return res.json({
function() { points,
if (type === 'json') { alreadyCompleted
return res.json({ });
points, }
alreadyCompleted return res.sendStatus(200);
}); });
} })
return res.sendStatus(200); .subscribe(() => {}, next);
}
);
} }
function completedZiplineOrBasejump(req, res, next) { function completedZiplineOrBasejump(req, res, next) {
@ -635,31 +642,38 @@ module.exports = function(app) {
} }
const { return user.getChallengeMap$()
alreadyCompleted, .flatMap(() => {
updateData const {
} = buildUserUpdate(req.user, completedChallenge.id, completedChallenge); alreadyCompleted,
updateData
} = buildUserUpdate(user, completedChallenge.id, completedChallenge);
return user.update$(updateData) return user.update$(updateData)
.doOnNext(({ count }) => log('%s documents updated', count)) .doOnNext(({ count }) => log('%s documents updated', count))
.doOnNext(() => { .doOnNext(() => {
if (type === 'json') { if (type === 'json') {
return res.send({ return res.send({
alreadyCompleted, alreadyCompleted,
points: alreadyCompleted ? points: alreadyCompleted ? user.points : user.points + 1
user.progressTimestamps.length : });
user.progressTimestamps.length + 1 }
return res.status(200).send(true);
}); });
}
return res.status(200).send(true);
}) })
.subscribe(() => {}, next); .subscribe(() => {}, next);
} }
function showMap(showAside, { user = {} }, res, next) { function showMap(showAside, { user }, res, next) {
const { challengeMap = {} } = user; return Observable.defer(() => {
if (user && typeof user.getChallengeMap$ === 'function') {
return getSuperBlocks$(challenge$, challengeMap) return user.getChallengeMap$();
}
console.log('foo');
return Observable.just({});
})
.doOnNext(challengeMap => console.log('challengeMap', challengeMap))
.flatMap(challengeMap => getSuperBlocks$(challenge$, challengeMap))
.subscribe( .subscribe(
superBlocks => { superBlocks => {
res.render('map/show', { res.render('map/show', {

View File

@ -420,63 +420,59 @@ module.exports = function(app) {
} }
function toggleLockdownMode(req, res, next) { function toggleLockdownMode(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) { const { user } = req;
if (err) { return next(err); } user.update$({ isLocked: !user.isLocked })
return user.updateAttribute('isLocked', !user.isLocked, function(err) { .subscribe(
if (err) { return next(err); } () => {
req.flash('info', { req.flash('info', {
msg: 'We\'ve successfully updated your Privacy preferences.' msg: 'We\'ve successfully updated your Privacy preferences.'
}); });
return res.redirect('/settings'); return res.redirect('/settings');
}); },
}); next
);
} }
function toggleReceivesAnnouncementEmails(req, res, next) { function toggleReceivesAnnouncementEmails(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) { const { user } = req;
if (err) { return next(err); } return user.update$({ sendMonthlyEmail: !user.sendMonthlyEmail })
return user.updateAttribute( .subscribe(
'sendMonthlyEmail', () => {
!user.sendMonthlyEmail,
(err) => {
if (err) { return next(err); }
req.flash('info', { req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.' msg: 'We\'ve successfully updated your Email preferences.'
}); });
return res.redirect('/settings'); return res.redirect('/settings');
}); },
}); next
);
} }
function toggleReceivesQuincyEmails(req, res, next) { function toggleReceivesQuincyEmails(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) { const { user } = req;
if (err) { return next(err); } return user.update$({ sendQuincyEmail: !user.sendQuincyEmail })
return user.updateAttribute('sendQuincyEmail', !user.sendQuincyEmail, .subscribe(
(err) => { () => {
if (err) { return next(err); }
req.flash('info', { req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.' msg: 'We\'ve successfully updated your Email preferences.'
}); });
return res.redirect('/settings'); return res.redirect('/settings');
} },
next
); );
});
} }
function toggleReceivesNotificationEmails(req, res, next) { function toggleReceivesNotificationEmails(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) { const { user } = req;
if (err) { return next(err); } return user.update$({ sendNotificationEmail: !user.sendNotificationEmail })
return user.updateAttribute( .subscribe(
'sendNotificationEmail', () => {
!user.sendNotificationEmail,
function(err) {
if (err) { return next(err); }
req.flash('info', { req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.' msg: 'We\'ve successfully updated your Email preferences.'
}); });
return res.redirect('/settings'); return res.redirect('/settings');
}); },
}); next
);
} }
function postDeleteAccount(req, res, next) { function postDeleteAccount(req, res, next) {

View File

@ -28,7 +28,7 @@ nav.navbar.navbar-default.navbar-fixed-top.nav-height
a(href='/login') Sign in a(href='/login') Sign in
else else
li.brownie-points-nav li.brownie-points-nav
a(href='/' + user.username) [ #{user.progressTimestamps.length} ] a(href='/' + user.username) [ #{user.points} ]
li.hidden-xs.hidden-sm.avatar li.hidden-xs.hidden-sm.avatar
a(href='/' + user.username) a(href='/' + user.username)
img.profile-picture.float-right(src='#{user.picture}') img.profile-picture.float-right(src='#{user.picture}')