Fix remove bonfire save endpoint

fix completedChallenge saving solution
remove unnecessary code.
This commit is contained in:
Berkeley Martinez
2015-12-08 13:52:03 -08:00
parent 9774638d43
commit 579c569d15
4 changed files with 32 additions and 132 deletions

View File

@ -78,14 +78,10 @@ window.common = (function(global) {
var publicURL = $('#public-url').val() || null; var publicURL = $('#public-url').val() || null;
var githubURL = $('#github-url').val() || null; var githubURL = $('#github-url').val() || null;
switch (common.challengeType) { switch (common.challengeType) {
case common.challengeTypes.HTML:
case common.challengeTypes.JS:
case common.challengeTypes.VIDEO: case common.challengeTypes.VIDEO:
data = { data = {
challengeInfo: { id: common.challengeId,
challengeId: common.challengeId, name: common.challengeName
challengeName: common.challengeName
}
}; };
$.post('/completed-challenge/', data) $.post('/completed-challenge/', data)
.success(function(res) { .success(function(res) {

View File

@ -16,7 +16,7 @@ window.common = (function(global) {
common.challengeName + ', Time: ' + time + ', Attempts: ' + 0 common.challengeName + ', Time: ' + time + ', Attempts: ' + 0
); );
var bonfireSolution = common.editor.getValue(); var solution = common.editor.getValue();
var didCompleteWith = $('#completed-with').val() || null; var didCompleteWith = $('#completed-with').val() || null;
$('#complete-courseware-dialog').modal('show'); $('#complete-courseware-dialog').modal('show');
@ -46,23 +46,20 @@ window.common = (function(global) {
next(); next();
}); });
$.post( const data = {
'/completed-bonfire/', { id: common.challengeId,
challengeInfo: { name: common.challengeName,
challengeId: common.challengeId, completedWith: didCompleteWith,
challengeName: common.challengeName, challengeType: common.challengeType,
completedWith: didCompleteWith, solution
challengeType: common.challengeType, };
solution: bonfireSolution
} $.post('/completed-challenge/', data, function(res) {
}, if (res) {
function(res) { window.location =
if (res) { '/challenges/next-challenge?id=' + common.challengeId;
window.location =
'/challenges/next-challenge?id=' + common.challengeId;
}
} }
); });
}); });
}; };

View File

@ -169,12 +169,10 @@ window.common = (function({ $, common = { init: [] }}) {
}); });
$.post( $.post(
'/completed-bonfire/', { '/completed-challenge/', {
challengeInfo: { id: common.challengeId,
challengeId: common.challengeId, name: common.challengeName,
challengeName: common.challengeName, challengeType: common.challengeType
challengeType: common.challengeType
}
}, },
function(res) { function(res) {
if (res) { if (res) {

View File

@ -41,14 +41,6 @@ const challengeView = {
7: 'coursewares/showStep' 7: 'coursewares/showStep'
}; };
/*
function makeChallengesUnique(challengeArr) {
// clone and reverse challenges
// then filter by unique id's
// then reverse again
return _.uniq(challengeArr.slice().reverse(), 'id').reverse();
}
*/
function numberWithCommas(x) { function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
} }
@ -56,14 +48,6 @@ function numberWithCommas(x) {
function updateUserProgress(user, challengeId, completedChallenge) { function updateUserProgress(user, challengeId, completedChallenge) {
let { completedChallenges } = user; let { completedChallenges } = user;
// migrate user challenges object to remove
/* if (!user.isUniqMigrated) {
user.isUniqMigrated = true;
completedChallenges = user.completedChallenges =
makeChallengesUnique(completedChallenges);
}*/
const indexOfChallenge = _.findIndex(completedChallenges, { const indexOfChallenge = _.findIndex(completedChallenges, {
id: challengeId id: challengeId
}); });
@ -153,11 +137,6 @@ module.exports = function(app) {
send200toNonUser, send200toNonUser,
completedZiplineOrBasejump completedZiplineOrBasejump
); );
router.post(
'/completed-bonfire',
send200toNonUser,
completedBonfire
);
router.get('/map', challengeMap); router.get('/map', challengeMap);
router.get( router.get(
@ -342,95 +321,25 @@ module.exports = function(app) {
); );
} }
function completedBonfire(req, res, next) {
debug('compltedBonfire');
var completedWith = req.body.challengeInfo.completedWith || false;
var challengeId = req.body.challengeInfo.challengeId;
var challengeData = {
id: challengeId,
name: req.body.challengeInfo.challengeName || '',
completedDate: Math.round(+new Date()),
solution: req.body.challengeInfo.solution,
challengeType: 5
};
observeQuery(
User,
'findOne',
{ where: { username: ('' + completedWith).toLowerCase() } }
)
.doOnNext(function(pairedWith) {
debug('paired with ', pairedWith);
if (pairedWith) {
updateUserProgress(
pairedWith,
challengeId,
assign({ completedWith: req.user.id }, challengeData)
);
}
})
.withLatestFrom(
Observable.just(req.user),
function(pairedWith, user) {
return {
user: user,
pairedWith: pairedWith
};
}
)
// side effects should always be done in do's and taps
.doOnNext(function(dats) {
updateUserProgress(
dats.user,
challengeId,
dats.pairedWith ?
// paired programmer found and adding to data
assign({ completedWith: dats.pairedWith.id }, challengeData) :
// user said they paired, but pair wasn't found
challengeData
);
})
// iterate users
.flatMap(function(dats) {
debug('flatmap');
return Observable.from([dats.user, dats.pairedWith]);
})
// save user
.flatMap(function(user) {
// save user will do nothing if user is falsey
return saveUser(user);
})
.subscribe(
function(user) {
debug('onNext');
if (user) {
debug('user %s saved', user.username);
}
},
next,
function() {
debug('completed');
return res.status(200).send(true);
}
);
}
function completedChallenge(req, res, next) { function completedChallenge(req, res, next) {
const completedDate = Math.round(+new Date()); const completedDate = Math.round(+new Date());
const { id, name } = req.body; const {
const { challengeId, challengeName } = req.body.challengeInfo || {}; id,
name,
challengeType,
solution
} = req.body;
updateUserProgress( updateUserProgress(
req.user, req.user,
id || challengeId, id,
{ {
id: id || challengeId, id,
completedDate: completedDate, challengeType,
name: name || challengeName || '', solution,
solution: null, name,
githubLink: null, completedDate,
verified: true verified: true
} }
); );