From acd47767d0c02dfa1a4152208a16c21255e4943b Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 22 Sep 2016 10:11:19 -0700 Subject: Upgrade sendgrid and fill in transaction details in notification email --- package.json | 2 +- public/js/profile.coffee | 1 - public/js/profile.js | 1 - routes/transactions.js | 54 +++++++++++++++++++++++------------- routes/users.js | 42 +++++++++++++++++----------- src/routes/transactions.coffee | 45 ++++++++++++++++++------------ src/routes/users.coffee | 41 +++++++++++++++++---------- views/transactions/notification.html | 2 +- views/users/profile.html | 5 +--- 9 files changed, 117 insertions(+), 76 deletions(-) diff --git a/package.json b/package.json index 0f94d4b8..ec334a23 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "passport-local": "1.0.0", "redis": "0.11.0", "request": "~2.40.0", - "sendgrid": "~1.2.0" + "sendgrid": "^4.3.0" }, "devDependencies": { "coffeeify": "^1.2.0", diff --git a/public/js/profile.coffee b/public/js/profile.coffee index dda0597c..fdbde8ee 100644 --- a/public/js/profile.coffee +++ b/public/js/profile.coffee @@ -7,7 +7,6 @@ $(-> user = $('#username').val() $.getJSON("/#{user}.json", (data) -> - $('#username').val(data.username) $('#email').val(data.email) $('#profile').fadeIn() ) diff --git a/public/js/profile.js b/public/js/profile.js index d680e209..a49c7583 100644 --- a/public/js/profile.js +++ b/public/js/profile.js @@ -12,7 +12,6 @@ $('#title').focus(); user = $('#username').val(); $.getJSON("/" + user + ".json", function(data) { - $('#username').val(data.username); $('#email').val(data.email); return $('#profile').fadeIn(); }); diff --git a/routes/transactions.js b/routes/transactions.js index 31875918..d132e418 100644 --- a/routes/transactions.js +++ b/routes/transactions.js @@ -1,8 +1,12 @@ (function() { - var db; + var config, db, moment; db = require('../redis'); + config = require("../config"); + + moment = require('moment'); + module.exports = { index: function(req, res) { return res.render('transactions/index', { @@ -53,6 +57,35 @@ create: function(req, res) { var finish; finish = function() { + db.hgetall("user:" + req.params.user.toLowerCase(), function(err, user) { + return res.render('transactions/notification', { + layout: 'mail', + amount: (req.body.received * req.body.exchange).toFixed(2).toString() + ' ' + user.currency, + address: req.body.address, + txid: req.body.txid, + js: (function() { + return global.js; + }), + css: (function() { + return global.css; + }) + }, function(err, html) { + var content, from_email, helper, mail, request, sg, subject, to_email; + helper = require('sendgrid').mail; + from_email = new helper.Email('info@coinos.io'); + to_email = new helper.Email(user.email); + subject = 'Transaction Sent'; + content = new helper.Content('text/html', html); + mail = new helper.Mail(from_email, subject, to_email, content); + sg = require('sendgrid')(config.sendgrid_token); + request = sg.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: mail.toJSON() + }); + return sg.API(request); + }); + }); res.write(JSON.stringify(req.body)); return res.end(); }; @@ -62,25 +95,6 @@ if (result) { return finish(); } - res.render('notification', { - layout: 'mail', - js: (function() { - return global.js; - }), - css: (function() { - return global.css; - }) - }, function(err, html) { - var email, sendgrid; - sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password); - email = new sendgrid.Email({ - to: 'asoltys@gmail.com', - from: 'info@coinos.io', - subject: 'Transaction Sent', - html: html - }); - return sendgrid.send(email); - }); multi = db.multi(); multi.hmset(req.body.txid, req.body); multi.rpush("" + req.params.user + ":transactions", req.body.txid); diff --git a/routes/users.js b/routes/users.js index 1449eb02..2c79cb31 100644 --- a/routes/users.js +++ b/routes/users.js @@ -154,15 +154,20 @@ return global.css; }) }, function(err, html) { - var email, sendgrid; - sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password); - email = new sendgrid.Email({ - to: req.body.email, - from: 'adam@coinos.io', - subject: 'Welcome to CoinOS', - html: html + var content, from_email, helper, mail, sg, subject, to_email; + helper = require('sendgrid').mail; + from_email = new helper.Email('info@coinos.io'); + to_email = new helper.Email(req.body.email); + subject = 'Welcome to CoinOS'; + content = new helper.Content('text/html', html); + mail = new helper.Mail(from_email, subject, to_email, content); + sg = require('sendgrid')(config.sendgrid_token); + request = sg.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: mail.toJSON() }); - return sendgrid.send(email); + return sg.API(request); }); }); }); @@ -232,15 +237,20 @@ return global.css; }) }, function(err, html) { - var email, sendgrid; - sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password); - email = new sendgrid.Email({ - to: req.body.email, - from: 'adam@coinos.io', - subject: 'CoinOS Wallet Key', - html: html + var content, from_email, helper, mail, sg, subject, to_email; + helper = require('sendgrid').mail; + from_email = new helper.Email('info@coinos.io'); + to_email = new helper.Email(req.body.email); + subject = 'CoinOS Wallet Key'; + content = new helper.Content('text/html', html); + mail = new helper.Mail(from_email, subject, to_email, content); + sg = require('sendgrid')(config.sendgrid_token); + request = sg.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: mail.toJSON() }); - return sendgrid.send(email); + return sg.API(request); }); } }, diff --git a/src/routes/transactions.coffee b/src/routes/transactions.coffee index cadb73b1..696eefba 100644 --- a/src/routes/transactions.coffee +++ b/src/routes/transactions.coffee @@ -1,4 +1,6 @@ db = require('../redis') +config = require("../config") +moment = require('moment') module.exports = index: (req, res) -> @@ -39,6 +41,32 @@ module.exports = create: (req, res) -> finish = -> + db.hgetall("user:"+req.params.user.toLowerCase(), (err, user) -> + res.render('transactions/notification', + layout: 'mail' + amount: (req.body.received * req.body.exchange).toFixed(2).toString() + ' ' + user.currency + address: req.body.address + txid: req.body.txid + js: (-> global.js) + css: (-> global.css) + (err, html) -> + helper = require('sendgrid').mail + from_email = new helper.Email('info@coinos.io') + to_email = new helper.Email(user.email) + subject = 'Transaction Sent' + content = new helper.Content('text/html', html) + mail = new helper.Mail(from_email, subject, to_email, content) + + sg = require('sendgrid')(config.sendgrid_token) + request = sg.emptyRequest( + method: 'POST' + path: '/v3/mail/send' + body: mail.toJSON() + ) + + sg.API(request) + ) + ) res.write(JSON.stringify(req.body)) res.end() @@ -46,23 +74,6 @@ module.exports = db.exists(req.body.txid, (err, result) -> return finish() if result - res.render('notification', - layout: 'mail', - js: (-> global.js), - css: (-> global.css), - (err, html) -> - sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password) - - email = new sendgrid.Email( - to: 'asoltys@gmail.com' - from: 'info@coinos.io' - subject: 'Transaction Sent' - html: html - ) - - sendgrid.send(email) - ) - multi = db.multi() multi.hmset(req.body.txid, req.body) multi.rpush("#{req.params.user}:transactions", req.body.txid) diff --git a/src/routes/users.coffee b/src/routes/users.coffee index e682602b..d1c8f602 100644 --- a/src/routes/users.coffee +++ b/src/routes/users.coffee @@ -112,16 +112,22 @@ module.exports = (sessions) -> js: (-> global.js), css: (-> global.css), (err, html) -> - sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password) - email = new sendgrid.Email( - to: req.body.email - from: 'adam@coinos.io' - subject: 'Welcome to CoinOS' - html: html + helper = require('sendgrid').mail + from_email = new helper.Email('info@coinos.io') + to_email = new helper.Email(req.body.email) + subject = 'Welcome to CoinOS' + content = new helper.Content('text/html', html) + mail = new helper.Mail(from_email, subject, to_email, content) + + sg = require('sendgrid')(config.sendgrid_token) + request = sg.emptyRequest( + method: 'POST' + path: '/v3/mail/send' + body: mail.toJSON() ) - sendgrid.send(email) + sg.API(request) ) ) ) @@ -178,16 +184,21 @@ module.exports = (sessions) -> js: (-> global.js), css: (-> global.css), (err, html) -> - sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password) - - email = new sendgrid.Email( - to: req.body.email - from: 'adam@coinos.io' - subject: 'CoinOS Wallet Key' - html: html + helper = require('sendgrid').mail + from_email = new helper.Email('info@coinos.io') + to_email = new helper.Email(req.body.email) + subject = 'CoinOS Wallet Key' + content = new helper.Content('text/html', html) + mail = new helper.Mail(from_email, subject, to_email, content) + + sg = require('sendgrid')(config.sendgrid_token) + request = sg.emptyRequest( + method: 'POST' + path: '/v3/mail/send' + body: mail.toJSON() ) - sendgrid.send(email) + sg.API(request) ) verify: (req, res) -> diff --git a/views/transactions/notification.html b/views/transactions/notification.html index 49a045a7..5f066cf8 100644 --- a/views/transactions/notification.html +++ b/views/transactions/notification.html @@ -1,3 +1,3 @@

Transaction Received

-You got paid +You received {{amount}} from {{address}}. Visit https://blockchain.info/tx/{{txid}} for more details. diff --git a/views/users/profile.html b/views/users/profile.html index 70f9acfc..0f37d20e 100644 --- a/views/users/profile.html +++ b/views/users/profile.html @@ -5,10 +5,7 @@