From 5227b1faa132b0f0224f8632bd8b62112eb8e8f0 Mon Sep 17 00:00:00 2001 From: Ro Date: Thu, 2 Jul 2020 13:34:39 -0700 Subject: [PATCH] removed preview temporarily, got mailer api working again --- brain/api/{mail => v1}/mailer.js | 25 +++++++++++++------------ brain/app.js | 5 ++--- brain/views/partials/mailforms.pug | 6 +++--- brain/views/partials/options.pug | 2 +- src/com/actions/Mailer.js | 29 +++++++++++------------------ src/com/events/DataEvent.js | 1 + src/libraries/FipamoAdminAPI.js | 18 ++++++++++++++++++ src/styles/main/_posts.styl | 2 +- 8 files changed, 50 insertions(+), 38 deletions(-) rename brain/api/{mail => v1}/mailer.js (72%) diff --git a/brain/api/mail/mailer.js b/brain/api/v1/mailer.js similarity index 72% rename from brain/api/mail/mailer.js rename to brain/api/v1/mailer.js index defb9b2..e407424 100644 --- a/brain/api/mail/mailer.js +++ b/brain/api/v1/mailer.js @@ -1,11 +1,13 @@ +import Settings, { SETTINGS_FILE } from '../../data/Settings'; var express = require('express'); var router = express.Router(); var nodemailer = require('nodemailer'); var mg = require('nodemailer-mailgun-transport'); -const fs = require('fs-extra'); const pug = require('pug'); -router.post('/', function(req, res) { - fs.readJson('config/site-settings.json') +const settings = new Settings(); +router.post('/', function (req, res) { + settings + .load(SETTINGS_FILE) .then(settings => { let transport = ''; var auth = ''; @@ -17,8 +19,8 @@ router.post('/', function(req, res) { secure: false, auth: { type: 'login', - email: settings.email.smtp, - password: settings.email.smtp.password + user: settings.email.smtp, + pass: settings.email.smtp.password } }; transport = nodemailer.createTransport(auth); @@ -26,33 +28,32 @@ router.post('/', function(req, res) { case 'option-mg': auth = { auth: { - api_key: settings.email.mailgun['api-key'], + api_key: settings.email.mailgun.key, domain: settings.email.mailgun.domain } }; transport = nodemailer.createTransport(mg(auth)); break; } - let render = pug.compileFile('themes/dash/email/base.pug'); + let render = pug.compileFile('brain/views/email/base.pug'); let html = render({ - title: settings.title, - header: 'a note from ' + settings.title, + title: settings.global.title, + header: 'a note from ' + settings.global.title, content: req.body.content, footer: 'powered by fipamo' }); transport.sendMail( { from: 'control@playvico.us', - to: 'are0h@protonmail.com', // An array if you have multiple recipients. + to: req.session.user.email, // An array if you have multiple recipients. subject: 'Hey beautiful', //You can use "html:" to send HTML email content. It's magic! html: html //You can use "text:" to send plain-text content. It's oldschool! //text: 'Mailgun rocks, pow pow!' }, - function(err, info) { + function (err, info) { if (err) { - //console.log(err); res.json({ message: 'MAIL ERROR', desc: err diff --git a/brain/app.js b/brain/app.js index 1dd6319..13b96b9 100644 --- a/brain/app.js +++ b/brain/app.js @@ -48,20 +48,19 @@ var nav = require('./routes/dash/nav'); //api var pages = require('./api/v1/pages'); var setting = require('./api/v1/settings'); -//var mailer = require('./api/mail/mailer'); +var mailer = require('./api/v1/mailer'); var auth = require('./api/v1/auth'); // API PATHS app.use('/api/v1/page', pages); app.use('/api/v1/settings', setting); -//app.use('/api/mail', mailer); app.use('/api/v1/auth', auth); +app.use('/api/v1/mailer', mailer); // PAGES app.use('/@/dashboard', dash); app.use('/@/dashboard/page', page); app.use('/@/dashboard/settings', settings); app.use('/@/dashboard/navigation', nav); -//app.use('/mailer', mailer); // catch 404 and forward to error handler app.use(function (req, res, next) { var err = new Error('Not Found'); diff --git a/brain/views/partials/mailforms.pug b/brain/views/partials/mailforms.pug index 9b7932d..f50df96 100644 --- a/brain/views/partials/mailforms.pug +++ b/brain/views/partials/mailforms.pug @@ -6,7 +6,7 @@ input(type='text', name='smtp-pass' id='smtp-pass', placeholder='password', value=settings.email.smtp.password , autofocus) #mail-mg(data-enabled='false') input(type='text', name='mg-domain' id='mg-domain', placeholder='domain', value=settings.email.mailgun.domain autofocus) - input(type='text', name='mg-key' id='mg-key', placeholder='api key', value=settings.email.mailgun['api-key'] , autofocus) + input(type='text', name='mg-key' id='mg-key', placeholder='api key', value=settings.email.mailgun.key , autofocus) -else if(settings.email.active == "option-mg") #mail-smtp(data-enabled='false') input(type='text', name='smtp-domain' id='smtp-domain', placeholder='domain', value=settings.email.smtp.domain autofocus) @@ -14,7 +14,7 @@ input(type='text', name='smtp-pass' id='smtp-pass', placeholder='password', value=settings.email.smtp.password , autofocus) #mail-mg(data-enabled='mg') input(type='text', name='mg-domain' id='mg-domain', placeholder='domain', value=settings.email.mailgun.domain autofocus) - input(type='text', name='mg-key' id='mg-key', placeholder='api key', value=settings.email.mailgun['api-key'] , autofocus) + input(type='text', name='mg-key' id='mg-key', placeholder='api key', value=settings.email.mailgun.key , autofocus) -else #mail-smtp(data-enabled='false') input(type='text', name='smtp-domain' id='smtp-domain', placeholder='domain', value=settings.email.smtp.domain autofocus) @@ -22,4 +22,4 @@ input(type='text', name='smtp-pass' id='smtp-pass', placeholder='password', value=settings.email.smtp.password , autofocus) #mail-mg(data-enabled='false') input(type='text', name='mg-domain' id='mg-domain', placeholder='domain', value=settings.email.mailgun.domain autofocus) - input(type='text', name='mg-key' id='mg-key', placeholder='api key', value=settings.email.mailgun['api-key'] , autofocus) \ No newline at end of file + input(type='text', name='mg-key' id='mg-key', placeholder='api key', value=settings.email.mailgun.key , autofocus) \ No newline at end of file diff --git a/brain/views/partials/options.pug b/brain/views/partials/options.pug index c2336bd..cb07f21 100644 --- a/brain/views/partials/options.pug +++ b/brain/views/partials/options.pug @@ -8,6 +8,6 @@ button#option-published.option-inactive.post-option-btn(data-active= status[2]) svg#option-published-icon(viewBox="0 0 20 20" class="icons") use#option-published-icon(xlink:href='/assets/images/global/sprite.svg#entypo-globe') - button#option-preview.option-inactive(data-active="false") + //button#option-preview.option-inactive(data-active="false") svg#option-preview-icon(viewBox="0 0 20 20" class="icons") use#option-preview-icon(xlink:href='/assets/images/global/sprite.svg#entypo-eye') \ No newline at end of file diff --git a/src/com/actions/Mailer.js b/src/com/actions/Mailer.js index 0ddfcf9..498a0e8 100644 --- a/src/com/actions/Mailer.js +++ b/src/com/actions/Mailer.js @@ -1,33 +1,26 @@ -import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/DataUtils'; -import * as DataEvent from '../../../src/com/events/DataEvent'; +import FipamoAdminAPI from '../../libraries/FipamoAdminAPI'; +import Notficaton from '../ui/Notifications'; +const admin = new FipamoAdminAPI(); +const notify = new Notficaton(); export default class Mailer { //-------------------------- // constructor //-------------------------- - constructor() { - this.dataUtils = new DataUtils(); - } + constructor() {} //-------------------------- // methods //-------------------------- sendMail() { - var self = this; let mailData = { content: 'This is a test email' }; - self.dataUtils - .request( - '/api/mail', - DataEvent.SETTINGS_UPDATED, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - mailData - ) - .then(() => { - //console.log(response); + admin + .sendMail(mailData) + .then(result => { + notify.alert(result.message, true); }) - .catch(() => { - //console.log(err); + .catch(err => { + notify.alert(err.message, false); }); } //-------------------------- diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index b5ceac4..77b1fc0 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -32,6 +32,7 @@ export const API_RENDER_PAGES = 'renderPages'; export const API_INIT = 'blogInit'; export const API_INIT_GOOD = 'blogInitGood'; export const API_INIT_LAME = 'blogInitLame'; +export const SEND_MAIL = 'sendMail'; class DataEvent { //-------------------------- // methods diff --git a/src/libraries/FipamoAdminAPI.js b/src/libraries/FipamoAdminAPI.js index b0a6d03..fedcfe4 100644 --- a/src/libraries/FipamoAdminAPI.js +++ b/src/libraries/FipamoAdminAPI.js @@ -18,6 +18,7 @@ export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar'; export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background'; export const API_PUBLISH_PAGES = '/api/v1/settings/publish-pages'; export const API_NAV_SYNC = '/api/v1/settings/nav-sync'; +export const API_SEND_MAIL = '/api/v1/mailer'; import * as DataEvent from '../com/events/DataEvent'; export default class APIUtils { //-------------------------- @@ -170,6 +171,23 @@ export default class APIUtils { }); }); } + sendMail(message) { + return new Promise((resolve, reject) => { + this._request( + API_SEND_MAIL, + DataEvent.SEND_MAIL, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + message + ) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } //-------------------------- // private //-------------------------- diff --git a/src/styles/main/_posts.styl b/src/styles/main/_posts.styl index 959f33b..5e164c9 100644 --- a/src/styles/main/_posts.styl +++ b/src/styles/main/_posts.styl @@ -193,7 +193,7 @@ border-radius 0 3px 3px 0 button - width 25% + width 33.3% height 39px object-transitions(0.3s) margin 0