salix/services/mailer/Application/template.js

49 lines
1.5 KiB
JavaScript

var fs = require('fs');
var mustache = require('mustache');
var database = require('./database.js');
var logger = require('./logger.js');
var Template = {
/**
* Obtiene la plantilla
* @param {String} name - Nombre de la plantilla
* @param {Object} params - Datos a reemplazar.
* @param {Object} callback - Callback
*/
getTemplate: function(name, params, callback) {
database.pool.query('SELECT name, attachmentPath FROM vn.mailTemplates WHERE name = ?', [name], function(error, rs)
{
if (rs.length == 0) {
logger.print(__LOG_ERROR, 'La plantilla ' + name + ' no existe');
return;
}
var path = './Application/Template/' + rs[0].name + '.html';
if (!fs.existsSync(path)) {
logger.print(__LOG_ERROR, 'No se ha podido cargar la plantilla ' + name + '.html');
} else {
Template.render(path, params, function(body) {
callback(body);
});
}
});
},
/**
* Renderiza las plantillas
* @param {String} path - Ruta de la plantilla
* @param {Object} params - Listado de parámetros a remplazar
* @param {Object} callback - Callback
*/
render: function(path, params, callback) {
fs.readFile(path, 'utf8', function(error, body) {
mustache.parse(body);
callback(mustache.render(body, params));
});
}
};
module.exports = Template;