-
Notifications
You must be signed in to change notification settings - Fork 2
/
seedEmsDataCsv.js
202 lines (167 loc) · 6.88 KB
/
seedEmsDataCsv.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var mongoose = require('mongoose');
var csv = require('fast-csv');
var Venues = require("./models/venues");
var Catering = require("./models/catering");
var Decorations = require("./models/decorations");
var Comment = require("./models/comment");
var User = require("./models/user")
var csvHeaders = {
VENUES: {
headers: ['name', 'image', 'price', 'description', 'location',
'capacity', 'category', 'contactno', 'cateringAvailable', 'decorationAvailable']
},
CATERING: {
headers: ['name', 'image', 'description', 'location', 'price', 'beverages', 'contactno']
},
DECORATIONS: {
headers: ['name', 'image', 'description', 'location', 'price', 'contactno']
}
};
function userRegistration() {
// remove all users
User.deleteMany({}, function(err){
if(err){
console.log(err);
}
// console.log("removed all users");
// create admin user
User.register({username: "admin"}, "1234", function(err, user){
if(err) {
console.log(err);
}
else {
user.save(function (err) {
if (err)
console.log(err);
else
console.log("created admin account")
});
// Add sample comments to business
addComment();
}
});
});
}
var cmntData = [{text: "This decorator is best in the city"},
{text: "This venue is suitable for all kinds of events."},
{text: "Tasty food with fair price."}];
function addComment() {
// remove all comments
Comment.deleteMany({}, function(err){
if(err){
console.log(err);
}
// console.log("removed all comments!");
User.find({}, function(err, foundUsers){
foundUsers.map(foundUser => {
// console.log("MK", foundUser)
Decorations.find({}, function(err, foundDecos){
foundDecos.map(foundDeco => {
foundDeco.author.id = foundUser._id;
foundDeco.author.username = foundUser.username;
// create new comment
Comment.create(cmntData[0], function(err, comment){
if(err){
req.flash("error", "something went wrong");
console.log(err);
}else{
// add username and id to the comment
comment.author.id = foundUser._id;
comment.author.username = foundUser.username;
//save comment
comment.save();
// connect new comment to the campgound
foundDeco.comments.push(comment);
foundDeco.save(); // saving the changes to the DB
}
});
});
});
Venues.find({}, function(err, foundVenues){
foundVenues.map(foundVenue => {
foundVenue.author.id = foundUser._id;
foundVenue.author.username = foundUser.username;
// create new comment
Comment.create(cmntData[1], function(err, comment){
if(err){
req.flash("error", "something went wrong");
console.log(err);
}else{
// add username and id to the comment
comment.author.id = foundUser._id;
comment.author.username = foundUser.username;
//save comment
comment.save();
// connect new comment to the campgound
foundVenue.comments.push(comment);
foundVenue.save(); // saving the changes to the DB
}
});
});
});
Catering.find({}, function(err, foundCatering){
foundCatering.map(foundCateror => {
foundCateror.author.id = foundUser._id;
foundCateror.author.username = foundUser.username;
// create new comment
Comment.create(cmntData[2], function(err, comment){
if(err){
req.flash("error", "something went wrong");
console.log(err);
}else{
// add username and id to the comment
comment.author.id = foundUser._id;
comment.author.username = foundUser.username;
//save comment
comment.save();
// connect new comment to the campgound
foundCateror.comments.push(comment);
foundCateror.save(); // saving the changes to the DB
}
});
});
});
});
});
});
}
function importCSVFile(filePath, modelSchema, modelName) {
// Remove existing entries
modelSchema.deleteMany({}, function(err){
if(err){
console.log(err);
}
//console.log("removed all " + modelName);
// Populate data from CSV
csv
.parseFile(filePath, {headers: true, ignoreEmpty: true})
.on('data', function(data) {
//console.log(data);
var Obj = mongoose.model(modelName);
var obj = new Obj();
Object.keys(data).forEach(function(key) {
var val = data[key];
//console.log(key);
//console.log(val);
if (val !== '')
obj.set(key, val);
});
obj.save(function (err) {
if (err)
console.log(err);
});
})
.on('end', function() {
console.log(modelName + " data loaded");
});
});
}
function seedEmsDataCsv() {
// Import CSV to MongoDB
importCSVFile(__dirname + '/dbdata/venues.csv', Venues, 'venues');
importCSVFile(__dirname + '/dbdata/catering.csv', Catering, 'catering');
importCSVFile(__dirname + '/dbdata/decorations.csv', Decorations, 'decorations');
// Register admin and add admin comment to business
userRegistration();
}
module.exports = seedEmsDataCsv;