-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseed.js
More file actions
215 lines (179 loc) · 6.01 KB
/
seed.js
File metadata and controls
215 lines (179 loc) · 6.01 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
This seed file is only a placeholder. It should be expanded and altered
to fit the development of your application.
It uses the same file the server uses to establish
the database connection:
--- server/db/index.js
The name of the database used is set in your environment files:
--- server/env/*
This seed file has a safety check to see if you already have users
in the database. If you are developing multiple applications with the
fsg scaffolding, keep in mind that fsg always uses the same database
name in the environment files.
*/
var mongoose = require('mongoose');
var Promise = require('bluebird');
var chalk = require('chalk');
var connectToDb = require('./server/db');
var User = Promise.promisifyAll(mongoose.model('User'));
var Product = Promise.promisifyAll(mongoose.model('Product'));
var Category = Promise.promisifyAll(mongoose.model('Category'));
var Review = Promise.promisifyAll(mongoose.model('Review'));
var Order = Promise.promisifyAll(mongoose.model('Order'));
var chance = require('chance')(123);
var _ = require('lodash');
var boatData = require('./boat-data/all_boats.json');
var seedUsers = function() {
var users = [{
email: 'testing@fsa.com',
password: 'password'
}, {
email: 'obama@gmail.com',
fullname: "Barack Hussein Obama II",
password: 'potus',
admin: true
}];
_.times(30, function() {
users.push({
email: chance.email(),
password: chance.string({length: 10}),
fullname: chance.name(),
admin: chance.bool({likelihood: 5})
});
})
return User.createAsync(users);
};
function randPhoto() {
var n = chance.natural({
min: 0,
max: 20
});
return 'https://s3.amazonaws.com/the-dock/images/boatImg' + n + '.jpg';
}
function randWords(minWords, maxWords) {
var numWords = chance.natural({
min: minWords,
max: maxWords
});
return chance.sentence({ words: numWords });
}
var categories = ['motor boat', 'cruise ship', 'pirate ship', 'party boat'];
function randInteger(minNum, maxNum) {
return chance.integer({ min: minNum, max: maxNum });
}
function randProduct(catId) {
var boat = boatData.pop();
return new Product({
title: boat.title,
description: randWords(10, 30),
price: randInteger(400, 900),
quantity: randInteger(60, 80),
categories: [catId],
photoUrl: boat.photoUrl
// this is photos of people for now
});
}
var products;
var users;
function createReview (user, product) {
return Review.create({
user: user,
product: product,
content: randWords(5,10),
starRating: 4
});
}
const clearDb = function() {
console.log("clearing database");
return Promise.map(['User', 'Product', 'Category', 'Order'], function(modelName) {
return mongoose.model(modelName).remove();
});
};
function seedProdCat(numOfProducts) {
var productDocs = [],
catIDs = [];
var categoriesObj = categories.map(function(elem) {
return { name: elem };
});
return Category.createAsync(categoriesObj)
.then(function(categoryDocs) {
catIDs = categoryDocs.map(elem => elem._id);
for (var i = 0; i < numOfProducts; i++) {
console.log(chance.pickone(catIDs));
// TODO update this to allow for multiple catagories
productDocs.push(randProduct(chance.pickone(catIDs)));
}
return productDocs;
});
}
var users;
var products;
connectToDb.then(function() {
clearDb()
.then(function() {
return Product.findAsync({});
})
.then(function(product) {
return Promise.map(seedProdCat(boatData.length), function(productDoc) {
return productDoc.save();
});
}).then(function(products) {
createdProducts = products;
return User.findAsync({}).then(function(users) {
return seedUsers();
});
})
.then(function() {
return User.find({});
})
.then(function(createdUsers) {
users = createdUsers;
return Product.find({});
})
.then(function(createdProducts) {
products = createdProducts;
var newOrders = [];
_.times(10, function() {
var purchasedItems = [];
_.times(3, function() {
var purchasedItem = {
product: chance.pickone(products),
quantity: chance.integer({ min: 1, max: 3 })
};
purchasedItems.push(purchasedItem);
});
var newOrder = {
purchasedItems: purchasedItems,
user: chance.pickone(users),
orderStatus: chance.pickone(['Created', 'Processing', 'Completed', 'Cancelled']),
streetAddress: "5 Hanover Sq",
city: "New York City",
state: "NY",
zipCode: 88888,
shippingEmail: chance.email()
};
newOrders.push(newOrder);
});
return Promise.map(newOrders, function(newOrder) {
return Order.create(newOrder);
});
})
.then(function () {
var reviewPromises = [
createReview(users[0], products[0]),
createReview(users[0], products[1]),
createReview(users[0], products[2]),
createReview(users[1], products[0]),
createReview(users[1], products[1]),
createReview(users[1], products[2]),
];
return Promise.all(reviewPromises);
})
.then(function () {
console.log(chalk.green('Seed successful!'));
process.kill(0);
}).catch(function (err) {
console.error(err);
process.kill(1);
});
});