From 1308d312a417733db7503219a0679ecdbe75f2bf Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Wed, 6 Apr 2016 21:08:19 -0700 Subject: [PATCH] Add methods to get challengeMap Update boot/challenge.js to use new methods Update boot/user.js to use new methods --- common/models/user.js | 24 ++++++ server/boot/certificate.js | 3 +- server/boot/challenge.js | 134 +++++++++++++++++------------- server/boot/user.js | 66 +++++++-------- server/views/partials/navbar.jade | 2 +- 5 files changed, 132 insertions(+), 97 deletions(-) diff --git a/common/models/user.js b/common/models/user.js index c772884066..4c48f5e3ab 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -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; + }); + }; }; diff --git a/server/boot/certificate.js b/server/boot/certificate.js index 6775edd3a0..d9bb42d246 100644 --- a/server/boot/certificate.js +++ b/server/boot/certificate.js @@ -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, diff --git a/server/boot/challenge.js b/server/boot/challenge.js index aa8938e6df..6496579ba3 100644 --- a/server/boot/challenge.js +++ b/server/boot/challenge.js @@ -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', { diff --git a/server/boot/user.js b/server/boot/user.js index 37b56bd214..8d9984e2ab 100644 --- a/server/boot/user.js +++ b/server/boot/user.js @@ -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) { diff --git a/server/views/partials/navbar.jade b/server/views/partials/navbar.jade index eb03371a1f..5e527ddc8d 100644 --- a/server/views/partials/navbar.jade +++ b/server/views/partials/navbar.jade @@ -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}')