Removing ng-di on the server.
This commit is contained in:
parent
ddb2f47a9d
commit
8697d79ba5
37 changed files with 2838 additions and 2878 deletions
|
@ -1,125 +1,122 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('ffffng')
|
||||
.service('MailTemplateService', function (
|
||||
UrlBuilder,
|
||||
config,
|
||||
_,
|
||||
async,
|
||||
deepExtend,
|
||||
fs,
|
||||
moment,
|
||||
Logger
|
||||
) {
|
||||
var templateBasePath = __dirname + '/../mailTemplates';
|
||||
var snippetsBasePath = templateBasePath + '/snippets';
|
||||
const _ = require('lodash')
|
||||
const async = require('async')
|
||||
const deepExtend = require('deep-extend')
|
||||
const fs = require('graceful-fs')
|
||||
const moment = require('moment')
|
||||
|
||||
var templateFunctions = {};
|
||||
const config = require('../config').config
|
||||
const Logger = require('../logger')
|
||||
const UrlBuilder = require('../utils/urlBuilder')
|
||||
|
||||
function renderSnippet(name, data) {
|
||||
var snippetFile = snippetsBasePath + '/' + name + '.html';
|
||||
const templateBasePath = __dirname + '/../mailTemplates';
|
||||
const snippetsBasePath = templateBasePath + '/snippets';
|
||||
|
||||
return _.template(fs.readFileSync(snippetFile).toString())(deepExtend(
|
||||
{},
|
||||
// jshint -W040
|
||||
this, // parent data
|
||||
// jshint +W040
|
||||
data,
|
||||
templateFunctions
|
||||
));
|
||||
}
|
||||
const templateFunctions = {};
|
||||
|
||||
function snippet(name) {
|
||||
return function (data) {
|
||||
return renderSnippet.bind(this)(name, data);
|
||||
};
|
||||
}
|
||||
function renderSnippet(name, data) {
|
||||
const snippetFile = snippetsBasePath + '/' + name + '.html';
|
||||
|
||||
function renderLink(href, text) {
|
||||
return _.template(
|
||||
'<a href="<%- href %>#" style="color: #E5287A;"><%- text %></a>'
|
||||
)({
|
||||
href: href,
|
||||
text: text || href
|
||||
});
|
||||
}
|
||||
return _.template(fs.readFileSync(snippetFile).toString())(deepExtend(
|
||||
{},
|
||||
// jshint -W040
|
||||
this, // parent data
|
||||
// jshint +W040
|
||||
data,
|
||||
templateFunctions
|
||||
));
|
||||
}
|
||||
|
||||
function renderHR() {
|
||||
return '<hr style="border-top: 1px solid #333333; border-left: 0; border-right: 0; border-bottom: 0;" />';
|
||||
}
|
||||
|
||||
function formatDateTime(unix) {
|
||||
return moment.unix(unix).locale('de').local().format('DD.MM.YYYY HH:mm');
|
||||
}
|
||||
|
||||
function formatFromNow(unix) {
|
||||
return moment.unix(unix).locale('de').fromNow();
|
||||
}
|
||||
|
||||
templateFunctions.header = snippet('header');
|
||||
templateFunctions.footer = snippet('footer');
|
||||
|
||||
templateFunctions.monitoringFooter = snippet('monitoring-footer');
|
||||
|
||||
templateFunctions.snippet = renderSnippet;
|
||||
|
||||
templateFunctions.link = renderLink;
|
||||
templateFunctions.hr = renderHR;
|
||||
|
||||
templateFunctions.formatDateTime = formatDateTime;
|
||||
templateFunctions.formatFromNow = formatFromNow;
|
||||
|
||||
return {
|
||||
configureTransporter: function (transporter) {
|
||||
var htmlToText = require('nodemailer-html-to-text').htmlToText;
|
||||
transporter.use('compile', htmlToText({
|
||||
tables: ['.table']
|
||||
}));
|
||||
},
|
||||
|
||||
render: function (mailOptions, callback) {
|
||||
var templatePathPrefix = templateBasePath + '/' + mailOptions.email;
|
||||
|
||||
async.parallel({
|
||||
subject: _.partial(fs.readFile, templatePathPrefix + '.subject.txt'),
|
||||
body: _.partial(fs.readFile, templatePathPrefix + '.body.html')
|
||||
},
|
||||
function (err, templates) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var data = deepExtend(
|
||||
{},
|
||||
mailOptions.data,
|
||||
{
|
||||
community: config.client.community,
|
||||
editNodeUrl: UrlBuilder.editNodeUrl()
|
||||
},
|
||||
templateFunctions
|
||||
);
|
||||
|
||||
function render(field) {
|
||||
return _.template(templates[field].toString())(data);
|
||||
}
|
||||
|
||||
var renderedTemplate;
|
||||
try {
|
||||
renderedTemplate = {
|
||||
subject: _.trim(render('subject')),
|
||||
body: render('body')
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
Logger
|
||||
.tag('mail', 'template')
|
||||
.error('Error rendering template for mail[' + mailOptions.id + ']:', error);
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
callback(null, renderedTemplate);
|
||||
}
|
||||
);
|
||||
}
|
||||
function snippet(name) {
|
||||
return function (data) {
|
||||
return renderSnippet.bind(this)(name, data);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function renderLink(href, text) {
|
||||
return _.template(
|
||||
'<a href="<%- href %>#" style="color: #E5287A;"><%- text %></a>'
|
||||
)({
|
||||
href: href,
|
||||
text: text || href
|
||||
});
|
||||
}
|
||||
|
||||
function renderHR() {
|
||||
return '<hr style="border-top: 1px solid #333333; border-left: 0; border-right: 0; border-bottom: 0;" />';
|
||||
}
|
||||
|
||||
function formatDateTime(unix) {
|
||||
return moment.unix(unix).locale('de').local().format('DD.MM.YYYY HH:mm');
|
||||
}
|
||||
|
||||
function formatFromNow(unix) {
|
||||
return moment.unix(unix).locale('de').fromNow();
|
||||
}
|
||||
|
||||
templateFunctions.header = snippet('header');
|
||||
templateFunctions.footer = snippet('footer');
|
||||
|
||||
templateFunctions.monitoringFooter = snippet('monitoring-footer');
|
||||
|
||||
templateFunctions.snippet = renderSnippet;
|
||||
|
||||
templateFunctions.link = renderLink;
|
||||
templateFunctions.hr = renderHR;
|
||||
|
||||
templateFunctions.formatDateTime = formatDateTime;
|
||||
templateFunctions.formatFromNow = formatFromNow;
|
||||
|
||||
module.exports = {
|
||||
configureTransporter (transporter) {
|
||||
const htmlToText = require('nodemailer-html-to-text').htmlToText;
|
||||
transporter.use('compile', htmlToText({
|
||||
tables: ['.table']
|
||||
}));
|
||||
},
|
||||
|
||||
render (mailOptions, callback) {
|
||||
const templatePathPrefix = templateBasePath + '/' + mailOptions.email;
|
||||
|
||||
async.parallel({
|
||||
subject: _.partial(fs.readFile, templatePathPrefix + '.subject.txt'),
|
||||
body: _.partial(fs.readFile, templatePathPrefix + '.body.html')
|
||||
},
|
||||
function (err, templates) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
const data = deepExtend(
|
||||
{},
|
||||
mailOptions.data,
|
||||
{
|
||||
community: config.client.community,
|
||||
editNodeUrl: UrlBuilder.editNodeUrl()
|
||||
},
|
||||
templateFunctions
|
||||
);
|
||||
|
||||
function render (field) {
|
||||
return _.template(templates[field].toString())(data);
|
||||
}
|
||||
|
||||
let renderedTemplate;
|
||||
try {
|
||||
renderedTemplate = {
|
||||
subject: _.trim(render('subject')),
|
||||
body: render('body')
|
||||
};
|
||||
} catch (error) {
|
||||
Logger
|
||||
.tag('mail', 'template')
|
||||
.error('Error rendering template for mail[' + mailOptions.id + ']:', error);
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
callback(null, renderedTemplate);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue