From c093b27e101f19e7667124479aa5c4baaf33adad Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Mon, 25 Mar 2019 11:10:03 -0400 Subject: added data validation before inserting into db --- web/package.json | 7 ++++--- web/routes/index.js | 30 +++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'web') diff --git a/web/package.json b/web/package.json index a669ecb..8a59f6e 100644 --- a/web/package.json +++ b/web/package.json @@ -9,10 +9,11 @@ "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", + "geojson-geometries-lookup": "^0.3.0", "http-errors": "~1.6.2", - "morgan": "~1.9.0", - "pug": "2.0.0-beta11", "mongodb": "*", - "mongoose": "*" + "mongoose": "*", + "morgan": "~1.9.0", + "pug": "2.0.0-beta11" } } diff --git a/web/routes/index.js b/web/routes/index.js index 23fcf36..3c066a9 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 @@ -23,24 +29,34 @@ 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(200).send({ error: errMssg }); + } + + if (!glookup.hasContainers(point)) { + var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto"; + return res.status(200).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.get('/stream', function(req, res){ // set up server side event (communication line between front end and server) res.sseSetup(); -- cgit v1.2.3 From e8f1fd60b765981bdd230789eebc9279db477b70 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Mon, 25 Mar 2019 11:19:42 -0400 Subject: fixed typo --- web/routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'web') diff --git a/web/routes/index.js b/web/routes/index.js index 3c066a9..4bbd7f6 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -37,7 +37,7 @@ router.post('/mobilerequest', function(req, res) { } if (!glookup.hasContainers(point)) { - var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto"; + var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto."; return res.status(200).send({ error: errMssg }); } -- cgit v1.2.3 From 7e68fc136076f2385030faf58f43f6764c0f69d7 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Tue, 26 Mar 2019 16:17:46 -0400 Subject: changing error code to 422 for one of the validation conditions --- web/routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'web') diff --git a/web/routes/index.js b/web/routes/index.js index 4bbd7f6..0c72e43 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -33,7 +33,7 @@ router.post('/mobilerequest', function(req, res) { if (!point.coordinates.length) { var errMssg = "Coordinates field must not be empty in a request."; - return res.status(200).send({ error: errMssg }); + return res.status(422).send({ error: errMssg }); } if (!glookup.hasContainers(point)) { -- cgit v1.2.3 From 2ec60276238f0b5d402ec2a5d02321e2e4c99303 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Tue, 26 Mar 2019 16:19:40 -0400 Subject: for both validations --- web/routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'web') diff --git a/web/routes/index.js b/web/routes/index.js index 0c72e43..0b32d0e 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -38,7 +38,7 @@ router.post('/mobilerequest', function(req, res) { if (!glookup.hasContainers(point)) { var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto."; - return res.status(200).send({ error: errMssg }); + return res.status(422).send({ error: errMssg }); } // here we are guaranteed to have a valid point in Toronto -- cgit v1.2.3