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);
};
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) {
const { user } = req;
return certTypeIds[certType]
return user.getChallengeMap()
.flatMap(() => certTypeIds[certType])
.flatMap(challenge => {
const {
id,

View File

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

View File

@ -420,63 +420,59 @@ module.exports = function(app) {
}
function toggleLockdownMode(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) {
if (err) { return next(err); }
return user.updateAttribute('isLocked', !user.isLocked, function(err) {
if (err) { return next(err); }
req.flash('info', {
msg: 'We\'ve successfully updated your Privacy preferences.'
});
return res.redirect('/settings');
});
});
const { user } = req;
user.update$({ isLocked: !user.isLocked })
.subscribe(
() => {
req.flash('info', {
msg: 'We\'ve successfully updated your Privacy preferences.'
});
return res.redirect('/settings');
},
next
);
}
function toggleReceivesAnnouncementEmails(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) {
if (err) { return next(err); }
return user.updateAttribute(
'sendMonthlyEmail',
!user.sendMonthlyEmail,
(err) => {
if (err) { return next(err); }
const { user } = req;
return user.update$({ sendMonthlyEmail: !user.sendMonthlyEmail })
.subscribe(
() => {
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/settings');
});
});
},
next
);
}
function toggleReceivesQuincyEmails(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) {
if (err) { return next(err); }
return user.updateAttribute('sendQuincyEmail', !user.sendQuincyEmail,
(err) => {
if (err) { return next(err); }
const { user } = req;
return user.update$({ sendQuincyEmail: !user.sendQuincyEmail })
.subscribe(
() => {
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/settings');
}
},
next
);
});
}
function toggleReceivesNotificationEmails(req, res, next) {
return User.findById(req.accessToken.userId, function(err, user) {
if (err) { return next(err); }
return user.updateAttribute(
'sendNotificationEmail',
!user.sendNotificationEmail,
function(err) {
if (err) { return next(err); }
const { user } = req;
return user.update$({ sendNotificationEmail: !user.sendNotificationEmail })
.subscribe(
() => {
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/settings');
});
});
},
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
else
li.brownie-points-nav
a(href='/' + user.username) [ #{user.progressTimestamps.length} ]
a(href='/' + user.username) [ #{user.points} ]
li.hidden-xs.hidden-sm.avatar
a(href='/' + user.username)
img.profile-picture.float-right(src='#{user.picture}')