From 29b6b453941a512ccfcef51302cb501c2ccaf082 Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 22:53:59 -0400 Subject: [PATCH 1/9] Carpeta para archivos csv --- public/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 public/.gitignore diff --git a/public/.gitignore b/public/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/public/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file From 92549496d4be593469fbcf3b5c90b4257df5cdb2 Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 22:55:02 -0400 Subject: [PATCH 2/9] .env.example --- .env.example | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..b8b82fb --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +DB_HOST= +DB_PORT= +DB_DATABASE= +DB_USERNAME= +DB_PASSWORD= + +MAIL_FROM= +MAIL_PASS= +MAIL_CC= +MAIL_TOSENT= \ No newline at end of file From 443ab410772fe2df8f667bbd7e4ad312fb0e69db Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 22:55:28 -0400 Subject: [PATCH 3/9] =?UTF-8?q?Conexi=C3=B3n=20a=20la=20base=20de=20datos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/database.js diff --git a/src/database.js b/src/database.js new file mode 100644 index 0000000..f331c60 --- /dev/null +++ b/src/database.js @@ -0,0 +1,9 @@ +const { Pool } = require('pg') + +module.exports = new Pool({ + host: process.env.DB_HOST, + port: process.env.DB_PORT, + database: process.env.DB_DATABASE, + user: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD +}) \ No newline at end of file From c38d311b94a0aa81d9b3379bef11fccd7f1bdcfe Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 22:57:31 -0400 Subject: [PATCH 4/9] Funcion que consulta datos a BD --- src/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/index.js diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..af7c2cc --- /dev/null +++ b/src/index.js @@ -0,0 +1,20 @@ +const dotenv = require('dotenv').config() + +if(dotenv.error) throw dotenv.error + +const c = con => console.log(con) + +const pool = require('./database') + +// Obtiene usuarios de BBDD para datos del archivo +const getUsers = async () => { + c('Consultando datos...') + const { rows } = await pool.query('SELECT * FROM auth.users') + c(`Consulta exitosa con ${rows.length} filas`) + pool.end() + return rows +} + +(async () => { + const users = await getUsers() +})() From b9f2419cf0ffca0485314e45609b3e1a8d807b6f Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 22:58:40 -0400 Subject: [PATCH 5/9] =?UTF-8?q?Funci=C3=B3n=20que=20crea=20archivo=20csv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/file.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/file.js diff --git a/src/file.js b/src/file.js new file mode 100644 index 0000000..b332bbd --- /dev/null +++ b/src/file.js @@ -0,0 +1,22 @@ +const path = require('path') +const fs = require('fs') +const csv = require('fast-csv') + +// Archivo a enviar +const creaArchivo = async (c,data) => { + const dirFile = path.resolve(__dirname,'../public'), + nameFile = 'usuarios_' + new Date().toLocaleDateString() + '.csv' + pathFile = dirFile + '/' + nameFile + let file + c('Creando archivo...') + fs.createWriteStream(pathFile).close() + c('Archivo creado: ' + nameFile) + c('Escribiendo datos...') + file = await csv.writeToPath(pathFile, data, { headers: true }) + file.close() + c('Archivo guardado exitosamente') + c('Ubicación: ' + pathFile) + return { pathFile, nameFile } +} + +module.exports = creaArchivo \ No newline at end of file From e270554e14eafe641a02d7b5690c442384204ccc Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 23:02:34 -0400 Subject: [PATCH 6/9] Funcion para enviar correo --- src/mail.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/mail.js diff --git a/src/mail.js b/src/mail.js new file mode 100644 index 0000000..d1f156a --- /dev/null +++ b/src/mail.js @@ -0,0 +1,68 @@ +const mailer = require('nodemailer') +const file = require('./file') + +const subject = 'Listado de usuarios hasta la fecha ' + new Date().toLocaleDateString() + +const html = 'Mensaje...'; + +const confTransp = { + host: 'smtp.gmail.com', + port: 465, + service: 'gmail', + secure: true, + auth: { + user: process.env.MAIL_FROM, + pass: process.env.MAIL_PASS + } +} + +let options = { + from: process.env.MAIL_FROM, + cc: process.env.MAIL_CC, + to: process.env.MAIL_TOSENT, + subject, + html, + attachments: [ + { + path: '', + filename: '', + } + ] +} + +// Envia email +const enviaEmail = async (c,data) => { + + const transport = mailer.createTransport(confTransp) + + c('Validando datos...') + + if(!data.length){ + options.attachments = null + options.cc = null + options.to = options.from + options.subject = 'Error en consulta de usuarios' + options.html = `Es probable que la lista esté vacía o error al ejecutar la consulta` + c(`${options.subject}. ${options.html}`) + }else{ + // Crea archivo + const { pathFile, nameFile } = await file(c,data) + options.attachments[0].path = pathFile + options.attachments[0].filename = nameFile + } + + c('Enviando correo electronico...') + + const resp = await transport.sendMail(options,(err,info)=>{ + if(err){ + c('Error al enviar correo:') + return console.error(err.message) + } + c('Archivo enviado existosamente') + c(info.messageId) + }) + + return resp +} + +module.exports = enviaEmail \ No newline at end of file From dedfdcee7dd5702564cc2afb1a760bfd65212be1 Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 23:07:50 -0400 Subject: [PATCH 7/9] Agregada funcion para enviar correo --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index af7c2cc..bb6274f 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ if(dotenv.error) throw dotenv.error const c = con => console.log(con) const pool = require('./database') +const enviaEmail = require('./mail') // Obtiene usuarios de BBDD para datos del archivo const getUsers = async () => { @@ -17,4 +18,5 @@ const getUsers = async () => { (async () => { const users = await getUsers() + enviaEmail(c,users) })() From 5fd56e6dad8a7b9ad4e0d42e3b9aeba494ad6ac3 Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 23:10:02 -0400 Subject: [PATCH 8/9] Readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e6d023f..303a392 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ Scrip de NodeJs para enviar un archivo .CSV por correo electrónico _Antes que continúes debes tener instalado:_ -* [NodeJS](https://nodejs.org) - _utilicé v12.6.2_ -* [npm](https://www.npmjs.com/) - _utilicé v6.14.4_ +* [NodeJS](https://nodejs.org) - _v12.6.2 o superior_ +* [npm](https://www.npmjs.com/) - _v6.14.4 o superior_ ## Instalación 🔧 Sigue estos pasos: From e9b43d11499b4738a5d9509a6198f5a3862938bc Mon Sep 17 00:00:00 2001 From: jreyesdev Date: Sat, 14 Aug 2021 23:12:06 -0400 Subject: [PATCH 9/9] Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 303a392..173a8a5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Enviar CSV por email -Scrip de NodeJs para enviar un archivo .CSV por correo electrónico +Script de NodeJs para enviar un archivo .CSV por correo electrónico ### Pre-requisitos 📋