var express = require('express'); var router = new express.Router(); var mail = require('../mail.js'); var database = require('../database.js'); var template = require('../template.js'); router.get('/:userId/notice', function(request, response) { database.pool.query('SELECT `e-mail` AS email, LOWER(p.Codigo) AS countryCode FROM Clientes AS c JOIN Paises AS p ON p.id = c.Id_Pais WHERE Id_Cliente = ?', [request.params.userId], function(error, qryRs) { if (qryRs.length == 0) return response.json({data: {message: 'Client not found'}}); template.getTemplate('notice', qryRs[0].countryCode, {userId: request.params.userId}, function(tplRs, error) { if (error) return response.json({data: {message: error}}); mail.send(qryRs[0].email, tplRs.subject, tplRs.body, tplRs.attachments, (mailrs, error) => { if (error) return response.json({data: {message: error}}); return response.json({data: {message: 'Mail sent'}}); }); }); }); response.send(request.params.userid); }); module.exports = router;