send email notifications from watcher

This commit is contained in:
Richard Ramos 2019-11-13 19:52:05 -04:00
parent 5cb43894b4
commit 696610fec6
8 changed files with 38 additions and 24 deletions

View File

@ -1,5 +1,5 @@
const path = require("path");
const fs = require("fs");
class DAppConfig {
constructor(){
this.configurations = {};
@ -50,6 +50,15 @@ class DAppConfig {
return Object.values(dappConfig.templates.contracts[contract].events).filter(x => x.ABI.name === eventName && x.ABI.type === 'event');
}
getEmailTemplate(dappId, t){
// TODO: avoid reading this on each user/email
const templatePath = path.join("./dapps", dappId);
const subject = t.template.subject;
const text = fs.readFileSync(path.join(templatePath, t.template.text)).toString();
const html = fs.readFileSync(path.join(templatePath, t.template.html)).toString();
return {text, html, subject};
}
}
module.exports = DAppConfig;

View File

@ -25,6 +25,7 @@ module.exports = {
},
index: "seller",
template: {
subject: "New trade!",
html: "escrow-creation-seller.html",
text: "escrow-creation-seller.txt"
}
@ -42,6 +43,7 @@ module.exports = {
},
index: "buyer",
template: {
subject: "New trade!",
html: "escrow-creation-buyer.html",
text: "escrow-creation-buyer.txt"
}

View File

@ -0,0 +1 @@
<p>The seller was notified about this trade</p>

View File

@ -0,0 +1 @@
The seller was notified about this trade

View File

@ -1 +1 @@
<p>A new escrow was created!</p>
<p>A buyer is interested in your offer</p>

View File

@ -1 +1 @@
A new escrow was created!
A buyer is interested in your offer

View File

@ -1,28 +1,19 @@
const sgMail = require("@sendgrid/mail");
const path = require("path");
const fs = require('fs');
const fs = require("fs");
class SendGridMailer {
constructor(config) {
sgMail.setApiKey(config.SENDGRID_API_KEY);
}
send(dappId, template, data) {
// TODO: extract this logic. Mailer only needs to worry about sending emails
const templatePath = path.join("dapps", dappId);
const config = require(path.join(path.join('../', templatePath, 'config.js')));
const t = config.templates[template];
// TODO: do not read these files constantly. Keep it on a cache or something. Also, don't use Sync.
const text = fs.readFileSync(path.join(templatePath, t.text)).toString();
const html = fs.readFileSync(path.join(templatePath, t.html)).toString();
send(template, from, data) {
// TODO: data should be used for templating
const msg = {
to: data.email,
from: config.from,
subject: t.subject,
text,
html
from,
...template
};
sgMail.send(msg);

View File

@ -3,11 +3,12 @@ const config = require("../config");
const Database = require("../database");
const Ethereum = require("./ethereum");
const { addressCompare } = require("./utils");
const events = new Events();
const Mailer = require("../mail/sendgrid");
const DappConfig = require("../config/dapps");
const Subscribers = require("../models/subscriber");
const dapps = new DappConfig();
const events = new Events();
const dappConfig = new DappConfig();
const mailer = new Mailer(config);
const db = new Database(events, config);
const eth = new Ethereum(events, config);
@ -20,21 +21,29 @@ events.on("db:connected", () => {
const blockNum =
process.argv.length >= 3 ? parseInt(process.argv[2], 10) : 0;
dapps.getDapps().forEach(dappId => {
const contracts = dapps.contracts(dappId);
dappConfig.getDapps().forEach(dappId => {
const contracts = dappConfig.contracts(dappId);
contracts.forEach(address => {
eth.scan(dappId, address, dapps.ABI(dappId, address), blockNum);
eth.scan(dappId, address, dappConfig.ABI(dappId, address), blockNum);
});
});
});
});
events.on("web3:event", ({ dappId, address, event, returnValues }) => {
dapps.template(dappId, address, event).forEach(async template => {
dappConfig.template(dappId, address, event).forEach(async template => {
const users = await Subscribers.findActiveUsersByDapp(dappId);
users.forEach(user => {
if (addressCompare(returnValues[template.index], user.address)) {
console.log("//TODO: Send email!");
console.log("Sending email...");
mailer.send(
dappConfig.getEmailTemplate(dappId, template),
dappConfig.config(dappId).from,
{
email: user.email,
...returnValues
}
);
}
});
});
@ -42,3 +51,4 @@ events.on("web3:event", ({ dappId, address, event, returnValues }) => {
// TODO: handle errors sending email
// TODO: handle web3js disconnects
// TODO: support templates