This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
populate.js
76 lines (70 loc) · 2.05 KB
/
populate.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
const fs = require("fs");
const colors = require("colors");
const dotenv = require("dotenv");
const geocoder = require("./utils/geocoder");
const connectDB = require("./config/db");
// Load env vars
dotenv.config({ path: "./config/config.env" });
// Load model
const Cabinet = require("./models/Cabinet");
const Center = require("./models/Center");
// Connect to database
connectDB().catch((err) => {
console.log(err);
process.exit(1); // Close & exit if connection to the database fails.
});
// Read JSON file that contains the data
const data = JSON.parse(
fs.readFileSync(`${__dirname}/inputlast.json`, "utf-8")
);
data.forEach(async (e, i) => {
setTimeout(async () => {
const res = await geocoder.reverse({
lat: e.position.lat,
lon: e.position.lng,
});
e.address = {
full: res[0].formattedAddress,
streetNumber: res[0].streetNumber,
streetName: res[0].streetName,
city: res[0].city,
country: res[0].country,
countryCode: res[0].countryCode,
zipcode: res[0].zipcode,
administrationLevels: res[0].administrationLevels,
};
await Cabinet.create(e).then((c) => console.log(c));
}, 5000 * (i + 1));
});
const centers = data.filter((d) => d.type === "DSLAM");
centers.forEach(async (e, i) => {
setTimeout(async () => {
let payload = {
id: e.id,
name: e.img_url,
position: e.position,
verified: true,
approved: true,
username: undefined,
createdAt: Date.now(),
};
const res = await geocoder.reverse({
lat: e.position.lat,
lon: e.position.lng,
});
payload.address = {
full: res[0].formattedAddress,
streetNumber: res[0].streetNumber,
streetName: res[0].streetName,
city: res[0].city,
country: res[0].country,
countryCode: res[0].countryCode,
zipcode: res[0].zipcode,
administrationLevels: res[0].administrationLevels,
};
payload.img_url = undefined;
await Center.create(payload)
.then((c) => console.log(c))
.catch((e) => console.log(e));
}, 5000 * i);
});