fix(current-challenge): Fix current-challenge API

This commit is contained in:
Bouncey
2018-10-28 06:18:13 +00:00
committed by mrugesh mohapatra
parent e7316e4567
commit dace86663f
4 changed files with 144 additions and 128 deletions

View File

@ -1,7 +1,7 @@
const path = require('path');
const fs = require('fs');
require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') });
const MongoClient = require('mongodb').MongoClient;
const { MongoClient, ObjectID } = require('mongodb');
const { getChallengesForLang } = require('@freecodecamp/curriculum');
const { flatten } = require('lodash');
const debug = require('debug');
@ -9,7 +9,7 @@ const debug = require('debug');
const { createPathMigrationMap } = require('./createPathMigrationMap');
const log = debug('fcc:tools:seedChallenges');
const { MONGOHQ_URL, LOCALE: lang } = process.env;
const { MONGOHQ_URL, LOCALE: lang = 'english' } = process.env;
function handleError(err, client) {
if (err) {
@ -32,14 +32,16 @@ MongoClient.connect(
function(err, client) {
handleError(err, client);
log('Connected successfully to mongo');
log('Connected successfully to mongo at %s', MONGOHQ_URL);
const db = client.db('freecodecamp');
const challenges = db.collection('challenge');
const challengeCollection = db.collection('challenge');
challenges.deleteMany({}, err => {
challengeCollection.deleteMany({}, err => {
handleError(err, client);
log('deleted all the challenges');
const curriculum = getChallengesForLang(lang);
const allChallenges = Object.keys(curriculum)
@ -51,18 +53,25 @@ MongoClient.connect(
return [...challengeArray, ...flatten(challengesForBlock)];
}, [])
.map(challenge => {
challenge._id = challenge.id.slice(0);
const currentId = challenge.id.slice(0);
challenge._id = ObjectID(currentId);
delete challenge.id;
return challenge;
});
try {
challenges.insertMany(allChallenges, { ordered: false });
challengeCollection.insertMany(
allChallenges,
{ ordered: false },
err => {
handleError(err, client);
log('challenge seed complete');
client.close();
}
);
} catch (e) {
handleError(e, client);
} finally {
log('challenge seed complete');
client.close();
log('generating path migration map');
const pathMap = createPathMigrationMap(curriculum);
const outputDir = path.resolve(