Merge pull request #55 from FreeCodeCamp/race-conditions
Ensure order of post/save/fetch operation in bonfire completions.
This commit is contained in:
13
app.js
13
app.js
@ -287,8 +287,6 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
var isCompletedDate = Math.round(+new Date() / 1000);
|
var isCompletedDate = Math.round(+new Date() / 1000);
|
||||||
var bonfireHash = req.body.bonfireInfo.bonfireHash;
|
var bonfireHash = req.body.bonfireInfo.bonfireHash;
|
||||||
var isSolution = req.body.bonfireInfo.solution;
|
var isSolution = req.body.bonfireInfo.solution;
|
||||||
// TODO
|
|
||||||
debug(isCompletedWith, 'Is completed with');
|
|
||||||
|
|
||||||
if (isCompletedWith) {
|
if (isCompletedWith) {
|
||||||
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
|
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
|
||||||
@ -328,14 +326,14 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
if (user && paired) {
|
if (user && paired) {
|
||||||
res.redirect('/bonfires');
|
res.send(true);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
req.user.completedBonfires.push({
|
req.user.completedBonfires.push({
|
||||||
_id: bonfireHash,
|
_id: bonfireHash,
|
||||||
completedWith: null,
|
completedWith: null,
|
||||||
@ -344,21 +342,20 @@ app.post('/completed-bonfire/', function (req, res) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
|
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
|
||||||
|
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
req.user.uncompletedBonfires.splice(index,1)
|
req.user.uncompletedBonfires.splice(index,1)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.user.save(function(err, user) {
|
req.user.save(function(err, user) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
if (user) {
|
if (user) {
|
||||||
res.redirect('/bonfires');
|
debug('Saving user');
|
||||||
|
res.send(true)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Unique Check API route
|
// Unique Check API route
|
||||||
|
@ -37,7 +37,7 @@ exports.returnNextBonfire = function(req, res, next) {
|
|||||||
return res.redirect('bonfires/meet-bonfire');
|
return res.redirect('bonfires/meet-bonfire');
|
||||||
}
|
}
|
||||||
var currentTime = parseInt(+new Date() / 1000);
|
var currentTime = parseInt(+new Date() / 1000);
|
||||||
if (currentTime - req.user.lastContentSync > 86400) {
|
if (currentTime - req.user.lastContentSync > 10) {
|
||||||
req.user.lastContentSync = currentTime;
|
req.user.lastContentSync = currentTime;
|
||||||
var completed = req.user.completedBonfires.map(function (elem) {
|
var completed = req.user.completedBonfires.map(function (elem) {
|
||||||
return elem._id;
|
return elem._id;
|
||||||
|
@ -214,10 +214,12 @@ var runTests = function(err, data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
if (allTestsPassed) {
|
if (allTestsPassed) {
|
||||||
allTestsPassed = false;
|
allTestsPassed = false;
|
||||||
showCompletion();
|
showCompletion();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -39,21 +39,33 @@ $(document).ready(function() {
|
|||||||
$('#complete-bonfire-dialog').modal('show');
|
$('#complete-bonfire-dialog').modal('show');
|
||||||
// Only post to server if there is an authenticated user
|
// Only post to server if there is an authenticated user
|
||||||
if ($('.signup-btn-nav').length < 1) {
|
if ($('.signup-btn-nav').length < 1) {
|
||||||
$.ajax({
|
|
||||||
type: 'POST',
|
$.post(
|
||||||
data: {
|
'/completed-bonfire',
|
||||||
|
{
|
||||||
bonfireInfo: {
|
bonfireInfo: {
|
||||||
completedWith : didCompleteWith,
|
completedWith : didCompleteWith,
|
||||||
solution: bonfireSolution,
|
solution: bonfireSolution,
|
||||||
bonfireHash: thisBonfireHash
|
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').on('click', function() {
|
||||||
$('#all-challenges-dialog').modal('show');
|
$('#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
|
// Bonfire instructions functions
|
||||||
$('#more-info').on('click', function() {
|
$('#more-info').on('click', function() {
|
||||||
|
Reference in New Issue
Block a user