Change migration middleware to not rely on boolean flag in User model
This commit is contained in:
19
app.js
19
app.js
@ -216,10 +216,6 @@ app.use(function (req, res, next) {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// User migration middleware
|
|
||||||
|
|
||||||
app.use(userController.userMigration);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main routes.
|
* Main routes.
|
||||||
*/
|
*/
|
||||||
@ -529,7 +525,7 @@ app.get('/field-guide/all-articles', fieldGuideController.showAllFieldGuides);
|
|||||||
|
|
||||||
app.get('/field-guide/:fieldGuideName',
|
app.get('/field-guide/:fieldGuideName',
|
||||||
fieldGuideController.returnIndividualFieldGuide
|
fieldGuideController.returnIndividualFieldGuide
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get('/field-guide/', fieldGuideController.returnNextFieldGuide);
|
app.get('/field-guide/', fieldGuideController.returnNextFieldGuide);
|
||||||
|
|
||||||
@ -540,14 +536,19 @@ app.post('/completed-field-guide/', fieldGuideController.completedFieldGuide);
|
|||||||
* Challenge related routes
|
* Challenge related routes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
app.get('/challenges/next-challenge', challengeController.returnNextChallenge);
|
app.get('/challenges/next-challenge',
|
||||||
|
userController.userMigration,
|
||||||
|
challengeController.returnNextChallenge);
|
||||||
|
|
||||||
app.get(
|
app.get(
|
||||||
'/challenges/:challengeName',
|
'/challenges/:challengeName',
|
||||||
|
userController.userMigration,
|
||||||
challengeController.returnIndividualChallenge
|
challengeController.returnIndividualChallenge
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get('/challenges/', challengeController.returnCurrentChallenge);
|
app.get('/challenges/',
|
||||||
|
userController.userMigration,
|
||||||
|
challengeController.returnCurrentChallenge);
|
||||||
// todo refactor these routes
|
// todo refactor these routes
|
||||||
app.post('/completed-challenge/', challengeController.completedChallenge);
|
app.post('/completed-challenge/', challengeController.completedChallenge);
|
||||||
|
|
||||||
@ -559,11 +560,11 @@ app.post('/completed-bonfire', challengeController.completedBonfire);
|
|||||||
// Unique Check API route
|
// Unique Check API route
|
||||||
app.get('/api/checkUniqueUsername/:username',
|
app.get('/api/checkUniqueUsername/:username',
|
||||||
userController.checkUniqueUsername
|
userController.checkUniqueUsername
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get('/api/checkExistingUsername/:username',
|
app.get('/api/checkExistingUsername/:username',
|
||||||
userController.checkExistingUsername
|
userController.checkExistingUsername
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get('/api/checkUniqueEmail/:email', userController.checkUniqueEmail);
|
app.get('/api/checkUniqueEmail/:email', userController.checkUniqueEmail);
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ var _ = require('lodash'),
|
|||||||
User = require('../models/User'),
|
User = require('../models/User'),
|
||||||
secrets = require('../config/secrets'),
|
secrets = require('../config/secrets'),
|
||||||
moment = require('moment'),
|
moment = require('moment'),
|
||||||
debug = require('debug')('freecc:cntr:challenges'),
|
debug = require('debug')('freecc:cntr:userController'),
|
||||||
resources = require('./resources'),
|
resources = require('./resources'),
|
||||||
R = require('ramda');
|
R = require('ramda');
|
||||||
|
|
||||||
@ -21,15 +21,17 @@ var _ = require('lodash'),
|
|||||||
* challenge structure
|
* challenge structure
|
||||||
*/
|
*/
|
||||||
exports.userMigration = function(req, res, next) {
|
exports.userMigration = function(req, res, next) {
|
||||||
|
if (req.user && req.user.completedChallenges.length === 0) {
|
||||||
debug('user migration called');
|
debug('user migration called');
|
||||||
var user = req.user;
|
debug('Completed coursewares', req.user.completedCoursewares);
|
||||||
if (!user.migratedToUnifiedChallengeStructure && user) {
|
debug('Completed bonfires', req.user.completedBonfires);
|
||||||
user.migratedToUnifiedChallengeStructure = true;
|
req.user.completedChallenges = R.filter(function (elem) {
|
||||||
user.completedChallenges = R.filter(function (elem) {
|
|
||||||
return elem; // getting rid of undefined
|
return elem; // getting rid of undefined
|
||||||
}, R.concat(
|
}, R.concat(
|
||||||
user.completedCoursewares,
|
req.user.completedCoursewares,
|
||||||
user.completedBonfires.map(function (bonfire) {
|
req.user.completedBonfires.map(function (bonfire) {
|
||||||
|
debug('Completed coursewares', req.user.completedCoursewares);
|
||||||
|
debug('Completed bonfires', req.user.completedBonfires);
|
||||||
return ({
|
return ({
|
||||||
completedDate: bonfire.completedDate,
|
completedDate: bonfire.completedDate,
|
||||||
_id: bonfire._id,
|
_id: bonfire._id,
|
||||||
@ -42,15 +44,11 @@ exports.userMigration = function(req, res, next) {
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
));
|
));
|
||||||
user.save(function (err) {
|
debug(req.user.completedChallenges);
|
||||||
if (err) {
|
next();
|
||||||
next(err);
|
|
||||||
} else {
|
} else {
|
||||||
next(req, res);
|
next();
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
next(req, res);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -168,7 +168,7 @@ var userSchema = new mongoose.Schema({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
uncompletedChallenges: Array
|
uncompletedChallenges: Array,
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,7 +103,7 @@
|
|||||||
"Complete \"Update\"",
|
"Complete \"Update\"",
|
||||||
"Complete \"RM\"",
|
"Complete \"RM\"",
|
||||||
"Complete \"Finale\"",
|
"Complete \"Finale\"",
|
||||||
"Once you've completed these first 7 challenges, move on to our next waypoint.",
|
"Once you've completed these first 7 challenges, move on to our next waypoint."
|
||||||
],
|
],
|
||||||
"challengeType": 2,
|
"challengeType": 2,
|
||||||
"tests": []
|
"tests": []
|
||||||
@ -135,7 +135,7 @@
|
|||||||
"Complete \"Filtered LS\"",
|
"Complete \"Filtered LS\"",
|
||||||
"Complete \"Make it Modular\"",
|
"Complete \"Make it Modular\"",
|
||||||
"Complete \"HTTP Client\"",
|
"Complete \"HTTP Client\"",
|
||||||
"Once you've completed these first 7 challenges, move on to our next waypoint.",
|
"Once you've completed these first 7 challenges, move on to our next waypoint."
|
||||||
],
|
],
|
||||||
"challengeType": 2,
|
"challengeType": 2,
|
||||||
"tests": []
|
"tests": []
|
||||||
|
Reference in New Issue
Block a user