-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_import.js
More file actions
161 lines (156 loc) · 6.89 KB
/
data_import.js
File metadata and controls
161 lines (156 loc) · 6.89 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var
url = require('url'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
config = require('./config.js'),
//db = mongoose.connect(config.creds.mongoose_auth),
schemas = require('./schema.js'),
tmp_brands = require('./tmp_data/brands.js'),
tmp_domains = require('./tmp_data/cigar_domain_values.js'),
tmp_cigars = [],
Brand = mongoose.model('Brand', schemas.BrandSchema),
AttributeDomain = mongoose.model('AttributeDomain', schemas.AttributeDomainSchema),
Cigar = mongoose.model('Cigar', schemas.CigarSchema),
User = mongoose.model('User', schemas.UserSchema),
App = mongoose.model('App', schemas.AppSchema),
UpdateRequest = mongoose.model('UpdateRequest', schemas.UpdateRequestSchema),
DeleteRequest = mongoose.model('DeleteRequest', schemas.DeleteRequestSchema);
function populateDB() {
// brands
User.find().exec(function (err, docs) {
if (docs.length == 0) {
console.log('Adding users and keys');
var new_user_id;
var new_user = new User();
new_user.username = 'aaron.murray';
new_user.email = 'aaron.murray@cigardb.co';
new_user.date_joined = new Date();
new_user.save(function (err, new_user) {
new_user_id = new_user.id;
});
var new_key = new App();
new_key.api_key = '2483f102-e4ae-4b41-b56f-e9e344ef9083';
new_key.user_id = new_user_id;
new_key.access_level = 99;
new_key.date_created = new Date();
new_key.save();
var new_user = new User();
new_user.username = 'developer_test';
new_user.email = 'foo@bar.com';
new_user.date_joined = new Date();
new_user.save(function (err, new_user) {
new_user_id = new_user.id;
});
var new_key = new App();
new_key.api_key = 'ca9e8600-ab8d-4181-940d-b57cd8277dab';
new_key.user_id = new_user_id;
new_key.access_level = 0;
new_key.date_created = new Date();
new_key.save();
var new_user = new User();
new_user.username = 'premium_test';
new_user.email = 'bar@foo.com';
new_user.date_joined = new Date();
new_user.save(function (err, new_user) {
new_user_id = new_user.id;
});
var new_key = new App();
new_key.api_key = '1f29f79b-7303-43b2-8bd1-6c5d5d4dc13d';
new_key.user_id = new_user_id;
new_key.access_level = 1;
new_key.date_created = new Date();
new_key.save();
}
});
Brand.find().exec(function (err, docs) {
if (docs.length == 0) {
console.log('No brands found, populating Brands collection.');
for (var i = 0; tmp_brands[i]; i++) {
var new_brand = new Brand();
new_brand.name = tmp_brands[i];
new_brand.status = 'approved';
new_brand.updated = new Date();
new_brand.save();
}
}
});
AttributeDomain.find().exec(function (err, docs) {
if (docs.length == 0) {
console.log('No attribute domains found, populating AttributeDomains collection.');
var new_dom = new AttributeDomain();
new_dom.binders = tmp_domains.binders;
new_dom.color = tmp_domains.color;
new_dom.country = tmp_domains.country;
new_dom.fillers = tmp_domains.fillers;
new_dom.strength = tmp_domains.strength;
new_dom.wrappers = tmp_domains.wrappers;
new_dom.vitola = tmp_domains.vitola;
new_dom.save();
}
});
Cigar.find().limit(1).exec(function (err, docs) {
if (docs.length == 0) {
console.log('LOADING CIGAR DATA');
tmp_cigars[1] = require('./tmp_data/cigars1.js');
tmp_cigars[2] = require('./tmp_data/cigars2.js');
tmp_cigars[3] = require('./tmp_data/cigars3.js');
tmp_cigars[4] = require('./tmp_data/cigars4.js');
var total_cigars = 0;
for (var i = 1; i <= 4; i++) {
for (var j = 0; tmp_cigars[i][j]; j++) {
var curr_cigar = tmp_cigars[i][j];
if (curr_cigar['binder']) {
curr_cigar.binder = curr_cigar.binder.split(',');
curr_cigar.binder = clean_domain_values(curr_cigar.binder, tmp_domains.binders);
}
if (curr_cigar['filler']) {
curr_cigar.filler = curr_cigar.filler.split(',');
curr_cigar.filler = clean_domain_values(curr_cigar.filler, tmp_domains.fillers);
}
if (curr_cigar['wrapper']) {
curr_cigar.wrapper = curr_cigar.wrapper.split(',');
curr_cigar.wrapper = clean_domain_values(curr_cigar.wrapper, tmp_domains.wrappers);
}
var new_cigar = new Cigar();
new_cigar.name = curr_cigar.cigar_name;
new_cigar.brand = curr_cigar.brand;
new_cigar.color = curr_cigar.color;
new_cigar.length = curr_cigar.length;
new_cigar.ring_gauge = curr_cigar.ring_gauge;
new_cigar.wrappers = curr_cigar.wrapper;
new_cigar.fillers = curr_cigar.filler;
new_cigar.binders = curr_cigar.binder;
new_cigar.country = curr_cigar.country_manufactured;
new_cigar.strength = curr_cigar.strength;
new_cigar.vitola = curr_cigar.shape;
new_cigar.save(function (err, product) {
if (err) {
console.log('ERROR saving cigar: ' + JSON.stringify(new_cigar));
} else {
console.log('Saved cigar: ' + product.name);
total_cigars++;
}
});
}
}
}
})
}
function clean_domain_values(raw_data, valid_values) {
if (!('contains' in String.prototype)) {
String.prototype.contains = function (str, startIndex) {
return -1 !== this.indexOf(str, startIndex);
};
}
var cleaned_values = valid_values.filter(function (value) {
if (value == 'Connecticut') {
return raw_data[0].contains(value) && (!raw_data[0].contains('Connecticut Broadleaf') && !raw_data[0].contains('Connecticut Shade'));
} else if (value == 'Criollo') {
return raw_data[0].contains(value) && (!raw_data[0].contains('Havana 98 Criollo'));
} else {
return raw_data[0].contains(value);
}
});
return cleaned_values;
}
module.exports = populateDB;