diff --git a/alertmonUtils.js b/alertmonUtils.js index 0230820..2da140e 100644 --- a/alertmonUtils.js +++ b/alertmonUtils.js @@ -12,14 +12,14 @@ exports.getLocDateFromUTC = function(utcDtTm) { return localDt; }; -exports.getCoreNameFromCoreId = function(db, coreId) { - db.all('SELECT coreName FROM Cores WHERE coreId = ? LIMIT 1;', coreId, function(err, rows){ - if (err !== null) { - console.log(err); - } else { - return rows.coreName; - } - } - ); - -}; +// This returned undefined, it probably won't work w/out some fancy dependency injection stuff. +//exports.getCoreNameFromCoreId = function(db, coreId) { + //db.all('SELECT coreName FROM Cores WHERE coreId = ? LIMIT 1;', coreId, function(err, rows){ + //if (err !== null) { + //console.log(err); + //} else { + //return rows.coreName; + //} + //} + //); +//}; diff --git a/server.js b/server.js index baff247..bb10574 100644 --- a/server.js +++ b/server.js @@ -20,7 +20,7 @@ var mailOptions = { from: 'Alert Monitor ', to: 'jody@kaplon.us,don@gettner.com', subject: 'Alert received', - text: 'test alert' // Get custom text in here with device info. + text: 'test alert' // Get custom text later on email generation. }; @@ -98,17 +98,17 @@ app.get('/core/edit/:id', function(req, res){ console.log("GET /core/edit/" + coreId + ", " + JSON.stringify(d, 4)); db.all("select coreName, locationDesc from Cores where coreId = ?;", coreId, function(err, rows){ - if(err !== null) { - console.log(err); - } else { - res.render('core-edit', {values: rows}, function(err, html) { - if(err !== null) { - console.log(err); - } else { - res.send(html); - } - }); - } + if(err !== null) { + console.log(err); + } else { + res.render('core-edit', {values: rows}, function(err, html) { + if(err !== null) { + console.log(err); + } else { + res.send(html); + } + }); + } }); }); @@ -178,6 +178,9 @@ app.get('/core/:id', function(req, res){ rows.forEach(function(row){ row.pubDate = alertmonUtils.getLocDateFromUTC(row.published_at); row.pubTime = alertmonUtils.getLocTimeFromUTC(row.published_at); + if(row.status.toLowerCase().indexOf('alert') > -1){ + row.isAlert = 1; + } }); res.render('core', {alerts: rows}, function(err, html) { @@ -197,7 +200,7 @@ app.post('/', function(req, res){ var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1); var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1); var stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)"); - stmt.run( // Use .slice to get rid of leading and trailing double quotes. + stmt.run( JSON.stringify(req.body, null, 4), coreid, pubAt, @@ -210,17 +213,24 @@ app.post('/', function(req, res){ mailOptions.text = 'An alert message was received: \n\n'; mailOptions.text = mailOptions.text + 'Status message, ' + status + '\n'; mailOptions.text = mailOptions.text + 'Published at, ' + alertmonUtils.getLocDateFromUTC(pubAt) + ' ' + alertmonUtils.getLocTimeFromUTC(pubAt) + '\n'; - mailOptions.text = mailOptions.text + 'From device, ' + alertmonUtils.getCoreNameFromCoreId(db, coreid) + '\n'; - - transporter.sendMail(mailOptions, function(error, info){ - if(error){ - console.log(error); - }else{ - console.log('Message sent: ' + info.response); + //mailOptions.text = mailOptions.text + 'From device, ' + alertmonUtils.getCoreNameFromCoreId(db, coreid) + '\n'; + var nameQry = 'SELECT coreName FROM Cores WHERE coreId = ?;' + db.get(nameQry, coreid, function(err, row){ + if ((err) || (typeof row == undefined)) { + // Don't care about this error or empty result set, still need to send email. + row.coreName = '# No Name #'; } + mailOptions.text = mailOptions.text + 'From device, ' + row.coreName + '\n'; + + transporter.sendMail(mailOptions, function(error, info){ + if(error){ + console.log(error); + }else{ + console.log('Message sent: ' + info.response); + } + }); }); } - //res.send(JSON.stringify(req.body, null, 4)); }); diff --git a/views/core.hbs b/views/core.hbs index 32f8809..4b17dbd 100644 --- a/views/core.hbs +++ b/views/core.hbs @@ -23,7 +23,7 @@ {{#each alerts}} - + {{this.pubDate}} {{this.pubTime}} {{this.status}}