Merge pull request #55 from FreeCodeCamp/race-conditions

Ensure order of post/save/fetch operation in bonfire completions.
This commit is contained in:
Quincy Larson
2015-01-28 14:44:31 -08:00
4 changed files with 30 additions and 32 deletions

13
app.js
View File

@ -287,8 +287,6 @@ app.post('/completed-bonfire/', function (req, res) {
var isCompletedDate = Math.round(+new Date() / 1000);
var bonfireHash = req.body.bonfireInfo.bonfireHash;
var isSolution = req.body.bonfireInfo.solution;
// TODO
debug(isCompletedWith, 'Is completed with');
if (isCompletedWith) {
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
@ -328,14 +326,14 @@ app.post('/completed-bonfire/', function (req, res) {
throw err;
}
if (user && paired) {
res.redirect('/bonfires');
res.send(true);
}
})
});
}
})
} else {
req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: null,
@ -344,21 +342,20 @@ app.post('/completed-bonfire/', function (req, res) {
})
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
req.user.save(function(err, user) {
if (err) {
throw err;
}
if (user) {
res.redirect('/bonfires');
debug('Saving user');
res.send(true)
}
});
}
});
// Unique Check API route

View File

@ -37,7 +37,7 @@ exports.returnNextBonfire = function(req, res, next) {
return res.redirect('bonfires/meet-bonfire');
}
var currentTime = parseInt(+new Date() / 1000);
if (currentTime - req.user.lastContentSync > 86400) {
if (currentTime - req.user.lastContentSync > 10) {
req.user.lastContentSync = currentTime;
var completed = req.user.completedBonfires.map(function (elem) {
return elem._id;

View File

@ -214,11 +214,13 @@ var runTests = function(err, data) {
}
}
});
}
if (allTestsPassed) {
allTestsPassed = false;
showCompletion();
}
}
};
function showCompletion() {

View File

@ -39,20 +39,32 @@ $(document).ready(function() {
$('#complete-bonfire-dialog').modal('show');
// Only post to server if there is an authenticated user
if ($('.signup-btn-nav').length < 1) {
$.ajax({
type: 'POST',
data: {
$.post(
'/completed-bonfire',
{
bonfireInfo: {
completedWith : didCompleteWith,
solution: bonfireSolution,
bonfireHash: thisBonfireHash
}
},
url: '/completed-bonfire/'
function(res) {
if (res) {
window.location.href = 'http://localhost:3001/bonfires'
}
})
}
}
$('.next-bonfire-button').on('click', function() {
var bonfireSolution = myCodeMirror.getValue();
var thisBonfireHash = passedBonfireHash || null;
var didCompleteWith = $('#completed-with').val() || null;
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
});
}
}
$('.all-challenges').on('click', function() {
$('#all-challenges-dialog').modal('show');
@ -68,19 +80,6 @@ $(document).ready(function() {
});
$('.next-bonfire-button').on('click', function() {
var bonfireSolution = myCodeMirror.getValue();
var thisBonfireHash = passedBonfireHash || null;
var didCompleteWith = $('#completed-with').val() || null;
completedBonfire(didCompleteWith, bonfireSolution, thisBonfireHash);
window.setTimeout(function() {
// TODO
window.location = '/bonfires';
}, 100);
});
// Bonfire instructions functions
$('#more-info').on('click', function() {