forked from connorbrett/tutor-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_requests.js
70 lines (59 loc) · 2.58 KB
/
migrate_requests.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
59
60
61
62
63
64
65
66
67
68
69
70
const {MongoClient} = require('mongodb');
// Set these to the appropriate variables.
const fromDbUsername = process.env.FROM_DB_USERNAME;
const fromDbPassword = process.env.FROM_DB_PASSWORD;
const toDbUsername = process.env.TO_DB_USERNAME;
const toDbPassword = process.env.TO_DB_PASSWORD;
const FROM_DB = 'tutorQueue';
const TO_DB = 'tutorCenter';
const fromUri = `mongodb+srv://${fromDbUsername}:${fromDbPassword}@cluster0.qs95z.mongodb.net/?retryWrites=true&w=majority`;
const toUri = `mongodb+srv://${toDbUsername}:${toDbPassword}@cluster0.qs95z.mongodb.net/?retryWrites=true&w=majority`;
async function getItems(client, dbName, collectionName){
const items = await client.db(dbName).collection(collectionName).find({}).toArray();
return items;
}
async function main(){
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const fromClient = new MongoClient(fromUri);
const toClient = new MongoClient(toUri);
try {
// Connect to the MongoDB cluster
await fromClient.connect();
await toClient.connect();
const courseObjs = await getItems(toClient, TO_DB, 'tutor_center_course')
const pre = await getItems(fromClient, FROM_DB, 'tutors');
const migratedTutors = await getItems(toClient, TO_DB, 'tutor_center_tutor')
const idMap = {};
for(let tutor of pre){
idMap[tutor._id] = migratedTutors.find(e=>e.email === tutor.email)._id
}
console.log(idMap);
for(let request of await getItems(fromClient, FROM_DB, "tutorrequests")){
const course = courseObjs.find(e=>e.code === request.course.replace(" ", ''));
if(course){
const reqRes = await toClient.db(TO_DB).collection('tutor_center_tutoringrequest').insertOne(
{
name: request.name,
email: request.email,
status: request.status,
created_time: request.submitted,
modified_time: new Date(),
description: request.description,
closed_time: request.submitted,
requested_course_id: course._id,
tutor_id: idMap[request.tutor]
}
);
}
}
} catch (e) {
console.error(e);
} finally {
await fromClient.close();
await toClient.close();
}
}
main().catch(console.error);