-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.js
256 lines (217 loc) · 7.47 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
// SETUP
// ================================================
var express = require('express')
, bodyParser = require('body-parser')
, mongoose = require('mongoose')
, path = require('path')
, request = require('request')
, geocoder = require('node-geocoder')('google','https', {apiKey:'AIzaSyBtoo72MlIo8Dwq0QPIAmAXIZWyrAAr9CQ'})
, request = require('request')
, createEmail = require('./assets/js/example-email.js')
// , mandrill = require('node-mandrill')('7ZUL0_LO1EUZVpEF0FbzGw')
// create server
var app = express()
// configure app to use bodyParser()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// where to serve static content
app.use( express.static('assets') )
// set our port
var port = process.env.PORT || 5000
// DATABASE
// ================================================
// connect to database
mongoose.connect('mongodb://jmcelroy:[email protected]:61731/sf-in-progress')
// SCHEMAS
var projectHearingSchema = require('./models/project_hearing_schema.js')
var ProjectHearing = mongoose.model('ProjectHearing', projectHearingSchema)
var projectSchema = require('./models/project_schema.js')
var Project = mongoose.model('Project', projectSchema)
// ROUTES
// ================================================
// landing page
app.get('/', function (req,resp){
resp.sendFile(path.join(__dirname, '/assets', '/index.html'))
})
// interactive map page
app.get('/map', function (req,resp){
resp.sendFile(path.join(__dirname, '/assets', '/map.html'))
})
// form for admins to add new projects
app.get('/projects/new', function (req, resp) {
resp.sendFile(path.join(__dirname, '/assets', '/new-project-form.html'))
})
// form for everyone to add new hearings
app.get('/hearings/new/:project_id', function (req, resp) {
resp.sendFile(path.join(__dirname, '/assets', '/new-hearing-form.html'))
})
// form for admins to update a project
app.get('/admin/projects/:project_id', function (req, resp) {
resp.sendFile(path.join(__dirname, '/assets', '/admin-form.html'))
})
// return a list of all the projects
app.get('/projects', function (req, resp){
return Project.find(function (err, projects){
if (err){
resp.send(err)
}
resp.json(projects)
})
})
// save a new project
app.post('/projects', function (req, resp) {
var saveProject = function(coords) {
var project = new Project({
name: req.body.name || ''
, address: req.body.address || ''
, city: req.body.city
, neighborhood: req.body.neighborhood || ''
, description: req.body.description || ''
, zoning: req.body.zoning || ''
, units: req.body.units || null
, status: req.body.status || ''
, website: req.body.website || ''
, picture: req.body.picture || ''
, statusCategory: determineStatusCategory(req.body.status) || ''
, coordinates: [(coords.latitude).toString(), (coords.longitude).toString()] || []
, featured: req.body.featured || false
, sponsorFirm: req.body.sponsorFirm || ''
, hearings: []
})
project.save(function(err){
if(err){
resp.send(err)
}
resp.json({message: project.address + ' saved'})
})
}
var address = req.body.address + ' ' + req.body.city
geocode(address, function(latitude,longitude) {
saveProject({'latitude': (latitude).toString(), 'longitude': (longitude).toString()})
}.bind(this))
})
app.get('/projects/featured', function (req, resp) {
return Project.find({featured: true}, function (err, projects) {
if (err) resp.send(err)
resp.json(projects)
})
})
// get a particular project
app.get('/projects/:project_id', function (req, resp){
var id = mongoose.Types.ObjectId(req.params.project_id)
return Project.findById(id, function (err, project){
if(err) resp.send(err)
resp.json(project)
})
})
// update / replace a particular project
app.post('/projects/:project_id', function (req, resp) {
var id = mongoose.Types.ObjectId(req.params.project_id)
var newDoc = req.body
newDoc.featured = req.body.featured == 'true'
var options = null
Project.findOneAndUpdate({_id: id}, newDoc, options, function (err, project) {
project.save()
resp.json({message: "project updated", project: project})
})
})
app.delete('/projects/:project_id', function (req, resp) {
var id = mongoose.Types.ObjectId(req.params.project_id)
Project.findOne({_id: id}, function (err, project) {
project.remove()
resp.sendStatus(200)
})
})
// email subscribe to a project
app.post('/subscribe/:project_id', function (req, resp) {
var projectId = mongoose.Types.ObjectId(req.params.project_id)
, email = req.body.email
, options = {}
Project.findByIdAndUpdate(
projectId
, { '$push' : {emails: email} }
, {}
, function (err, project) {
var message = createEmail(email, project.address, null)
// send email via mandrill
request.post('https://mandrillapp.com/api/1.0/messages/send.json', {form: message.confirmation}, function (err, resp) {
if (err) console.log('Mandrill error: ', err)
else {
console.log('Mandrill resp', resp.body)
}
})
resp.json({message: req.body.email + ' subscribed to project ' + projectId, project: project})
}
)
})
// project hearing form submission
app.post('/hearings/:project_id', function (req, resp) {
var projectId = mongoose.Types.ObjectId(req.params.project_id)
var hearing = new ProjectHearing({
projectId: projectId
, location: req.body.location
// TODO: make sure this is getting saved as timestamp. The result will be
// NaN if the date is not an ISO string format or numeric value.
, date: req.body.date
, packetUrl: req.body.documents
, documents: req.body.documents // any documents in addition to the pdf url
, type: req.body.hearing_type // continuance, consent, regular, review
, description: req.body.description
, staffContact: {
name: req.body.staffContactName
, phone: req.body.staffContactPhone
}
, preliminaryRecommendation: req.body.preliminaryRecommendation
// This is the outcome of the meeting. This is published in the minutes
// about 1 month after the meeting.
// , action: req.body.action
})
var options = {};
Project.findByIdAndUpdate(
projectId
, { $push : {hearings: hearing} }
, options
, function (err, project) {
// send email to subscribers to this project
var subscribers = project.emails
console.log('SUBSCRIBERS ', subscribers)
// email is null for now (defaults to [email protected])
var message = createEmail(null, project.address)
request.post('https://mandrillapp.com/api/1.0/messages/send.json', {form: message.announcement}, function (err, resp) {
if (err) console.log('Mandrill error: ', err)
else {
console.log('Mandrill resp', resp.body)
}
})
resp.sendStatus(201)
})
})
app.all('*', function(req, res){
res.sendStatus(404);
})
// START THE SERVER
// ================================================
app.listen(port)
console.log('SF in Progress is running. \n Open your browser and navigate to localhost:' + port + '/map')
// UTILITY FUNCTIONS
// ================================================
var determineStatusCategory = function determineStatusCategory(status) {
var statusCategory
if (status === "Construction") {
statusCategory = "construction"
}
else if (status.substring(0,2) === "PL" || status.substring(0,2) === "Pl") {
statusCategory = "planning"
}
else if (status.substring(0,2) === "BP") {
statusCategory = "building"
}
return statusCategory
}
var geocode = function geocode(address, callback) {
geocoder.geocode(address, function(err, resp) {
if (!err && resp.length) {
callback(resp[0].longitude, resp[0].latitude)
}
})
}