-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
171 lines (147 loc) · 4.99 KB
/
server.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
////////////// Modules ////////////////////////
require('dotenv').config()
const moment = require('moment');
moment().format();
const cron = require('node-cron');
const express = require('express');
const path = require('path');
const db = require('./models');
const apiRoutes = require('./routes/apiRoutes');
const expressSession = require('express-session');
const SessionStore = require('express-session-sequelize')(expressSession.Store)
const cookieParser = require('cookie-parser');
const passport = require('./passport')
const appSettings = require('./appSettings');
// const enforce = require('express-sslify');
const invoiceJob = require('./cron/invoiceGenerator');
// update the config folder with your un and pw. Make sure the DB is created first before server is running
global.db = db;
////////////// Configuration //////////////////
const PORT = process.env.PORT || 3001;
const app = express();
//Enforce HTTPS/SSL
if (process.env.NODE_ENV == 'production') {
app.use(require('express-sslify').HTTPS({ trustProtoHeader: true }));
}
app.use(express.urlencoded());
app.use(express.json());
app.use(cookieParser());
// Don't clear database in production. Seems important.
var syncOption = (process.env.NODE_ENV == 'production') ? {} : { force: true };
db.sequelize.sync(
syncOption
).then(() => {
// Generate default user(s) and settings, when applicable
return Promise.all([
generateDatabaseSeed(),
appSettings.init(),
]);
}).then(() => {
invoiceJob.schedule();
const sequelizeSessionStore = new SessionStore({
db: db.sequelize,
});
// app.use(cookieParser());
app.use(expressSession({
secret: 'a whop bop baloobop.',
store: sequelizeSessionStore,
resave: false,
saveUninitialized: false,
}));
//``
app.use(passport.initialize())
app.use(passport.session()) // will call the deserializeUser
app.use(apiRoutes)
}).then(() => {
////////////// Routing ////////////////////////
app.use('/auth', require('./routes/auth'));
app.use('/static', express.static(path.join(__dirname, 'client', 'build', 'static')));
app.use('/img', express.static(path.join(__dirname, 'client', 'build', 'img')));
app.get('*', (req, res) => {
var indexPath = path.join(__dirname, 'client', 'build', 'index.html');
res.sendfile(indexPath);
});
app.listen(PORT, () => {
console.log('Listening on port ' + PORT);
});
}).catch(err => {
console.log("There was an error connecting to the database or performing follow-up logic");
console.error(err);
});
/** Seeds the database when applicable. Returns a promise that resolves when the operation is complete. */
function generateDatabaseSeed() {
if (process.env.NODE_ENV == 'production') {
return db.User.count()
.then(count => {
if (count > 0) return Promise.resolve();
return db.User.create({
fullname: "Administrator",
role: "admin",
activationCode: "admin",
authtype: null,
local_username: null,
local_password: null,
googleId: null,
phone: "000-000-0000",
email: "none@none.com",
address: "none",
city: "none",
state: "CA",
zip: 90210,
});
})
}
var newUnitPromise = db.Unit.create({
unitName: "Big Office",
rate: 90
});
var newAdminPromise = db.User.create({
fullname: "admin j. user",
role: "admin",
activationCode: "admin",
authtype: null,
local_username: null,
local_password: null,
googleId: null,
phone: "000-000-0000",
email: "fake@web.com",
address: "none",
city: "none",
state: "CA",
zip: 90210,
});
var newTenantPromise = db.User.create({
fullname: "Freddy McTenant",
role: "tenant",
activationCode: "tenant",
authtype: null,
local_username: null,
local_password: null,
googleId: null,
phone: "000-000-0000",
email: "fake@mail.com",
address: "none",
city: "none",
state: "CA",
zip: 90210,
});
var newPaymentPromise = db.Payment.create({
amount: 450,
paid: false,
due_date: '2018-04-17 00:58:52',
UnitId: 1
});
var newPaymentPromise2 = db.Payment.create({
amount: 500,
paid: false,
due_date: '2018-04-17 00:58:52',
UnitId: 1
});
return Promise
.all([newUnitPromise, newAdminPromise, newTenantPromise, newPaymentPromise])
.then(([newUnit, newAdmin, newTenant, newPayment]) => {
newUnit.addUsers([newAdmin, newTenant]);
// newAdmin.addUnit(newUnit).then(()=>
// newTenant.addUnit(newUnit))
});
}