-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
235 lines (195 loc) · 8.34 KB
/
app.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
var express = require('express');
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var app = express();
const MongoClient = require('mongodb').MongoClient;
var mongo = require('mongodb');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var env = require(__dirname + '/env-vars.js');
var gmail_login = env.gmail_login;
var gmail_pass = env.gmail_pass;
var db;
var db2;
// var router = express.Router();
app.use(express.json()); //convert req to json
app.use(express.static(__dirname + '/app'));
app.use(session({secret: "Sam is awesome"}));
var allPages = ['/home','/what-we-do','/organization','/faces-of-our-members','/faq','/news','/contact','/become-member','/member-app','/volunteer-to-drive','/volunteer-app','/family-involvement','/member-programs','/pay-online','/donate','/corporate', '/non-rider-member','/dashboard','/login', '/view-form','/draft','/help-on-wheels'];
MongoClient.connect('mongodb://itnadmin:itnUser0136!@ds153700.mlab.com:53700/itnbluegrass', function(err, client) {
if (err) {
console.log('db not connecting, but inside mongo block', err);
};
db = client.db('itnbluegrass');
app.get('/getMemberApps', function (req,res) {
db.collection('memberapp').find().toArray(function (err, result) {
console.log('result is ', result);
res.send(result);
})
}); // end of /getMemberForms get request
app.get('/getVolunteerApps', function (req,res) {
db.collection('volunteerapp').find().toArray(function (err, result) {
console.log('result is ', result);
res.send(result);
})
}); // end of /getVolunteerForms get request
app.get('/getNonRiderApps', function (req,res) {
db.collection('nonriderapp').find().toArray(function (err, result) {
console.log('result is ', result);
res.send(result);
})
}); // end of /getNonRiderForms get request
app.get('/getContactForms', function (req,res) {
db.collection('contactform').find().toArray(function (err, result) {
console.log('result is ', result);
res.send(result);
})
}); // end of /getContactForms get request
app.get('/getNewsletterForms', function (req,res) {
db.collection('newsletterform').find().toArray(function (err, result) {
console.log('result is ', result);
res.send(result);
})
}); // end of /getNewsletterForms get request
app.get('/getAdmin', function (req,res) {
db.collection('users').find().toArray(function (err, result) {
var userInput = JSON.parse(req.query.formData);
if ((result[0].username === userInput.username) && (result[0].password === userInput.password)){
console.log('a match, initializing session');
req.session.user = userInput;
console.log('new session is ', req.session.user, 'and', req.session);
res.send(result);
}
else {
res.status(500).send('error')
}
})
}); // end of /getAdmin get request
app.delete('/deleteForm/:formId', function (req,res) {
console.log('req param', req.params.formId, 'req query', req.query.formType);
var tableName = req.query.formType;
var recordId = req.params.formId;
db.collection(tableName).deleteOne({_id: new mongo.ObjectId(recordId)}, function(err, result){
console.log('record has been removed, i think');
res.send(result);
});
}); // end of delete request
});//end of mongoclient
app.get('/getAllRides', function (req,res) {
MongoClient.connect('mongodb://itnadmin:itnUser0136!@ds119442.mlab.com:19442/itnamerica-new', function(err, client) {
if (err) {
console.log('db itnamerica not connecting, but inside mongo block:', err);
};
db2 = client.db('itnamerica-new');
db2.collection('ridesdatamonthly').find().toArray(function (err, result) {
console.log('result is ', result);
res.send(result);
})
});
}); // end of /getRidesData get request
app.post('/sendmail', function(req, res){
console.log('post req', req.body);
let transporter = nodemailer.createTransport(smtpTransport({
service: "Gmail", // sets automatically host, port and connection security settings
auth: {
user: gmail_login,
pass: gmail_pass
}
})
)
let mailOptions = {};
if (req.body && req.body.pdf){
console.log('sending email with pdf, membership, volunteer or non-rider forms');
mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
subject: req.body.subject, // Subject line
// text: JSON.stringify(req.body.text), // plain text body
attachments: [{path: req.body.pdf}],
bcc: 'info@itnbluegrass.org'
};
}
else if (req.body && req.body.html){
console.log('sending email without pdf, contact form');
mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
subject: req.body.subject, // Subject line
text: JSON.stringify(req.body.text), // plain text body
html: req.body.html, // html body
bcc: 'info@itnbluegrass.org'
};
} else {
console.log('sending email with neither');
mailOptions = {
from: req.body.from, // sender address
to: req.body.to, // list of receivers
subject: req.body.subject, // Subject line
text: JSON.stringify(req.body.text), // plain text body
bcc: 'info@itnbluegrass.org'
};
}
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
transporter.close();
});
MongoClient.connect('mongodb://itnadmin:itnUser0136!@ds153700.mlab.com:53700/itnbluegrass', function(err, client) {
if (err) {
console.log('db not connecting, but inside mongo block', err);
};
db = client.db('itnbluegrass');
var objWithPDF; var pdfVal;
if ((req.body && req.body.pdf) && (req.body.formType === 'membership')) {
objWithPDF = req.body.text;
objWithPDF.pdf = req.body.pdf;
db.collection('memberapp').save(objWithPDF, function(err, result){
if (err) { return console.log('connecting to db, but not saving obj', err); }
console.log('member app saved to database', result);
// res.redirect('/');
})
}
else if ((req.body && req.body.pdf) && (req.body.formType === 'volunteer')) {
objWithPDF = req.body.text;
objWithPDF.pdf = req.body.pdf;
db.collection('volunteerapp').save(objWithPDF, function(err, result){
if (err) { return console.log('connecting to db, but not saving obj', err);}
console.log('volunteer app saved to database', result);
// res.redirect('/');
})
}
else if ((req.body && req.body.pdf) && (req.body.formType === 'nonrider')) {
objWithPDF = req.body.text;
objWithPDF.pdf = req.body.pdf;
db.collection('nonriderapp').save(objWithPDF, function(err, result){
if (err) { return console.log('connecting to db, but not saving obj', err);}
console.log('nonrider app saved to database', result);
// res.redirect('/');
})
}
else if ((req.body && req.body.html) && (req.body.formType === 'contact')) {
db.collection('contactform').save(req.body.text, function(err, result){
if (err) { return console.log('connecting to db, but not saving obj', err);}
console.log('contact form saved to database', result);
// res.redirect('/');
})
}
else if (req.body.text.email && (req.body.formType === 'newsletter')) {
console.log('inside newsletter backend block',req.body.text);
db.collection('newsletterform').save(req.body.text, function(err, result){
if (err) { return console.log('connecting to db, but not saving obj', err);}
console.log('newsletter form saved to database', result);
// res.redirect('/');
})
}
});//end of mongoclient
console.log('after mongo block');
res.end();
}); // end /sendmail post request
app.use(allPages, function(req, res){
res.sendFile(__dirname + '/app/index.html');
});
app.listen(process.env.PORT || 13270);