forked from quiquemz/food-aid
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
268 lines (228 loc) · 7.59 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* Module dependencies. */
var bodyParser = require('body-parser');
var express = require('express');
var cookieParser = require('cookie-parser');
var handlebars = require('express3-handlebars');
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var path = require('path');
var _ = require('lodash');
// Route Functions
var index = require('./routes/index');
var register = require('./routes/register');
var settings = require('./routes/settings');
var support = require('./routes/support');
var paymentinfo = require('./routes/paymentinfo');
//Customer Routes Functions
var customerHome = require('./routes/customer/home');
var deal = require('./routes/customer/deal');
var orderSummary = require('./routes/customer/order-summary');
var map = require('./routes/customer/map');
//Retailer Routes Functions
var retailerHome = require('./routes/retailer/home');
var retailerOptions = require('./routes/retailer/options');
var retailerInfo = require('./routes/retailer/info');
var retailerVerification = require('./routes/retailer/verification');
var retailerConfirmation = require('./routes/retailer/confirmation');
//JSON OBJECTS
var users = require('./users.json');
var offers = require('./data.json');
// express variable
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', handlebars());
app.set('view engine', 'handlebars');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//Re-routing to login st
app.get('/', function (req, res) {
res.redirect('/login');
});
// Add routes here
app.get('/login', index.view);
app.post('/login', login);
app.get('/register', register.view);
app.post('/register', registerNewUser);
app.get('/settings', settings.view);
app.get('/support', support.view);
app.get('/paymentinfo', paymentinfo.view);
// Routes to see current state of database
app.get('/users', function(req, res) {
res.send(users);
});
app.get('/offers', function(req, res) {
res.send(offers);
});
// Add customer routes
app.get('/customer/home', function(req, res){
res.render('customer/home', offers);
});
app.get('/customer/home2', function(req,res){
//data['showAlternate'] = true;
res.render('customer/home2', offers);
});
app.get('/customer/deal', deal.view);
app.get('/customer/order-summary', orderSummary.view);
app.get('/customer/map', map.view);
//Add retailer routes
app.get('/retailer/home', retailerHome.view);
app.post('/retailer/home', createNewOffer);
app.get('/retailer/options',retailerOptions.view);
app.post('/retailer/options', addOptionsToOffer);
app.get('/retailer/verification',retailerVerification.view);
app.get('/retailer/confirmation', retailerConfirmation.view);
app.get('/retailer/info', retailerInfo.view);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
//Helper functions
function login(req, res) {
var email = req.body.email;
var passw = req.body.password;
var user = _.find(users.usersList, {'email': email, 'password': passw});
if(user && user.type === 'Customer') {
res.cookie('username', user.username);
res.redirect('/customer/home');
}
else if(user && user.type === 'Retailer') {
res.cookie('username', user.username);
res.redirect('/retailer/home');
} else if(user)
//alert('ERROR. User is neither Customer nor Retailer!!!');
console.log('ERROR. User is neither Customer nor Retailer');
else
res.redirect('/login');
}
function registerNewUser(req, res) {
if(checkUniqueness(req.body)) {
console.log('Username or email already exists. Try ' + req.body.username + '8');
return;
}
if(!checkNewUserRequest(req)) {
//console.log(req.body);
console.log('Please fill all required fields');
return;
}
users.usersList.push(
{
"firstName": req.body.firstName,
"lastName": req.body.lastName,
"username": req.body.username,
"type": req.body.radio,
"email": req.body.email,
"password": req.body.password,
"id": (users.usersList.length + 1)
}
);
login(req, res);
}
function checkUniqueness(body) {
return _.find(users.usersList, {'username': body.username}) ||
_.find(users.usersList, {'email': body.email})
}
function checkNewUserRequest(req){
if(req.body.radio && req.body.firstName &&
req.body.lastName && req.body.username &&
req.body.email && req.body.password &&
req.body.confirmPassword && req.body.checkbox === 'on') {
if(req.body.password === req.body.confirmPassword)
return true;
}
return false;
}
function createNewOffer(req, res) {
if(!checkNewOfferRequest(req)) {
console.log('Please fill all required fields');
return;
}
var feature;
if(req.body.radio == 'YES') {
var offer = _.find(offers.offers, {'feature': true});
offer.feature = false;
console.log(offer);
feature = true;
}
offers.offers.push({
"owner": req.body.owner,
"name": req.body.name,
"id": offers.offers.length + 1,
"wait-time": "35-50 min",
"offer": req.body.offer,
"price": parseInt(req.body.price),
"price-per-unit": "$" + req.body.price + ".00/" + req.body.measure,
"quantity": parseInt(req.body.quantity),
"img": "http://lorempixel.com/400/200/food",
"local-img": "http://lorempixel.com/400/200/food",
"feature": feature
});
res.cookie('offerId', offers.offers.length);
res.redirect('/retailer/options');
}
function addOptionsToOffer(req, res) {
var id = parseInt(req.cookies.offerId);
var offer = _.find(offers.offers, {'id': id});
if(offer) {
if(req.body.radCharity) {
offer.donationOrg = req.body.radCharity;
} else {
offer.donationOrg = null;
}
if(req.body.radProfit) {
offer.profitOrg = req.body.radProfit;
} else {
offer.profitOrg = null;
}
}
if(offer.profitOrg)
res.render('retailer/confirmation', offer);
}
function checkNewOfferRequest(req){
if(req.body.owner && req.body.name &&
req.body.offer && req.body.price &&
req.body.measure && req.body.quantity &&
req.body.radio) {
return true;
}
return false;
}
/*var url = 'mongodb://localhost:27017/myproject';
var insertDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Insert some documents
collection.insertMany([
{a : 1}, {a : 2}, {a : 3}
], function(err, result) {
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the collection");
callback(result);
});
};
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
};
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
insertDocuments(db, function() {
findDocuments(db, function () {
db.close();
});
});
});*/