forked from nodebusters/ask-izzy-plus-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseedDatabase.js
212 lines (188 loc) · 6.47 KB
/
seedDatabase.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
203
204
205
206
207
208
209
210
211
212
// MONGOOSE: Connection configuration to MongoDB database via Mongoose library
// DOTENV: Reads MONGO_DRIVER url from .env file
console.clear();
require('dotenv').load();
const mongoose = require('mongoose');
// MONGOOSE: Connects to the MongoDB database on localhost:27017
const driver = process.env.MONGO_DRIVER_PROD;
// const driver = process.env.MONGO_DRIVER;
mongoose.connect(driver);
// MONGOOSE: Connection event - when successfully connected
const Organisation = require('./models/Organisation');
const User = require('./models/User');
const AdminUser = require('./models/AdminUser');
mongoose.connection.on('connected', () => {
console.log('connected to mongod');
console.log('-------------------------------');
async function dropDatabase(){
return new Promise ((resolve,reject)=>{
Organisation.deleteMany({})
.then(()=>{
User.deleteMany({})
.then(()=>{
resolve('done');
})
})
})
}
async function seedOrganisations(){
return new Promise ((resolve,reject)=>{
// const siteData = {id:1}
// To seed up to five organisations, set index to 1-5
for (let index = 1; index <= 1; index++) {
const booleanValue = false;
let stringValue = `Text entry ${index}` // adding the index to the strings.
const serviceData ={
id: index,
name: stringValue,
description: stringValue,
referralInfo: stringValue,
adhcEligible: booleanValue,
assessmentCriteria: stringValue,
targetGender: stringValue,
availability: stringValue,
billingMethod: stringValue,
cost: stringValue,
crisisKeywords: stringValue,
details: stringValue,
eligibilityInfo: stringValue,
ineligibilityInfo: stringValue,
fundingBody: stringValue,
healthcareCardHolders: booleanValue,
intakeInfo: stringValue,
intakePoint: stringValue,
isBulkBilling: booleanValue,
ndisApproved: booleanValue,
promotedService: booleanValue,
specialRequirements: stringValue,
language: stringValue,
ageGroupKeyword: stringValue,
ageGroupDescription: stringValue,
serviceTypes: stringValue,
indigenousClassification: stringValue,
capacityStatus: stringValue,
capacityStatusText: stringValue,
capacityFrequency: stringValue,
capacityLastNotification: stringValue,
capacityLastStatusUpdate: stringValue,
capacityExpireDate: stringValue,
accreditationName: stringValue,
}
stringValue = `Site Text Entry`
const siteData = {
id: index,
name: stringValue,
accessibility: stringValue,
locationDetails: stringValue,
parkingInfo: stringValue,
publicTransportInfo: stringValue,
isMobile: booleanValue,
emailAddress: stringValue,
emailIsConfidential: booleanValue,
website: stringValue,
postalAddress: stringValue,
postalAddressState: stringValue,
postalAddressSuburb: stringValue,
postalAddressPostcode: stringValue,
postalAddressIsConfidential: booleanValue,
phoneNumber: stringValue,
phoneKind: stringValue,
phoneIsConfidential: booleanValue,
openingHours: [{day:"Monday",
openTime:"8:30",
closeTime:"15:30",
openingHoursNote: "closes early"}],
addressBuilding: stringValue,
addressLevel: stringValue,
addressFlatUnit: stringValue,
addressStreetNumber: stringValue,
addressStreetName: stringValue,
addressStreetType: stringValue,
addressStreetSuffix: stringValue,
addressSuburb: stringValue,
addressState: stringValue,
addressPostcode: stringValue,
addressIsConfidential: booleanValue,
servicesInSite: [serviceData,serviceData,serviceData],
}
const organisationData = {
name: stringValue,
description: stringValue,
createdAt: new Date(),
lastUpdated: new Date(),
website: stringValue,
abn: stringValue,
providerType: stringValue,
alsoKnownAs: stringValue,
emailAddress: stringValue,
emailIsConfidential: booleanValue,
postalAddress: stringValue,
postalAddressState: stringValue,
postalAddressSuburb: stringValue,
postalAddressPostcode: stringValue,
postalAddressIsConfidential: booleanValue,
phoneNumber: stringValue,
phoneKind: stringValue,
phoneIsConfidential: booleanValue,
ceo: stringValue,
sitesInOrganisation: [siteData, siteData],
}
Organisation.create(organisationData)
.then(() => {
console.log(`organisation ${index} seeded`);
resolve('done');
});
}
})
}
async function seedUsers(){
return new Promise ((resolve,reject)=>{
Organisation.find()
.then((organisations)=>{
const organisationId = organisations[0]._id
console.log('organisationId',': ', organisationId);
organisations[0].name = "Salvation Army";
organisations[0].save();
const dataUser = {
email: "askizzyplustest1@gmail.com",
firstName: "John",
lastName: "Citizen",
organisation:organisationId,
}
User.create(dataUser)
.then (()=>{
console.log(`user seeded`);
resolve('done');
})
resolve ('done');
})
})
}
async function seedAdminUsers(){
return new Promise ((resolve,reject)=>{
const dataAdminUser = {
email: "askizzyplustest2@gmail.com",
firstName: "Admin",
lastName: "Ask Izzy Plus"
}
AdminUser.create(dataAdminUser)
.then (()=>{
console.log(`admin user seeded`);
resolve('done');
})
resolve ('done');
})
}
async function seed(){
await dropDatabase();
await seedOrganisations();
await seedUsers();
await seedAdminUsers();
console.log("Seeding process finalized.");
}
seed();
// MONGOOSE: Connection event - when disconnected
mongoose.connection.on('error', () => {
console.log('failed to connect to mongod');
});
})