refactor dapp config for obtaining email templates
This commit is contained in:
parent
696610fec6
commit
0b66661b9a
40
api/index.js
40
api/index.js
|
@ -1,5 +1,5 @@
|
|||
const Events = require("events");
|
||||
const stripHexPrefix = require('strip-hex-prefix');
|
||||
const stripHexPrefix = require("strip-hex-prefix");
|
||||
const { isSignatureValid } = require("./utils");
|
||||
const express = require("express");
|
||||
const { check, validationResult } = require("express-validator");
|
||||
|
@ -11,25 +11,23 @@ const Database = require("../database");
|
|||
const events = new Events();
|
||||
const Subscriber = require("../models/subscriber");
|
||||
const subscriberStatus = require("../models/subscriber-status");
|
||||
const Mailer = require('../mail/sendgrid');
|
||||
const DappConfig = require('../config/dapps');
|
||||
const Mailer = require("../mail/sendgrid");
|
||||
const DappConfig = require("../config/dapps");
|
||||
|
||||
const dapps = new DappConfig();
|
||||
const dappConfig = new DappConfig();
|
||||
const mailer = new Mailer(config);
|
||||
const db = new Database(events, config);
|
||||
|
||||
db.init();
|
||||
|
||||
|
||||
const hexValidator = value => {
|
||||
const regex = /^[0-9A-Fa-f]*$/g;
|
||||
if(regex.test(stripHexPrefix(value))){
|
||||
if (regex.test(stripHexPrefix(value))) {
|
||||
return true;
|
||||
}
|
||||
throw new Error('Invalid hex string');
|
||||
throw new Error("Invalid hex string");
|
||||
};
|
||||
|
||||
|
||||
events.on("db:connected", () => {
|
||||
const app = express();
|
||||
|
||||
|
@ -45,11 +43,11 @@ events.on("db:connected", () => {
|
|||
[
|
||||
check("signature")
|
||||
.exists()
|
||||
.isLength({min: 132, max: 132})
|
||||
.isLength({ min: 132, max: 132 })
|
||||
.custom(hexValidator),
|
||||
check("address")
|
||||
.exists()
|
||||
.isLength({min: 42, max: 42})
|
||||
.isLength({ min: 42, max: 42 })
|
||||
.custom(hexValidator),
|
||||
check("email")
|
||||
.exists()
|
||||
|
@ -67,7 +65,7 @@ events.on("db:connected", () => {
|
|||
return res.status(404).json({ errors: errors.array() });
|
||||
}
|
||||
|
||||
if(!dapps.isDapp(dappId)){
|
||||
if (!dappConfig.isDapp(dappId)) {
|
||||
return res.status(404).send("Invalid dapp");
|
||||
}
|
||||
|
||||
|
@ -92,8 +90,15 @@ events.on("db:connected", () => {
|
|||
status: subscriberStatus.CONFIRMED // TODO: remove this once email confirmation is done
|
||||
});
|
||||
|
||||
mailer.send(dappId, 'sign-up', { email });
|
||||
const template = dappConfig.template(dappId, "sign-up");
|
||||
|
||||
mailer.send(
|
||||
dappConfig.getEmailTemplate(dappId, template),
|
||||
dappConfig.config(dappId).from,
|
||||
{
|
||||
email
|
||||
}
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
// TODO: add global error handler
|
||||
|
@ -105,12 +110,13 @@ events.on("db:connected", () => {
|
|||
);
|
||||
|
||||
app.post("/:dapp/unsubscribe", async (req, res) => {
|
||||
// TODO:
|
||||
const {
|
||||
params: { dappId },
|
||||
body: { address, email, signature }
|
||||
} = req;
|
||||
|
||||
if(!dapps.isDapp(dappId)){
|
||||
if (!dappConfig.isDapp(dappId)) {
|
||||
return res.status(404).send("Invalid dapp");
|
||||
}
|
||||
|
||||
|
@ -133,9 +139,11 @@ events.on("db:connected", () => {
|
|||
return res.status(200).send("OK");
|
||||
});
|
||||
|
||||
app.get("/confirm/:token", (req, res) => {});
|
||||
app.get("/confirm/:token", (req, res) => {
|
||||
// TODO:
|
||||
});
|
||||
|
||||
app.get("/", (req, res) => res.status(200).json({ status: isConnected() }));
|
||||
app.get("/", (req, res) => res.status(200).json({ ok: true }));
|
||||
|
||||
app.listen(config.PORT, () =>
|
||||
console.log(`App listening on port ${config.PORT}!`)
|
||||
|
@ -145,7 +153,5 @@ events.on("db:connected", () => {
|
|||
// MVP
|
||||
// ====
|
||||
|
||||
// Folder with DApp information, event ABI, and email templates
|
||||
|
||||
// TODO: register DAPP and content
|
||||
// TODO: handle errors sending email
|
||||
|
|
|
@ -45,17 +45,23 @@ class DAppConfig {
|
|||
return ABI;
|
||||
}
|
||||
|
||||
template(dappId, contract, eventName) {
|
||||
template(dappId, templateName) {
|
||||
const dappConfig = this.config(dappId);
|
||||
return dappConfig.templates[templateName];
|
||||
}
|
||||
|
||||
eventConfig(dappId, contract, eventName) {
|
||||
const dappConfig = this.config(dappId);
|
||||
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
|
||||
getEmailTemplate(dappId, template){
|
||||
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();
|
||||
const subject = template.subject;
|
||||
|
||||
// TODO: avoid reading this on each user/email
|
||||
const text = fs.readFileSync(path.join(templatePath, template.text)).toString();
|
||||
const html = fs.readFileSync(path.join(templatePath, template.html)).toString();
|
||||
return {text, html, subject};
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@ const config = {
|
|||
SENDGRID_API_KEY: secret.SENDGRID_API_KEY,
|
||||
/* WATCHER */
|
||||
BLOCK_DELAY: 12, // 15-170 secs
|
||||
EVENTS_RANGE: 30,
|
||||
POLL_SLEEP: 1 // seconds
|
||||
EVENTS_RANGE: 20, // blocks
|
||||
POLL_SLEEP: 2 // seconds
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
|
|
|
@ -31,13 +31,13 @@ events.on("db:connected", () => {
|
|||
});
|
||||
|
||||
events.on("web3:event", ({ dappId, address, event, returnValues }) => {
|
||||
dappConfig.template(dappId, address, event).forEach(async template => {
|
||||
dappConfig.eventConfig(dappId, address, event).forEach(async eventConfig => {
|
||||
const users = await Subscribers.findActiveUsersByDapp(dappId);
|
||||
users.forEach(user => {
|
||||
if (addressCompare(returnValues[template.index], user.address)) {
|
||||
if (addressCompare(returnValues[eventConfig.index], user.address)) {
|
||||
console.log("Sending email...");
|
||||
mailer.send(
|
||||
dappConfig.getEmailTemplate(dappId, template),
|
||||
dappConfig.getEmailTemplate(dappId, eventConfig.template),
|
||||
dappConfig.config(dappId).from,
|
||||
{
|
||||
email: user.email,
|
||||
|
|
Loading…
Reference in New Issue