diff --git a/services/mailer/Application/Route/manuscript.js b/services/mailer/Application/Route/manuscript.js index de66bfa741..0c51be6cb3 100644 --- a/services/mailer/Application/Route/manuscript.js +++ b/services/mailer/Application/Route/manuscript.js @@ -7,20 +7,6 @@ var template = require('../template.js'); // Escrito de cambios en méto de pago del cliente router.post('/paymentUpdate', function(request, response) { database.pool.query('SELECT Cliente, `e-mail` AS email FROM Clientes WHERE Id_Cliente = ?', [request.body.user], function(error, rs) { -<<<<<<< HEAD - var params = { - clientName: rs[0].Cliente - }; - - template.getTemplate('paymentUpdate', params, function(body) { - var data = { - recipient: rs[0].email, - subject: 'Cambios en las condiciones de pago', - body: body - }; - - if (mail.send(data)) { -======= // Datos del escrito var params = { clientName: rs[0].Cliente @@ -28,7 +14,6 @@ router.post('/paymentUpdate', function(request, response) { // Obtener plantilla y enviar template.getTemplate('paymentUpdate', 'es', params, function(tplResult) { if (mail.send({recipient: rs[0].email, subject: tplResult.subject, body: tplResult.body})) { ->>>>>>> 0c82c83ca33798a69686c98e44de2753bc50c021 response.json({status: "OK"}); } else { response.json({status: "ERROR"}); diff --git a/services/mailer/Application/logger.js b/services/mailer/Application/logger.js index 80b18a6e22..89267f3988 100644 --- a/services/mailer/Application/logger.js +++ b/services/mailer/Application/logger.js @@ -1,59 +1,48 @@ -<<<<<<< HEAD - -// Tipos de advertencias -global.__LOG_INFO = 1; -global.__LOG_WARNING = 2; -global.__LOG_ERROR = 3; - -module.exports = { - - /** - * Imprimir advertencia directamente en consola - * @param {Integer} type - Constante tipo de advertencia - */ - print: function(type, message) - { - if (type == __LOG_INFO) - { - console.log('[INFORMACIÓN] -> ' + message); - } - else if(type == __LOG_WARNING) - { - console.log('[ADVERTENCIA] -> ' + message); - } - else if(type == __LOG_ERROR) - { - console.log('[ERROR] -> ' + message); - } - } -} -======= +var fs = require('fs'); +var mustache = require('mustache'); +var database = require('./database.js'); +var language = require('./language.js'); +var path = require('path'); -/** - * Tipos de advertencias - */ -global.__LOG_INFO = 1; -global.__LOG_WARNING = 2; -global.__LOG_ERROR = 3; - -module.exports = { +var Template = { /** - * Imprimir advertencia directamente en consola - * @param {Integer} type Constante tipo de advertencia - * @param {String} message Mensaje a mostrar + * 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 */ - 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; - } + 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)); + }); } }; ->>>>>>> 0c82c83ca33798a69686c98e44de2753bc50c021 + +module.exports = Template;