From fd095d34f67cfb1478507fabb6cd72dfa3c070ac Mon Sep 17 00:00:00 2001 From: DanielRamirezGe Date: Sun, 20 Sep 2020 10:39:36 -0500 Subject: [PATCH] Se agrego el registro --- routes/getEscuelas.js | 4 +- routes/index.js | 2 +- routes/registro.js | 160 ++++++++++++++++++++++++++++++++++++++++++ sql/database.sql | 6 +- 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 routes/registro.js diff --git a/routes/getEscuelas.js b/routes/getEscuelas.js index da61a17..e9ddf54 100644 --- a/routes/getEscuelas.js +++ b/routes/getEscuelas.js @@ -17,9 +17,9 @@ app.get('/getEscuelas', async(req, res) => { try { let cad = `select * from universidad;`; let uni = await query(cad); - cad = "select * from evento where tipoEvento = 'Estudios de practica'"; + cad = "select * from evento where tipoEvento = 'Estudios de practica' and cupo > 0"; let estudios = await query(cad); - cad = "select * from evento where tipoEvento = 'Recorridos'"; + cad = "select * from evento where tipoEvento = 'Recorridos' and cupo > 0"; let recorridos = await query(cad); res.status(200).json({ error: false, msj: "exito", universidad: uni, estudios: estudios, recorridos: recorridos }); return; diff --git a/routes/index.js b/routes/index.js index 7ff0c4e..0a142eb 100644 --- a/routes/index.js +++ b/routes/index.js @@ -5,6 +5,6 @@ app.use(require('./loginPersona')); app.use(require('./loginAdmin')); app.use(require('./enviarPassword')); app.use(require('./getEscuelas')); - +app.use(require('./registro')); module.exports = app; \ No newline at end of file diff --git a/routes/registro.js b/routes/registro.js new file mode 100644 index 0000000..6684ec5 --- /dev/null +++ b/routes/registro.js @@ -0,0 +1,160 @@ +const connection = require('../conf/connection.js'); +const express = require('express'); +const nodemailer = require("nodemailer"); + +var randomstring = require("randomstring"); +const bcrypt = require('bcrypt'); + +var app = express(); + + +function query(consulta) { + return new Promise(function(resolve, reject) { + connection.query(`${consulta}`, (error, results, files) => { + if (error) reject(error); + resolve(results); + }); + }) +} + +async function sendEmail( destination, costo ) { + + // Generate test SMTP service account from ethereal.email + // Only needed if you don't have a real mail account for testing + let testAccount = await nodemailer.createTestAccount(); + + // create reusable transporter object using the default SMTP transport + let transporter = nodemailer.createTransport({ + service: "Gmail", + port: 587, + secure: false, // true for 465, false for other ports + auth: { + user: 'asinea103@fa.unam.mx', // generated ethereal user + pass: 'As1n34103++' + }, + }); + + // send mail with defined transport object + let info = await transporter.sendMail({ + from: '"ASINEA 2020" ', // sender address + to: `"${destination}"`, // list of receivers + subject: "Inscripción exitosa ASINEA 2020", // Subject line + text: ` + Felicidades tu registro a ASINEA 2020 fue exitoso. Los pasos a seguir son: + 1.- Se generara tu ficha de deposito con el monto de $${costo}.00 la cual se te hara llegar en los proximos dias por este medio + 2.- Tienes dos dias habiles para realizar el pago. + 3.- Entra al portal de ASINEA para subir el recibo de tu pago, para que pueda ser validado. + 4.- Al ser comprobado tu pago recibiras tu comprabante de inscripción + + Nota: Recuerda que algunos eventos tienen un maximo de asistentes por lo que no se asegura tu lugar si no realizas el pago. + `, // plain text body + + }); + + console.log("Message sent: %s", info.messageId); + // Message sent: + + // Preview only available when sending through an Ethereal account + console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); + // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou... +} + + +function depurado(cadena) { + if (cadena != null) { + cadena = cadena.replace(/'/g, "''"); + cadena = cadena.replace(/;/g, "m"); + return cadena; + } else + return null; +} + +app.post('/registro', async(req, res) => { + + try{ + + for(let i in req.body) + { + console.log(`${i} : ${req.body[i]}`) + } + + + + let data= req.body; + + let correo = depurado(data.correo); + + let sqlString = `SELECT * FROM persona where correo = '${data.correo}'; `; + + let sqlAnswer = await query(sqlString); + + if (sqlAnswer.length != 0) { + + console.log( "El correo ya fue registrado anteriormente"); + res.status(200).json({ error: false, msj: "El correo del lider ya fue registrado anteriormente" }); + return; + } + + let nombre = depurado(data.nombre) + let apellidoPaterno = depurado(data.apellidoPaterno); + let apellidoMaterno = depurado(data.apellidoMaterno); + let curp = depurado(data.curp); + let telefono = depurado(data.telefono); + let institucion = depurado(data.institucion); + let asignaturaImpartida = depurado(data.asignaturaImpartida); + let codigoGrupal = depurado(data.codigoGrupal); + + let password = await bcrypt.hash(data.password, 10); + + + + + // `idPersona` `nombre` `apellidoPaternp` `apellidoMaterno` `curp` `edad` `genero` `telefono` `correo` `password` `comunidad` `institucion` `situacionAcademica` `nivel` `gradoAcademico` `asignatura` `participacion` `codigoGrupal` `totalPagar` `requiereFactura` `envioComprobante` `referenciaEnviada` `validarPago` `rechazado` + + sqlString = `INSERT INTO persona VALUES (null, '${nombre}', '${apellidoPaterno}', '${apellidoMaterno}', '${curp}', ${data.edad}, '${data.genero}', '${telefono}', '${data.correo}', '${password}', '${data.comunidad}', '${institucion}', '${data.situacionAcademica}', '${data.nivel}', '${data.gradoAcademico}', '${asignaturaImpartida}', '${data.tipoParticipacion}', '${codigoGrupal}', '${data.pago}', false , false, false, false, false )`; + + await query(sqlString); + + sqlString = `SELECT LAST_INSERT_ID() as idPersona;`; + let idPersona = await query(sqlString); + + console.log('este es el idPersona',idPersona); + + idPersona = idPersona[0]; + + idPersona = idPersona.idPersona; + console.log(`Este es el ID persona : ${idPersona}`) + + for(let i=0; i < data.eventos.length ; i++){ + let idEvento = data.eventos[i]; + + sqlString = `INSERT INTO asistencia VALUES (null, ${idPersona}, ${idEvento} )`; + await query ( sqlString ); + + sqlString = `UPDATE evento SET cupo = cupo-1 WHERE idEvento=${idEvento};`; + await query( sqlString ); + } + + try{ + await sendEmail( correo, data.pago ); + } + catch(error){ + res.status(200).json({error: false, msj: 'No se pudo enviar el correo de confirmación pero tu inscripcion esta registrada, nos comunicaremos con tigo personalmente', registrado: false }); + } + + res.status(200).json({error: false, msj: 'Tu registro fue exitoso ¡Gracias!', registrado: true}); + + + + + } + catch(error){ + console.log(error); + res.status(400).json({error: true, msj: `Sucedio un error ${error}`}); + } + + +}); + + +module.exports = app \ No newline at end of file diff --git a/sql/database.sql b/sql/database.sql index 8a43700..27fc6c0 100644 --- a/sql/database.sql +++ b/sql/database.sql @@ -19,6 +19,9 @@ CREATE TABLE `asistencia` ( `idEvento` integer ); + +INSERT INTO admin VALUES (null, 'Administrador' , '$2b$10$tUzaWli0CUzGnoZ1saxSF.RRtxDcdgRP0tQOQdmqbVG1fiz4jCMLW'); + CREATE TABLE `persona` ( `idPersona` integer PRIMARY KEY NOT NULL AUTO_INCREMENT, `nombre` varchar(100), @@ -42,7 +45,8 @@ CREATE TABLE `persona` ( `requiereFactura` boolean, `envioComprobante` boolean, `referenciaEnviada` boolean, - `validarPago` boolean + `validarPago` boolean, + `rechazado` boolean ); CREATE TABLE `evento` (