-
Notifications
You must be signed in to change notification settings - Fork 35
/
install.js
62 lines (57 loc) · 1.74 KB
/
install.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
// Performs some basic setup tasks. Should be run as part of deploy process.
var database = require('./lib/database');
var Report = require('./models/report');
var User = require('./models/user');
var config = require('./config/secrets').get();
var tasks = [];
// Enable full-text indexing for Reports
function enableIndexing(callback) {
// Wait for database connection
database.mongoose.connection.on('error', function(err) {
console.error('mongoose connection error (retrying): ', err);
setTimeout(function() {
database.mongoose.connect(database.connectURL,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
}, 200);
});
database.mongoose.connection.once('open', function() {
Report.ensureIndexes(function(err) {
if (err) console.error(err);
else console.log('Indexing is enabled for Reports.');
callback();
});
});
}
tasks.push(enableIndexing);
// Verify that an admin user exists
function createAdminUser(callback) {
User.findOne({ role: 'admin' }, function(err, user) {
if (!user) {
var userData = {
provider: 'aggie',
email: config.adminEmail,
username: 'admin',
password: config.adminPassword,
role: 'admin',
hasDefaultPassword: true
};
// Create new admin user
User.create(userData, function(err, user) {
if (err) console.error(err);
else console.log('"admin" user created with password "' + config.adminPassword + '"');
callback();
});
} else callback();
});
}
tasks.push(createAdminUser);
var remaining = tasks.length;
tasks.forEach(function(task) {
task(function() {
if (--remaining === 0) process.exit();
});
});