From 648c136fcf8ae0f695781ef13f3c8ebbeb24fb24 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 24 May 2017 07:34:34 +0000 Subject: [PATCH] add mailer --- lib/apis.js | 74 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index 0655657..fb65f9a 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -11,6 +11,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { // TODO xconfx.apispath xconfx.restPath = path.join(__dirname, '..', '..', 'packages', 'rest'); xconfx.appApiGrantsPath = path.join(__dirname, '..', '..', 'packages', 'client-api-grants'); + xconfx.appConfigPath = path.join(__dirname, '..', '..', 'var'); function notConfigured(req, res) { var msg = "api package '" + req.pkgId + "' not configured for client uri '" + req.experienceId + "'" @@ -54,6 +55,12 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { }); } + function getSiteConfig(clientUrih) { + return fs.readFileAsync(path.join(xconfx.appConfigPath, clientUrih + '.json'), 'utf8').then(function (text) { + return JSON.parse(text); + }).then(function (data) { return data; }, function (/*err*/) { return {}; }); + } + function loadRestHelper(myConf, pkgId) { var pkgPath = path.join(myConf.restPath, pkgId); @@ -229,30 +236,55 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { return null; } - if (localCache.rests[pkgId]) { - localCache.rests[pkgId].handler(req, res, next); - hasBeenHandled = true; + return getSiteConfig(clientUrih).then(function (siteConfig) { + /* + Object.defineProperty(req, 'siteConfig', { + enumerable: true + , configurable: false + , writable: false + , value: siteConfig + }); + */ + Object.defineProperty(req, 'getSiteMailer', { + enumerable: true + , configurable: false + , writable: false + , value: function getSiteMailer() { + var nodemailer = require('nodemailer'); + var transport = require('nodemailer-mailgun-transport'); + //var mailconf = require('../../../com.daplie.mailer/config.mailgun'); + var mailconf = siteConfig['mailgun.org']; + var mailer = PromiseA.promisifyAll(nodemailer.createTransport(transport(mailconf))); - if (now - localCache.rests[pkgId].createdAt > staleAfter) { - localCache.rests[pkgId] = null; - } - } - - if (!localCache.rests[pkgId]) { - //return doesThisPkgExist - - return loadRestHandler(xconfx, pkgId).then(function (myHandler) { - if (!myHandler) { - notConfigured(req, res); - return; - } - - localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now }; - if (!hasBeenHandled) { - myHandler.handler(req, res, next); + return mailer; } }); - } + + if (localCache.rests[pkgId]) { + localCache.rests[pkgId].handler(req, res, next); + hasBeenHandled = true; + + if (now - localCache.rests[pkgId].createdAt > staleAfter) { + localCache.rests[pkgId] = null; + } + } + + if (!localCache.rests[pkgId]) { + //return doesThisPkgExist + + return loadRestHandler(xconfx, pkgId).then(function (myHandler) { + if (!myHandler) { + notConfigured(req, res); + return; + } + + localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now }; + if (!hasBeenHandled) { + myHandler.handler(req, res, next); + } + }); + } + }); }); }); };