-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfillDB.js
More file actions
123 lines (105 loc) · 3.23 KB
/
fillDB.js
File metadata and controls
123 lines (105 loc) · 3.23 KB
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
var mongoose = require('mongoose')
, companyService = require('./server/services/companyService')
, Company = require('./server/models/company')
, config = require('./server/config')
;
var start = Date.now();
var log = function (message) {
console.log("[" + (Date.now() - start) + "] " + message);
};
log("Trying to connect to db...");
var dbUrl = config.dbUrl;
console.log("DB URL: " + dbUrl);
mongoose.connect(dbUrl); // comment
mongoose.connection.on('error', function (err, req, res, next) {
log("Cant connect to MongoDB - please verify that it was started.");
});
mongoose.connection.once('open', function callback() {
log("Connected to db");
});
log("Begin server.js");
var fs = require('fs');
var cities = [
'ירושליים'
, 'ירושלים'
, 'תל אביב'
, 'יקום'
, 'חיפה'
, 'קרית גת'
, 'פתח תקוה'
, 'יקנעם'
, 'ראש העין'
, 'בני ברק'
, 'ראשון לציון'
, 'קיסריה'
, 'מגדל העמק'
, 'רמת גן'
, 'גבעתיים'
, 'הרצלייה'
, 'יהוד'
, 'אור יהודה'
, 'רחובות'
, 'גבעת שמואל'
, 'כפר סבא'
, 'רעננה'
, 'חולון'
, 'רמת השרון'
, 'הוד השרון'
, 'יבנה'
, 'קרית מוצקין'
, 'קרית ביאליק'
, 'טירת הכרמל'
, 'באר שבע'
, 'אילת'
, 'חדרה'
, 'נתניה'
];
function searchCity(address) {
for (var i = 0; i < cities.length; i++) {
var city = cities[i];
if (address.indexOf(city) > -1) {
return city
}
}
return "";
}
// 1. find a specific company with multi locations
// 2. cut and paste the city
// 3. repeat for all
var extractCity = function (street) {
for (var i = 0; i < cities.length; i++) {
var city = cities[i];
//if (street.toLowerCase().indexOf(city.toLowerCase()) > -1)
if (street.indexOf(city) > -1)
return city;
}
return null;
};
companyService.getCompanies()
.then(function (companies) {
for (var i = 0; i < companies.length; i++) {
var company = companies[i];
// Setting locations
for (var l = 0; l < company.locations.length; l++) {
var location = company.locations[l];
if (!location || !(location.street))
continue;
var street = location.street;
console.log(i + ": " + street);
var cityFound = extractCity(street);
if (cityFound) {
//console.log("found! " + cityFound);
location.city = cityFound;
var re = new RegExp("[ ,]*" + cityFound, "");
location.street = location.street.replace(re, "");
console.log("city:" + location.city);
console.log("street:" + location.street);
// TODO 1. enable save 2. backup db before running!
company.save();
}
}
//if (i == 30)
// return;
//companyService.updateCompany(company);
}
});