-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseed.js
111 lines (103 loc) · 2.57 KB
/
seed.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
const db = require('./models');
function addRentee() {
const numRentee = 3;
let rentees = [];
for (let i = 0; i < numRentee; i++) {
rentees.push({
username: `Rentee ${i}`,
email: `r${i}@m.com`,
password: 'qq',
isOwner: false
})
}
rentees.forEach(rentee => {
db.User
.findOne({ email: rentee.email })
.then(dbRentee => {
if (dbRentee === null)
db.User
.create(rentee)
.then(dbRentee => console.log(`Created user: ${dbRentee.username}`))
.catch(err => console.log(err))
else
console.log(`User with email: ${rentee.email} already exists`);
})
});
}
function addReqests() {
db.User
.find({ isOwner: false })
.then(dbRentees => {
dbRentees.forEach(rentee => {
console.log(rentee._id)
createRequests(rentee._id)
})
})
}
function createRequests(renteeId) {
const numeReqs = 5;
requests = [];
for (let i = 0; i < numeReqs; i++) {
console.log('Creating reqs: ', i);
time = new Date();
time.setDate(time.getDate() + Math.floor(Math.random() * 10));
requests.push({
renteeId,
item: `Item # ${i}`,
priceInitial: Math.floor(Math.random() * 20) + 10,
location: `Location ${i}`,
time
});
}
db.Request
.create(requests)
.then(dbReqs => console.log(dbReqs))
.catch(err => console.log(err))
}
function addOwner() {
const numOwners = 3;
let owners = [];
for (let i = 0; i < numOwners; i++) {
owners.push({
username: `Owner ${i}`,
email: `o${i}@m.com`,
password: 'qq',
isOwner: true
})
}
owners.forEach(owner => {
db.User
.findOne({ email: owner.email })
.then(dbOwner => {
if (dbOwner === null)
db.User
.create(owner)
.then(dbOwner => console.log(`Created owner: ${dbOwner.username}`))
.catch(err => console.log(err))
else
console.log(`Owner: ${owner.username} already exists`);
})
});
}
function addOffers() {
db.User
.find({ isOwner: true })
.then(dbOwners => dbOwners.forEach(owner => createOffer(owner._id)))
.catch(err => console.log(err))
}
function createOffer(ownerId) {
db.Request
.find({})
.then(requests => (
requests.forEach(request => {
db.Offer
.create({
ownerId,
requestId: request._id,
price: request.priceInitial - 1
})
})
))
.catch(err => console.log(err))
}
module.exports = { addRentee, addOwner, addReqests, addOffers };