-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.js
127 lines (110 loc) · 3.51 KB
/
importer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* EQ Reporting
* Importer
*
* This script imports the scraped data into the local database.
* (See scraper.js)
*
*/
require("dotenv").config();
const MongoClient = require("mongodb").MongoClient;
const Long = require("mongodb").Long;
const members = require("./scraper/scraped/members.json");
const companionships = require("./scraper/scraped/companionships.json");
const moment = require("moment");
let db;
async function collectionExists(name) {
const collections = await db.listCollections().toArray();
return collections.some((collection) => collection.name === name);
}
const importMembers = () => {
for (let family of members.families) {
var { headOfHouse, spouse, children, address, phone } = family;
var hasOthersInHouse = !!(spouse || children.length);
var familyPhone = normalizePhone(phone);
var all = [headOfHouse, spouse, ...children].filter(item => item);
var preparedHouseholdDocs = all.map(individual => {
if (individual) {
individual.address = address;
individual.isHeadOfHouse =
individual.individualId == headOfHouse.individualId;
individual.hasOthersInHouse = hasOthersInHouse;
individual.dateCreated = new Date();
individual.dateUpdated = new Date();
individual.birthdate = moment(individual.birthdate).toDate();
individual.spouseName = (individual.isHeadOfHouse && spouse) ? spouse.givenName1 : null;
let individualPhone = normalizePhone(individual.phone);
individual.phone = individualPhone
? individualPhone
: familyPhone;
console.log('Adding', individual.formattedName, individual.phone);
/* Ensure Id fields are converted to Integers */
for (let key of Object.keys(individual)) {
if (key.endsWith("Id")) {
individual[key] = individual[key] == 0
? null
: Long(individual[key]);
}
}
return individual;
}
});
insertDocuments("members", preparedHouseholdDocs);
}
};
const normalizePhone = phone => {
if (!phone || phone == "") {
return null;
}
if (phone[0] == "+") phone = phone.slice(1);
if (phone[0] == 1) phone = phone.slice(1);
var newPhone = phone.replace(/\D/g, "");
return `+1${newPhone}`;
};
const importCompanionships = () => {
let companionshipsToAdd = [];
for (district of companionships) {
for (companionship of district.companionships) {
companionship.teacherIndividualIds = companionship.teachers.map(t =>
Long(t.individualId)
);
companionship.assignmentIndividualIds = companionship.assignments.map(
a => Long(a.individualId)
);
companionship.dateStarted = moment(
companionship.startDate
).toDate();
delete companionship.startDate;
delete companionship.teachers;
companionshipsToAdd.push(companionship);
}
}
insertDocuments("companionships", companionshipsToAdd);
};
const createIndexes = () => {
db
.collection("members")
.createIndex({ individualId: 1 }, { background: true, w: 1 });
};
const insertDocuments = (collection, documents) => {
var collection = db.collection(collection);
collection.insertMany(documents);
};
async function start() {
db = await MongoClient.connect(
`${process.env.MONGO_HOST}:${process.env.MONGO_PORT}/${process.env.MONGO_DB}`
);
if (await collectionExists('members')) {
await db.collection("members").dropIndexes();
await db.collection("members").drop();
}
if (await collectionExists('companionships')) {
await db.collection("companionships").dropIndexes();
await db.collection("companionships").drop();
}
importMembers();
importCompanionships();
createIndexes();
db.close();
}
start();