diff --git a/.vscode/launch.json b/.vscode/launch.json index ecebe6bce5..b35ebd9e4f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,26 +1,6 @@ { "version": "0.2.0", "configurations": [ - { - "name": "Iniciar", - "type": "node", - "request": "launch", - "program": "${workspaceRoot}/services/auth/server/server.js", - "stopOnEntry": false, - "args": [], - "cwd": "${workspaceRoot}", - "preLaunchTask": null, - "runtimeExecutable": null, - "runtimeArgs": [ - "--nolazy" - ], - "env": { - "NODE_ENV": "development" - }, - "console": "internalConsole", - "sourceMaps": false, - "outFiles": [] - }, { "name": "Asociar", "type": "node", diff --git a/client/client/src/billing-data/billing-data.js b/client/client/src/billing-data/billing-data.js index a6887039e3..51a411013e 100644 --- a/client/client/src/billing-data/billing-data.js +++ b/client/client/src/billing-data/billing-data.js @@ -43,7 +43,7 @@ export default class Controller { } returnDialog(response) { if (response === 'ACCEPT') { - this.$http.post(`/mailer/manuscript/payment-update/${this.client.id}`).then( + this.$http.post(`/mailer/notification/payment-update/${this.client.id}`).then( () => { this.vnApp.showMessage(this.translate.instant('Notification sent!')); } diff --git a/services/auth/server/boot/routes.js b/services/auth/server/boot/routes.js index 9aa76896b3..3812a50fb5 100644 --- a/services/auth/server/boot/routes.js +++ b/services/auth/server/boot/routes.js @@ -88,29 +88,4 @@ module.exports = function(app) { User.logout(req.accessToken.id, () => res.redirect('/')); }); - - app.get('/validateToken', function(req, res) { - let token = req.headers.authorization; - - validateToken(token, function(isValid) { - if (isValid) { - res.status(200); - } else { - res.status(401).json({ - message: 'Invalid token' - }); - } - }); - }); - - function validateToken(tokenId, cb) { - app.models.AccessToken.findById(tokenId, function(err, token) { - if (token) { - token.validate(function (err, isValid) { - cb(isValid === true, token); - }); - } else - cb(false); - }); - } }; diff --git a/services/client/common/methods/client/activate.js b/services/client/common/methods/client/activate.js index 9d78d0d63b..279baffbff 100644 --- a/services/client/common/methods/client/activate.js +++ b/services/client/common/methods/client/activate.js @@ -49,7 +49,7 @@ module.exports = function(Client) { method: 'POST', headers: { 'content-type': 'application/json', - 'authorization': ctx.req.headers.authorization + 'Authorization': ctx.req.headers.authorization }, json: {} }; diff --git a/services/mailer/application/auth.js b/services/mailer/application/auth.js index ee8f114e13..81f423e15d 100644 --- a/services/mailer/application/auth.js +++ b/services/mailer/application/auth.js @@ -1,4 +1,5 @@ var database = require('./database.js'); +let config = require('./config.js'); module.exports = { @@ -31,7 +32,23 @@ module.exports = { if (this.isTokenExpired(token.created, token.ttl)) return this.response.status(401).send({message: 'Token expired'}); - this.request.userId = token.userId; + // Set proxy host + let host = this.request.headers.host.split(':')[0]; + let proxy; + + if (host == '127.0.0.1') + proxy = config.proxy.localhost; + else if(process.env.NODE_ENV == 'production') + proxy = config.proxy.salix; + else if(process.env.NODE_ENV == 'development') + proxy = config.proxy.testSalix; + + this.request.proxyHost = `http://${proxy.host}:${proxy.port}`; + this.request.user = { + id: token.userId, + token: this.getToken() + } + this.next(); }); }, diff --git a/services/mailer/application/config.js b/services/mailer/application/config.js new file mode 100644 index 0000000000..1b78e36cec --- /dev/null +++ b/services/mailer/application/config.js @@ -0,0 +1,18 @@ +var path = require('path'); +var fs = require('fs'); +var config = {}; + +let devConfigPath = path.join(__dirname, '/config/datasources.development.json'); +let configPath = path.join(__dirname, '/config/datasources.json'); + +try { + config = Object.assign(require(configPath), require(devConfigPath)); +} catch (e) { + if (e.code == 'MODULE_NOT_FOUND') + return require(configPath); +} + +config.proxy = require('../../nginx/config.json'); +config.package = require('../package.json'); + +module.exports = config; \ No newline at end of file diff --git a/services/mailer/application/config/app.json b/services/mailer/application/config/app.json deleted file mode 100644 index c9ec4174b9..0000000000 --- a/services/mailer/application/config/app.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "MailServer", - "version": "1.0.0", - "port": 3003, - "debug": false, - "defaultLanguage": "es", - "senderMail": "noreply@localhost", - "senderName": "" -} \ No newline at end of file diff --git a/services/mailer/application/config/app.test.json b/services/mailer/application/config/app.test.json deleted file mode 100644 index c9ec4174b9..0000000000 --- a/services/mailer/application/config/app.test.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "MailServer", - "version": "1.0.0", - "port": 3003, - "debug": false, - "defaultLanguage": "es", - "senderMail": "noreply@localhost", - "senderName": "" -} \ No newline at end of file diff --git a/services/mailer/application/config/datasources.json b/services/mailer/application/config/datasources.json new file mode 100644 index 0000000000..69943439d3 --- /dev/null +++ b/services/mailer/application/config/datasources.json @@ -0,0 +1,29 @@ +{ + "app": { + "port": 3003, + "debug": false, + "defaultLanguage": "es", + "senderMail": "noreply@localhost", + "senderName": "" + }, + "mysql": { + "host": "localhost", + "port": 3306, + "user": "reports", + "password": "", + "database": "" + }, + "smtp": { + "host": "localhost", + "port": 465, + "secure": true, + "auth": { + "user": "noreply", + "pass": "" + }, + "tls": { + "rejectUnauthorized": false + }, + "pool": true + } +} \ No newline at end of file diff --git a/services/mailer/application/config/datasources.test.json b/services/mailer/application/config/datasources.test.json new file mode 100644 index 0000000000..69943439d3 --- /dev/null +++ b/services/mailer/application/config/datasources.test.json @@ -0,0 +1,29 @@ +{ + "app": { + "port": 3003, + "debug": false, + "defaultLanguage": "es", + "senderMail": "noreply@localhost", + "senderName": "" + }, + "mysql": { + "host": "localhost", + "port": 3306, + "user": "reports", + "password": "", + "database": "" + }, + "smtp": { + "host": "localhost", + "port": 465, + "secure": true, + "auth": { + "user": "noreply", + "pass": "" + }, + "tls": { + "rejectUnauthorized": false + }, + "pool": true + } +} \ No newline at end of file diff --git a/services/mailer/application/config/mysql.json b/services/mailer/application/config/mysql.json deleted file mode 100644 index ddfd88bede..0000000000 --- a/services/mailer/application/config/mysql.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "host": "localhost", - "port": 3306, - "user": "reports", - "password": "", - "database": "" -} \ No newline at end of file diff --git a/services/mailer/application/config/mysql.test.json b/services/mailer/application/config/mysql.test.json deleted file mode 100644 index 5b00829b39..0000000000 --- a/services/mailer/application/config/mysql.test.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "host": "localhost", - "port": 3306, - "user": "root", - "password": "", - "database": "vn" -} \ No newline at end of file diff --git a/services/mailer/application/config/smtp.json b/services/mailer/application/config/smtp.json deleted file mode 100644 index 1ffa810243..0000000000 --- a/services/mailer/application/config/smtp.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "host": "localhost", - "port": 465, - "secure": true, - "auth": { - "user": "noreply", - "pass": "" - }, - "tls": { - "rejectUnauthorized": false - }, - "pool": true -} \ No newline at end of file diff --git a/services/mailer/application/database.js b/services/mailer/application/database.js index 53ce363f89..4fe4a0ba95 100644 --- a/services/mailer/application/database.js +++ b/services/mailer/application/database.js @@ -1,5 +1,5 @@ var mysql = require('mysql'); -let settings = require('./settings.js'); +let config = require('./config.js'); module.exports = { /** @@ -11,12 +11,12 @@ module.exports = { * Start database pool */ init: function() { - this.pool = mysql.createPool(settings.mysql()); + this.pool = mysql.createPool(config.mysql); this.pool.getConnection(function(error, connection) { if (error) { throw new Error('Can\'t connect to database: ' + error.code); - } else if (settings.app().debug) { + } else if (config.app.debug) { console.log('Database connection stablished'); } }); @@ -27,7 +27,7 @@ module.exports = { */ testEmail: function() { this.pool.query('SELECT fakeEmail as email FROM vn.config', function(error, qryRs) { - settings.testEmail = qryRs[0].email; + config.smtp.testEmail = qryRs[0].email; }); } }; diff --git a/services/mailer/application/locale.js b/services/mailer/application/locale.js index 496d48bb87..f9737fea32 100644 --- a/services/mailer/application/locale.js +++ b/services/mailer/application/locale.js @@ -1,5 +1,5 @@ var fs = require('fs'); -var settings = require('./settings.js'); +var config = require('./config.js'); var path = require('path'); module.exports = { @@ -11,7 +11,7 @@ module.exports = { */ load: function(template, countryCode, cb) { var localeFile = path.join(__dirname, 'template', `${template}`, 'locale', `${countryCode}.json`); - var defaultLocaleFile = path.join(__dirname, 'template', `${template}`, 'locale', `${settings.app().defaultLanguage}.json`); + var defaultLocaleFile = path.join(__dirname, 'template', `${template}`, 'locale', `${config.app.defaultLanguage}.json`); fs.stat(localeFile, (error, stats) => { if (error) { diff --git a/services/mailer/application/mail.js b/services/mailer/application/mail.js index 4807157a9f..3980e51286 100644 --- a/services/mailer/application/mail.js +++ b/services/mailer/application/mail.js @@ -1,5 +1,5 @@ var nodemailer = require('nodemailer'); -var settings = require('./settings.js'); +var config = require('./config.js'); var template = require('./template.js'); var database = require('./database.js'); @@ -9,15 +9,15 @@ var database = require('./database.js'); module.exports = { transporter: null, /** - * Load mail settings. + * Load mail config. */ init: function() { - this.transporter = nodemailer.createTransport(settings.smtp()); + this.transporter = nodemailer.createTransport(config.smtp); this.transporter.verify(function(error, success) { if (error) { throw new Error(error); - } else if (settings.app().debug) { + } else if (config.app.debug) { console.log('SMTP connection stablished'); } }); @@ -34,15 +34,15 @@ module.exports = { */ send: function(recipient, subject, body, attachments, params, cb) { let mailOptions = { - from: '"' + settings.app().senderName + '" <' + settings.app().senderMail + '>', + from: '"' + config.app.senderName + '" <' + config.app.senderMail + '>', to: recipient, subject: subject, html: body, attachments }; - if (settings.app().debug) { - mailOptions.to = settings.testEmail; + if (config.app.debug) { + mailOptions.to = config.smtp.testEmail; } this.transporter.sendMail(mailOptions, (error, info) => { @@ -53,7 +53,7 @@ module.exports = { if (error) return cb(new Error('Email not sent: ' + error)); - if (settings.app().debug) + if (config.app.debug) console.log('Mail sent ' + info.messageId + ' [' + info.response + ']'); cb(); @@ -70,10 +70,16 @@ module.exports = { * @param {Object} cb - Callback */ sendWithTemplate: function(tplName, params, cb) { - template.get(tplName, params, false, (error, result) => { + template.get(tplName, params, (error, result) => { if (error) return cb(error); + // Custom attachments + if (params.attachments) + params.attachments.forEach(function(attachment) { + result.attachments.push(attachment); + }); + this.send(result.recipient, result.subject, result.body, result.attachments, params, error => { if (error) return cb(error); @@ -96,10 +102,10 @@ module.exports = { log: function(senderId, recipientId, sender, subject, body, plainTextBody, status) { let qry = `INSERT INTO mail(senderFk, recipientFk, sender, replyTo, subject, body, plainTextBody, sent, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`; - let qryParams = [senderId, recipientId, sender, settings.app().senderMail, subject, body, plainTextBody, 1, status]; + let qryParams = [senderId, recipientId, sender, config.app.senderMail, subject, body, plainTextBody, 1, status]; database.pool.query(qry, qryParams, function(error, result) { - if (settings.app().debug && error) + if (config.app.debug && error) console.log('Mail log: ' + error); }); } diff --git a/services/mailer/application/route/manuscript.js b/services/mailer/application/route/manuscript.js deleted file mode 100644 index b8c54d43b2..0000000000 --- a/services/mailer/application/route/manuscript.js +++ /dev/null @@ -1,85 +0,0 @@ -var express = require('express'); -var router = new express.Router(); -var mail = require('../mail.js'); -var template = require('../template.js'); -var httpRequest = require('request'); - -// Payment method changes -router.post('/payment-update/:clientId', function(request, response, next) { - mail.sendWithTemplate('payment-update', {recipient: request.params.clientId}, error => { - if (error) - return response.status(400).json({message: error.message}); - - return response.json(); - }); -}); - -// Printer setup -router.post('/printer-setup/:clientId', function(request, response, next) { - mail.sendWithTemplate('printer-setup', {recipient: request.params.clientId}, error => { - if (error) - return response.status(400).json({message: error.message}); - - return response.json(); - }); -}); - -// Printer setup preview -router.get('/printer-setup/:clientId', function(request, response, next) { - template.get('printer-setup', {recipient: request.params.clientId}, true, (error, result) => { - if (error) - return response.status(400).json({message: error.message}); - - response.send(result.body); - }); -}); - -// Client welcome -router.post('/client-welcome/:clientId', function(request, response, next) { - mail.sendWithTemplate('client-welcome', {recipient: request.params.clientId}, error => { - if (error) - return response.status(400).json({message: error.message}); - - return response.json(); - }); -}); - -// Client welcome preview -router.get('/client-welcome/:clientId', function(request, response, next) { - template.get('client-welcome', {recipient: request.params.clientId}, true, (error, result) => { - if (error) - return response.status(400).json({message: error.message}); - - response.send(result.body); - }); -}); - -// Sepa core -/* router.post('/sepa-core/:clientId', function(request, response, next) { - var options = { - url: 'http://localhost:3008/manuscript/sepa-core/7422', - method: 'POST', - headers: { - 'authorization': request.headers.authorization - } - } - - let httpStream = httpRequest(options, function(error, httpResponse, body) { - if (error) - return response.status(400).json({message: httpResponse.message}); - }); - - if (httpStream) - mail.send('joan@verdnatura.es', 'Correu de prova', 'test message', [{filename: 'test.pdf', content: httpStream}], function(error, result) { - if (error) - return response.status(400).json({message: error.message}); - }); - /* mail.sendWithTemplate('sepa-core', {recipient: request.params.clientId}, error => { - if (error) - return response.status(400).json({message: error.message}); - - return response.json(); - }); -}); */ - -module.exports = router; diff --git a/services/mailer/application/route/notification.js b/services/mailer/application/route/notification.js index d33f6f07bc..bd7a81f112 100644 --- a/services/mailer/application/route/notification.js +++ b/services/mailer/application/route/notification.js @@ -1,8 +1,92 @@ var express = require('express'); var router = new express.Router(); +var config = require('../config.js'); var mail = require('../mail.js'); -var database = require('../database.js'); -var settings = require('../settings.js'); +var template = require('../template.js'); +var httpRequest = require('request'); + + +// Printer setup +router.post('/printer-setup/:clientId', function(request, response) { + mail.sendWithTemplate('printer-setup', {clientId: request.params.clientId}, error => { + if (error) + return response.status(400).json({message: error.message}); + + return response.json(); + }); +}); + +// Printer setup preview +router.get('/printer-setup/:clientId', function(request, response) { + template.get('printer-setup', {clientId: request.params.clientId, isPreview: true}, (error, result) => { + if (error) + return response.status(400).json({message: error.message}); + + response.send(result.body); + }); +}); + +// Client welcome +router.post('/client-welcome/:clientId', function(request, response) { + mail.sendWithTemplate('client-welcome', {clientId: request.params.clientId}, error => { + if (error) + return response.status(400).json({message: error.message}); + + return response.json(); + }); +}); + +// Client welcome preview +router.get('/client-welcome/:clientId', function(request, response) { + template.get('client-welcome', {clientId: request.params.clientId, isPreview: true}, (error, result) => { + if (error) + return response.status(400).json({message: error.message}); + + response.send(result.body); + }); +}); + +// Client SEPA CORE +router.post('/sepa-core/:clientId', function(request, response) { + let path = `${request.proxyHost}/print/manuscript/sepa-core/${request.params.clientId}`; + let options = { + url: path, + method: 'GET', + headers: { + 'Authorization': request.headers.authorization + } + } + + let httpStream = httpRequest(options, function(error, httpResponse, body) { + if (error || httpResponse.statusCode != 200) + return response.status(400).json({message: error.message}); + }); + + if (httpStream) + mail.sendWithTemplate('sepa-core', { + clientId: request.params.clientId, + attachments: [{filename: 'sepa-core.pdf', content: httpStream}] + }, error => { + if (error) + return response.status(400).json({message: error.message}); + + return response.json(); + }); +}); + +// Client SEPA CORE preview +router.get('/sepa-core/:clientId', function(request, response) { + template.get('sepa-core', { + clientId: request.params.clientId, + token: request.user.token, + isPreview: true + }, (error, result) => { + if (error) + return response.status(400).json({message: error.message}); + + response.send(result.body); + }); +}); // Single user notification /* router.post('/:recipient/noticeUserSend', function(request, response) { @@ -64,6 +148,16 @@ var settings = require('../settings.js'); }); }); */ +// Payment method changes +router.post('/payment-update/:clientId', function(request, response) { + mail.sendWithTemplate('payment-update', {clientId: request.params.clientId}, error => { + if (error) + return response.status(400).json({message: error.message}); + + return response.json(); + }); +}); + // Send notification to alias creditInsurance on client deactivate router.post('/client-deactivate/:clientId', function(request, response) { var params = { diff --git a/services/mailer/application/router.js b/services/mailer/application/router.js index b51b8a195e..ed5794bbe6 100644 --- a/services/mailer/application/router.js +++ b/services/mailer/application/router.js @@ -1,15 +1,11 @@ var express = require('express'); var router = new express.Router(); -var settings = require('./settings.js'); // Mailer default page router.get('/', function(request, response) { response.json({}); }); -// Manuscripts -router.use('/manuscript', require('./route/manuscript.js')); - // Notifications router.use('/notification', require('./route/notification.js')); diff --git a/services/mailer/application/settings.js b/services/mailer/application/settings.js deleted file mode 100644 index 3858cf7193..0000000000 --- a/services/mailer/application/settings.js +++ /dev/null @@ -1,45 +0,0 @@ -var path = require('path'); -/** - * Módulo de configuración - */ -module.exports = { - /** - * Obtiene la configuración en función del entorno en el que se está - * ejecutando la aplicación. - * @param {String} name Nombre del fichero - * @return {Object} Objeto de configuración - */ - getConfig: function(name) { - let env = process.env.NODE_ENV; - - if (!env) - env = 'development'; - - return require(path.join(__dirname, 'config', `${name}.${env}.json`)); - }, - /** - * Configuración de la aplicación - * @return {Object} Objeto de configuración app - */ - app: function() { - return this.getConfig('app'); - }, - /** - * Configuración de smtp - * @return {Object} Objeto de configuración smtp - */ - smtp: function() { - return this.getConfig('smtp'); - }, - /** - * Configuración de mysql - * @return {Object} Objeto de configuración MySQL - */ - mysql: function() { - return this.getConfig('mysql'); - }, - - testEmail: function() { - return this.getConfig('app').testEmail; - } -}; diff --git a/services/mailer/application/template.js b/services/mailer/application/template.js index 382b7202d1..d2c13f2226 100644 --- a/services/mailer/application/template.js +++ b/services/mailer/application/template.js @@ -1,8 +1,8 @@ var fs = require('fs'); var mustache = require('mustache'); var locale = require('./locale.js'); -var path = require('path'); var inlineCss = require('inline-css'); +var path = require('path'); module.exports = { /** @@ -12,10 +12,10 @@ module.exports = { * @param {Object} params - Params * @param {Object} cb - Callback */ - get: function(template, params, isPreview, cb) { + get: function(template, params, cb) { var templatePath = path.join(__dirname, 'template', `${template}`, `index.html`); var classPath = path.join(__dirname, 'template', `${template}`, `${template}.js`); - var stylePath = path.join(__dirname, '../static', 'css', 'style.css'); + var stylePath = path.join(__dirname, 'template', `${template}`, 'static', 'css', 'style.css'); fs.stat(templatePath, (error, stat) => { if (error) @@ -24,14 +24,26 @@ module.exports = { let TemplateClass = require(classPath); let instance = new TemplateClass(); - let getRenderedStyles = body => { + let getRenderedStyles = (error, body) => { + if (error) + return cb(error); + this.renderStyles(stylePath, body, (error, body) => { + if (error) + return cb(error); + + // Check if has a subject param params.subject = params.subject || instance.subject; - if (params.subject == undefined) - params.subject = body.match(new RegExp('
{{_.dear}},
-{{_.bodyDescription}}
- -- Sus datos para poder comprar en la web de verdnatura (https://www.verdnatura.es) - o en nuestras aplicaciones para iOS (https://goo.gl/3hC2mG) y Android (https://goo.gl/8obvLc), son: -
- +{{_.dear}}
+{{{_.bodyDescription}}}
-
Para realizar un pedido en nuestra web, debe configurarlo indicando:
- +{{_.sectionHowToBuyDescription}}
En nuestra web y aplicaciones puedes visualizar el stock disponible de flor cortada, verdes, plantas, complementos y artificial. - Tenga en cuenta que dicho stock puede variar en función de la fecha seleccionada al configurar el pedido. Es importante CONFIRMAR los pedidos para que la mercancía quede reservada.
- -El reparto se realiza de lunes a sábado según la zona en la que se encuentre. Por regla general, los pedidos que se entregan por agencia, deben estar confirmados y pagados antes de las 17h - del día en que se preparan (el día anterior a recibirlos), aunque esto puede variar si el pedido se envía a través de nuestro reparto y según la zona.
- -Las formas de pago admitidas en Verdnatura son
- +{{_.sectionHowToBuyStock}}
+{{_.sectionHowToBuyDelivery}}
+ +{{_.sectionHowToPayDescription}}
Verdnatura vende EXCLUSIVAMENTE a profesionales, por lo que debe remitirnos el Modelo 036 ó 037, - para comprobar que está dado/a de alta en el epígrafe correspondiente al comercio de flores.
- -Verdnatura aceptará las reclamaciones que se realicen dentro de los dos días naturales - siguientes a la recepción del pedido (incluyendo el mismo día de la recepción). Pasado este plazo no se aceptará ninguna reclamación.
- -Cualquier duda que le surja, no dude en consultarla, ¡estamos para atenderle!
+{{_.sectionToConsiderDescription}}
+{{_.sectionClaimsPolicyDescription}}
+{{{_.doubtsText}}}
{{{salesPersonName}}} {{{salesPersonPhone}}} {{{salesPersonEmail}}}
- -{{_.fiscalAddress}}
-{{_.privacy}}
-{{_.privacyLaw}}
-{{_.fiscalAddress}}
+{{_.privacy}}
+{{_.privacyLaw}}
+