diff --git a/server.js b/server.js index 55801cd..918ea7b 100644 --- a/server.js +++ b/server.js @@ -118,18 +118,11 @@ app.get('/', function(req, res){ }); }); -app.post(':loc/admin', isAuthenticated, function(req, res){ - winston.info('POST by admin at ' + loc); - // possible to forward req to POST to '/'? - var loc = req.params.loc; -}); - app.post('/', function(req, res){ var postEvent = req.body.postEvent; var source = req.body.source; winston.info(req.body); - // If it's stripped down JSON sent by cell modem, there won't be a req.body.data key w/full-nested JSON as value!!! - // ???how to fix??? + // If it's stripped down JSON sent by cell modem, there won't be a req.body.data key w/full-nested JSON as value. // If post body includes coreid and published_at, assume it's from a particle devcie and parse accordingly. if (req.body.coreid !== undefined && req.body.published_at !== undefined) { var innerDataJSON = JSON.parse(req.body.data); @@ -144,6 +137,20 @@ app.post('/', function(req, res){ ); done(); }); + } else if (req.body.loc !== undefined) { // Assume POST from /admin template + var coreid = '2a002b000947343432313031' // Hard-code TT coreid for now, otherwise need to look up coreid from location value. + var received_at = new Date(Date.now()); + received_at = received_at.toISOString(); + var status = req.body.status; + winston.info(coreid + '; ' + received_at + '; ' + status + '; ' + JSON.stringify(req.body, null, 4)); + pg.connect(conString, function(err, client, done) { + if(err) { return winston.error('error fetching client from pool', err); } + client.query( + "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);", + [JSON.stringify(req.body, null, 4), coreid, received_at, status] + ); + done(); + }); } else { // parse for minimal data from cell modem. var deviceid = JSON.stringify(req.body.data, null, 4).slice(1,4); var statusCode = JSON.stringify(req.body.data, null, 4).slice(5,7); @@ -197,14 +204,17 @@ app.get('/:loc/admin', isAuthenticated, function(req, res) { } // Loop over elements in rows array, convert ugly UTC times to pretty local times. result.rows.forEach(function(row){ + row.loc = loc; row.pubdate = courtsopenUtils.getLocDateFromUTC(row.published_at); row.pubtime = courtsopenUtils.getLocTimeFromUTC(row.published_at); if(row.status.toLowerCase().indexOf('closed') > -1){ row.statusclass = 'closed'; row.oppstatus = 'open'; + row.prettystatus = 'Open'; } else { row.statusclass = 'open'; row.oppstatus = 'closed'; + row.prettystatus = 'Closed'; } }); res.render('admin', {values: result.rows}, function(err, html) { diff --git a/views/admin.hbs b/views/admin.hbs index ef0d2d5..727c784 100644 --- a/views/admin.hbs +++ b/views/admin.hbs @@ -19,8 +19,9 @@