-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkCities.js
49 lines (44 loc) · 1.24 KB
/
checkCities.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
const resources = require("./data/resources.json");
const allCities = require("./data/newAllCities.json");
const { normalize } = require("./parser");
const findState = require("./utils/findState");
const Contact = require("./models/Contact.schema");
const City = require("./models/City.schema");
const checkCities = async () => {
for (const state in allCities) {
for (const city of allCities[state]) {
console.time(city.name);
let cityObj = {
city: city.name,
state: findState(normalize(String(city.name))),
resourceCount: {},
};
for (const resource in resources) {
cityObj.resourceCount[resource] = {
count: Number(
await Contact.countDocuments({
resource_type: resource,
city: city.name,
})
),
};
}
cityObj.totalContacts = Object.values(cityObj.resourceCount)
.map((resObj) => {
return resObj.count;
})
.reduce((acc, val) => {
return acc + val;
});
await City.updateOne(
{
city: city.name,
},
cityObj,
{ upsert: true }
);
console.timeEnd(city.name);
}
}
};
module.exports = { checkCities };