logger fixed

This commit is contained in:
Dani Herrero 2017-06-01 08:20:05 +02:00
parent 36a5051634
commit 6dcf37fc5a
1 changed files with 23 additions and 43 deletions

View File

@ -1,48 +1,28 @@
var fs = require('fs');
var mustache = require('mustache');
var database = require('./database.js');
var language = require('./language.js');
var path = require('path');
var Template = {
/**
* Tipos de advertencias
*/
global.__LOG_INFO = 1;
global.__LOG_WARNING = 2;
global.__LOG_ERROR = 3;
module.exports = {
/**
* Obtiene la plantilla.
* @param {String} templateName - Nombre de la plantilla
* @param {Object} langCode - Código del idioma
* @param {Object} params - Datos a reemplazar.
* @param {Object} callback - Callback
* Imprimir advertencia directamente en consola
* @param {Integer} type Constante tipo de advertencia
* @param {String} message Mensaje a mostrar
*/
getTemplate: function(templateName, langCode, params, callback) {
database.pool.query('SELECT name, attachmentPath FROM vn.mailTemplates WHERE name = ?', [templateName], function(error, rs) {
// Comprobamos que exista la plantilla
if (rs.length == 0)
throw new Error('La plantilla ' + templateName + ' no existe');
var langParams = language.load(templateName, langCode);
params = Object.assign({}, langParams, params);
var templatePath = path.join(__dirname, 'Template', `${rs[0].name}.html`);
if (!fs.existsSync(templatePath))
throw new Error('No se ha podido cargar la plantilla ' + templateName + '.html');
Template.render(templatePath, params, function(tplBody) {
callback({subject: params.subject, body: tplBody});
});
});
},
/**
* 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));
});
print: function(type, message) {
switch (type) {
case __LOG_INFO:
console.log(`[INFO] -> ${message}`);
break;
case __LOG_WARNING:
console.log(`[WARN] -> ${message}`);
break;
case __LOG_ERROR:
console.log(`[ERROR] -> ${message}`);
break;
}
}
};
module.exports = Template;