-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
144 lines (118 loc) · 4.5 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
// Include Server Dependencies
var express = require("express");
var bodyParser = require("body-parser");
var logger = require("morgan");
var mongoose = require("mongoose");
var axios = require("axios");
require('dotenv').config()
// todo: include db schemas
var User = require("./models/User");
var user_controller = require("./controllers/controller");
// Create Instance of Express
var app = express();
var PORT = process.env.PORT || 3000; // Sets an initial port
// Run Morgan for Logging
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
app.use(express.static("./public"));
// -------------------------------------------------
mongoose.connect("mongodb://localhost/React");
var db = mongoose.connection;
db.on("error", function(err) {
console.log("Mongoose Error: ", err);
});
db.once("open", function() {
console.log("Mongoose connection successful.");
});
String.prototype.toObjectId = function() {
var ObjectId = (require('mongoose').Types.ObjectId);
return new ObjectId(this.toString());
};
app.post("/api/send_email", function(req, res) {
var helper = require('sendgrid').mail;
from_email = new helper.Email("[email protected]");
to_email = new helper.Email("[email protected]");
subject = `Email from ${req.body.name} ${req.body.email} at immigration portal`;
content = new helper.Content("text/plain", req.body.message);
mail = new helper.Mail(from_email, subject, to_email, content);
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
res.end();
})
})
app.get("/api/user_search/:email?", function(req, res) {
console.log(req.params.email);
User.find({ "email": req.params.email }, function(error, data) {
if (error) {
res.send(error);
} else {
console.log(data);
res.send(data);
}
});
});
app.post("/api/user_create/:email", function(req, res) {
// let user = new User(req.params);
User.update({"email": req.params.email}, {upsert: true}, function(err, doc) {
if (err) {
console.log(err);
} else {
res.send(doc);
}
})
})
app.post("/api/validate_address", function(req, res){
var address_validator_url = "https://us-street.api.smartystreets.com/street-address?auth-id=" + process.env.SMARTYSTREETS_AUTH_ID + "&auth-token=" + process.env.SMARTYSTREETS_AUTH_TOKEN + "&street=" + req.body.street_number + "%20" + req.body.street_name + "&city=" + req.body.city + "&state=" + req.body.state + "&zipcode=" + req.body.zipcode;
console.log(address_validator_url);
axios.get(address_validator_url)
.then(function(response) {
console.log(response.data);
res.send(response.data);
})
.catch(function(error) {
console.log(error);
});
})
app.post("/api/post_user", function(req, res) {
var query = { '_id': req.body.db_id.toObjectId() };
console.log(req.body);
User.findOneAndUpdate(query, { $set: {email: req.body.email, first_name: req.body.first_name, last_name: req.body.last_name, street_number: req.body.street_number, street_name: req.body.street_name, city: req.body.city, state: req.body.state, zipcode: req.body.zipcode, date_of_birth: req.body.date_of_birth, survey_step: req.body.survey_step } }, { new: true },
function(error, data) {
console.log(data);
if (error) {
res.send(error);
} else {
console.log(data);
res.send(data);
}
});
})
app.post("/api/update_survey_step/:email?", function(req, res){
console.log(req.params.email);
User.findOneAndUpdate({ "email": req.params.email }, { $set: { survey_step: req.body.survey_step} }, { new: true },function(error, data) {
if (error) {
res.send(error);
} else {
console.log(data);
res.send(data);
}
});
})
app.get("*", function(req, res) {
res.sendFile(__dirname + "/public/index.html");
});
// -------------------------------------------------
app.listen(PORT, function() {
console.log("App listening on PORT: " + PORT);
});