forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge-migration.js
58 lines (51 loc) · 1.53 KB
/
challenge-migration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require('dotenv').load();
var bonfires = require('./bonfires.json'),
app = require('../server/server'),
mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient,
User = app.models.User,
UserIdentity = app.models.userIdentity,
oldUri='mongodb://localhost:27017/app30893198',
coursewares = require('./coursewares.json');
var counter = 0;
var offerings = 2;
var CompletionMonitor = function() {
counter++;
console.log('call ' + counter);
if (counter < offerings) {
return;
} else {
process.exit(0);
}
};
MongoClient.connect(oldUri, function(err, database) {
database.collection('users').find({}).batchSize(20).toArray(function(err, users) {
if (users !== null && users.length !== 0) {
var mappedUserArray = users.map(function(user) {
Object.keys(user.profile).forEach(function(prop) {
user[prop] = user.profile[prop];
});
Object.keys(user.portfolio).forEach(function(prop) {
user[prop] = user.portfolio[prop];
});
user.completedCoursewares = Object.keys(user.challengesHash)
.filter(function(key) {
return user.challengesHash[key] !== 0;
})
.map(function(key) {
return({
id: coursewares[key].id,
completedDate: user.challengesHash[key]
});
});
return user;
});
User.create(mappedUserArray, function(err) {
if (err) {
console.log(err);
}
console.log("a batch finished");
});
}
});
});