refactor challenge and challengeMap

This commit is contained in:
Berkeley Martinez
2015-06-02 19:02:54 -07:00
parent 956a2b915e
commit e7d01424e9
4 changed files with 441 additions and 452 deletions

View File

@@ -57,6 +57,9 @@
"less": "~1.7.5",
"less-middleware": "~2.0.1",
"lodash": "~2.4.1",
"loopback": "^2.18.0",
"loopback-boot": "^2.8.0",
"loopback-connector-mongodb": "^1.10.0",
"lusca": "~1.0.2",
"method-override": "~2.3.0",
"moment": "~2.10.2",

View File

@@ -31,17 +31,33 @@
*/
var R = require('ramda'),
express = require('express'),
Challenge = require('../../common/models/Challenge'),
User = require('../../common/models/User'),
resources = require('../resources/resources'),
userMigration = require('../resources/middleware').userMigration,
MDNlinks = require('../../seed_data/bonfireMDNlinks');
var router = express.Router();
var challengeMapWithNames = resources.getChallengeMapWithNames();
var challengeMapWithIds = resources.getChallengeMapWithIds();
function getMDNlinks(links) {
// takes in an array of links, which are strings
var populatedLinks = [];
// for each key value, push the corresponding link
// from the MDNlinks object into a new array
if (links) {
links.forEach(function (value) {
populatedLinks.push(MDNlinks[value]);
});
}
return populatedLinks;
}
module.exports = function(app) {
var router = app.Router();
var Challenge = app.models.Challenge;
var User = app.models.User;
router.get(
'/challenges/next-challenge',
userMigration,
@@ -59,20 +75,6 @@ router.post('/completed-challenge/', completedChallenge);
router.post('/completed-zipline-or-basejump', completedZiplineOrBasejump);
router.post('/completed-bonfire', completedBonfire);
function getMDNlinks(links) {
// takes in an array of links, which are strings
var populatedLinks = [];
// for each key value, push the corresponding link
// from the MDNlinks object into a new array
if (links) {
links.forEach(function (value) {
populatedLinks.push(MDNlinks[value]);
});
}
return populatedLinks;
}
function returnNextChallenge(req, res, next) {
if (!req.user) {
return res.redirect('../challenges/learn-how-free-code-camp-works');
@@ -176,11 +178,11 @@ function returnIndividualChallenge(req, res, next) {
.join(' ') :
dashedName.replace(/\-/g, ' ');
Challenge.find({'name': new RegExp(challengeName, 'i')},
Challenge.find(
{ where: { name: new RegExp(challengeName, 'i') } },
function(err, challengeFromMongo) {
if (err) {
return next(err);
}
if (err) { return next(err); }
// Handle not found
if (challengeFromMongo.length < 1) {
req.flash('errors', {
@@ -339,12 +341,12 @@ function completedBonfire(req, res, next) {
var challengeName = req.body.challengeInfo.challengeName;
if (isCompletedWith) {
var paired = User.find({'profile.username': isCompletedWith.toLowerCase()})
.limit(1);
paired.exec(function (err, pairedWith) {
if (err) {
return next(err);
} else {
User.find({
where: { 'profile.username': isCompletedWith.toLowerCase() },
limit: 1
}, function (err, pairedWith) {
if (err) { return next(err); }
var index = req.user.uncompletedChallenges.indexOf(challengeId);
if (index > -1) {
req.user.progressTimestamps.push(Date.now() || 0);
@@ -388,11 +390,9 @@ function completedBonfire(req, res, next) {
challengeType: 5
});
req.user.save(function (err, user) {
if (err) {
return next(err);
}
if (err) { return next(err); }
if (pairedWith) {
pairedWith.save(function (err, paired) {
if (err) {
@@ -406,7 +406,6 @@ function completedBonfire(req, res, next) {
res.send(true);
}
});
}
});
} else {
req.user.completedChallenges.push({
@@ -425,14 +424,9 @@ function completedBonfire(req, res, next) {
req.user.uncompletedChallenges.splice(index, 1);
}
req.user.save(function (err, user) {
if (err) {
return next(err);
}
// NOTE(berks): Under certain conditions the res is never ended
if (user) {
req.user.save(function (err) {
if (err) { return next(err); }
res.send(true);
}
});
}
}
@@ -486,13 +480,11 @@ function completedZiplineOrBasejump(req, res, next) {
}
if (isCompletedWith) {
var paired = User.find({'profile.username': isCompletedWith.toLowerCase()})
.limit(1);
paired.exec(function (err, pairedWithFromMongo) {
if (err) {
return next(err);
} else {
User.find({
where: { 'profile.username': isCompletedWith.toLowerCase() },
limit: 1
}, function (err, pairedWithFromMongo) {
if (err) { return next(err); }
var index = req.user.uncompletedChallenges.indexOf(challengeId);
if (index > -1) {
req.user.progressTimestamps.push(Date.now() || 0);
@@ -512,9 +504,7 @@ function completedZiplineOrBasejump(req, res, next) {
});
req.user.save(function (err, user) {
if (err) {
return next(err);
}
if (err) { return next(err); }
if (req.user._id.toString() === pairedWith._id.toString()) {
return res.sendStatus(200);
@@ -545,7 +535,6 @@ function completedZiplineOrBasejump(req, res, next) {
}
});
});
}
});
} else {
@@ -570,12 +559,12 @@ function completedZiplineOrBasejump(req, res, next) {
if (err) {
return next(err);
}
// NOTE(berks): under certain conditions this will not close the response.
// NOTE(berks): under certain conditions this will not close
// the response.
if (user) {
return res.sendStatus(200);
}
});
}
}
module.exports = router;
};

View File

@@ -1,17 +1,17 @@
var R = require('ramda'),
express = require('express'),
// debug = require('debug')('freecc:cntr:challengeMap'),
User = require('../../common/models/User'),
resources = require('./../resources/resources'),
middleware = require('../resources/middleware'),
router = express.Router();
middleware = require('../resources/middleware');
module.exports = function(app) {
var User = app.models.User;
var router = app.Router();
router.get('/map', middleware.userMigration, challengeMap);
router.get('/learn-to-code', function(req, res) {
res.redirect(301, '/map');
});
router.get('/about', function(req, res) {
res.redirect(301, '/map');
});
@@ -48,10 +48,9 @@ function challengeMap(req, res, next) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var daysRunning = Math.ceil(timeDiff / (1000 * 3600 * 24));
User.count({}, function (err, camperCount) {
if (err) {
return next(err);
}
User.count(function(err, camperCount) {
if (err) { return next(err); }
res.render('challengeMap/show', {
daysRunning: daysRunning,
camperCount: numberWithCommas(camperCount),
@@ -61,5 +60,4 @@ function challengeMap(req, res, next) {
});
});
}
module.exports = router;
};

View File

@@ -1,6 +1,6 @@
var express = require('express');
var Job = require('../../common/models/Job');
var router = express.Router();
module.exports = function(app) {
var Job = app.models.Job;
var router = app.Router();
router.get('/jobs', jobsDirectory);
@@ -14,5 +14,4 @@ function jobsDirectory(req, res, next) {
});
});
}
module.exports = router;
};