aboutsummaryrefslogtreecommitdiff
path: root/web/routes
diff options
context:
space:
mode:
authorIvan Shen <iwshen11@gmail.com>2019-03-28 19:38:23 +0000
committerGitHub <noreply@github.com>2019-03-28 19:38:23 +0000
commit143891d8467bf80209255fd55da1739625583858 (patch)
tree568e5fa7c38757ec64943aa4f37143728cba6eb7 /web/routes
parenta4de620727d4e7d294ced1d1e1c956c0be8c0cd2 (diff)
parent3dd064805b0c4930886a99dccc9d4d980e8c15e0 (diff)
Merge branch 'qa' into feat/28
Diffstat (limited to 'web/routes')
-rw-r--r--web/routes/index.js57
1 files changed, 48 insertions, 9 deletions
diff --git a/web/routes/index.js b/web/routes/index.js
index 23fcf36..2633c04 100644
--- a/web/routes/index.js
+++ b/web/routes/index.js
@@ -2,7 +2,13 @@ var express = require('express');
var router = express.Router();
var Mockpoints = require('../models/mockpoints');
var Points = require('../models/points');
+var fs = require("fs");
+var GeoJsonGeometriesLookup = require('geojson-geometries-lookup');
+var torontoNeighbourhoords = JSON.parse(fs.readFileSync(__dirname + '/../data/Neighbourhoods.geojson', "utf8"));
+
const connections = [];
+const glookup = new GeoJsonGeometriesLookup(torontoNeighbourhoords);
+
/* GET home page. */
router.get('/', function(req, res, next) {
// for now just redirect to /map
@@ -11,9 +17,7 @@ router.get('/', function(req, res, next) {
/* GET Map page. */
router.get('/map', function(req,res) {
- // load the map with all the points
- // Mockpoints.get_points(function(err, points){
- Points.get_points(function(err, points){
+ Points.get_all_active_points(function(err, points){
res.render('map', {
lat : 43.665234,
lng : -79.383370,
@@ -23,23 +27,58 @@ router.get('/map', function(req,res) {
});
router.post('/mobilerequest', function(req, res) {
- var data = req.body;
- Points.save_request(data, function(err, result) {
+ var point = req.body;
+
+ if (!point.coordinates.length) {
+ var errMssg = "Coordinates field must not be empty in a request.";
+ return res.status(422).send({ error: errMssg });
+ }
+
+ if (!glookup.hasContainers(point)) {
+ var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto.";
+ return res.status(422).send({ error: errMssg });
+ }
+
+ // here we are guaranteed to have a valid point in Toronto
+ Points.save_request(point, function(err, result) {
if (err) {
console.log(`err inserting mobile request into db: ${err}`);
- res.status(500).send({ error: "boo:(" });
+ res.status(500).send({ error: "Sorry, something failed on our end." });
} else {
// send event to all connections
for(var i = 0; i < connections.length; i++) {
- connections[i].sseSend(data);
+ connections[i].sseSend(point);
}
console.log(result);
- res.status(200).send({'status': 'success', 'data': data});
+ res.status(201).send({'status': 'success'});
}
});
-
});
+router.post('/savestatus/:status', function(req, res) {
+ var data = req.body;
+ var pointId = data.id;
+ var setStatus = req.params.status;
+ if (
+ setStatus !== "new" &&
+ setStatus !== "pending" &&
+ setStatus !== "complete"
+ ) {
+ console.log("Invalid status supplied.");
+ return res.status(422).send({error: "Error: Status must be one of 'new', 'pending', or 'complete'"});
+ }
+
+ // here we are guaranteed to be able to do the query
+ Points.update_point_status(pointId, setStatus, function(err, result) {
+ if (err) {
+ console.log(`err updating point status in db: ${err}`);
+ res.status(500).send({ error: "Oops. Something went wrong on our end." });
+ } else {
+ console.log(result);
+ res.status(200).send({'status': 'success'});
+ }
+ });
+});
router.get('/stream', function(req, res){
// set up server side event (communication line between front end and server)