Files
hackApi/routes/registro.js
T

238 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-07-05 19:21:09 -05:00
const connection = require('../conf/connection.js');
const express = require('express');
2020-08-02 19:21:23 -05:00
const nodemailer = require("nodemailer");
async function main() {
// 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({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
// user: 'hackatonaws@unam.mx',
// pass: 'H4ck4#20'
user: 'hackaton-test@pcpuma.acatlan.unam.mx', // generated ethereal user
pass: 'H4cka-T3st', // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo@example.com>', // sender address
to: "bar@example.com, baz@example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
2020-07-05 19:21:09 -05:00
var app = express()
// ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'georgechido'
function query(consulta) {
return new Promise(function(resolve, reject) {
connection.query(`${consulta}`, (error, results, files) => {
2020-07-05 23:23:45 -05:00
if (error){
reject(error);
}
2020-07-05 19:21:09 -05:00
resolve(results);
2020-07-05 23:23:45 -05:00
console.log("si pasa por aqui");
2020-07-05 19:21:09 -05:00
});
})
}
function depurado(cadena) {
if (cadena != null)
return cadena.replace(/'/g, "''");
else
return null;
}
// {
// desafio:
// campus:
// numeroIntegrantes:
// nombreLider:
// apellidoPaLider:
// apellidoMaLider:
// correoLider:
// carreraLider:
// integrantes: [{nombre: , apellidoPa, apellidoMa, correo}]
// }
app.post('/registro', async(req, res) => {
2020-07-05 23:23:45 -05:00
2020-07-05 19:21:09 -05:00
let body = req.body;
let idEquipo = 1;
try{
2020-07-06 01:00:25 -05:00
let n_correo = depurado( body.correoLider);
2020-07-05 19:21:09 -05:00
let cad = `SELECT * FROM Persona WHERE correo='${n_correo}' AND lider = true;`;
let respuesta = await query(cad);
2020-08-02 19:21:23 -05:00
console.log(respuesta);
2020-07-05 19:21:09 -05:00
if (respuesta.length != 0) {
2020-07-06 01:13:56 -05:00
2020-07-05 23:23:45 -05:00
console.log( "El correo del lider ya fue registrado anteriormente");
2020-07-05 19:21:09 -05:00
res.status(400).json({ error: false, msj: "El correo del lider ya fue registrado anteriormente" });
return;
}
}
catch(error){
2020-07-06 01:13:56 -05:00
2020-07-05 19:21:09 -05:00
let men = `Errror al buscar el correo ${error}`;
console.log(men);
res.status(400).json({ error: true, msj: men });
2020-07-06 01:00:25 -05:00
return;
2020-07-05 19:21:09 -05:00
}
try{
2020-07-05 23:23:45 -05:00
let cad = `INSERT INTO Equipo VALUES (null, '${body.nombreEqui}', '${body.desafio}', '${body.campus}', ${body.numeroIntegrantes});`;
2020-07-05 19:21:09 -05:00
await query(cad);
2020-07-06 01:57:49 -05:00
cad = `SELECT COUNT(*) as num FROM Equipo;`;
2020-07-05 19:21:09 -05:00
let ans = await query(cad);
2020-07-06 01:57:49 -05:00
// console.log(`respuesta: ${ans}`);
// for(key in ans){
// console.log(` ${key} : ${ans[key]}`);
// for(key2 in ans[key]){
// console.log(` ${key2} : ${ans[key][key2]}`);
// }
// }
// console.log(`aqui: ${ans[0].num}`);
idEquipo = ans[0].num;
2020-07-05 19:21:09 -05:00
}
catch(error){
2020-07-06 01:13:56 -05:00
2020-07-05 19:21:09 -05:00
let men = `Error al registar Equipo ${error}`;
console.log(men);
res.status(400).json({ error: true, msj: men });
2020-07-06 01:00:25 -05:00
return;
2020-07-05 19:21:09 -05:00
}
// CREATE TABLE `Persona` (
// `idPersona` integer PRIMARY KEY NOT NULL AUTO_INCREMENT,
// `idEquipo` integer,
// `nombre` varchar(100),
// `apellidoP` varchar(100),
// `apellidoM` varchar(100),
// `correo` varchar(100),
// `carrera` varchar(100),
// `lider` boolean
// );
try{
for( let i=0; i < body.integrantes.length; i++){
let row = body.integrantes[i];
2020-07-06 01:00:25 -05:00
let c = depurado(row.correo);
let cad = `INSERT INTO Persona VALUES ( null, ${idEquipo}, '${row.nombre}', '${row.apellidoPa}', '${row.apellidoMa}', '${c}', null, false );`;
2020-07-05 19:21:09 -05:00
await query(cad);
}
2020-07-06 01:00:25 -05:00
let c = depurado(body.correoLider);
let cad = `INSERT INTO Persona VALUES ( null, ${idEquipo}, '${body.nombreLider}', '${body.apellidoPaLider}', '${body.apellidoMaLider}', '${c}', '${body.carreraLider}', true );`;
2020-07-05 19:21:09 -05:00
await query(cad);
}
catch(error){
2020-07-06 01:13:56 -05:00
2020-07-05 19:21:09 -05:00
let men = `Error al registar Integrantes ${error}`
console.log(men);
res.status(400).json({ error: true, msj: men });
2020-07-06 01:00:25 -05:00
return;
2020-07-05 19:21:09 -05:00
}
2020-07-06 01:00:25 -05:00
try{
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
2020-08-02 19:21:23 -05:00
user: 'hackaton-test@pcpuma.acatlan.unam.mx',
pass: 'H4cka-T3st'
2020-07-06 01:00:25 -05:00
}
});
var mailOptions = {
2020-08-01 00:38:02 -05:00
from: ` hackatonaws@unam.mx`,
2020-07-06 01:00:25 -05:00
to: `${body.correoLider}`,
subject: 'Registro HACKATHON VIRTUAL',
2020-08-02 19:21:23 -05:00
// text: `Tu registro se ha realizado de manera correcta es importante que considere el equipo la asistencia a las siguientes actividades
// Primer Taller: Julio 14
// Segundo Taller: Julio 16
// Tercer Taller: Julio 22
// Cuarto Taller: Julio 28
// Hackathon: Agosto 1-2
2020-07-05 19:21:09 -05:00
2020-08-02 19:21:23 -05:00
// Una vez inscrito, se compartirán más detalles.`
html: `<!doctype html>
<html ⚡4email>
<head>
<meta charset="utf-8">
<style amp4email-boilerplate>body{visibility:hidden}</style>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>
</head>
<body>
<p>Image: <amp-img src="https://cldup.com/P0b1bUmEet.png" width="16" height="16"/></p>
<p>GIF (requires "amp-anim" script in header):<br/>
<amp-anim src="https://cldup.com/D72zpdwI-i.gif" width="500" height="350"/></p>
</body>
</html>`
2020-07-06 01:00:25 -05:00
};
await transporter.sendMail(mailOptions, function(error, info){
2020-08-02 19:21:23 -05:00
// main().catch(error =>{
// console.error
if(error){
2020-07-06 01:00:25 -05:00
let men = `Error al enviar mensaje: ${error}`;
console.log(men);
res.status(400).json({ error: true, msj: men });
2020-07-06 01:13:56 -05:00
2020-07-06 01:00:25 -05:00
return;
2020-07-05 19:21:09 -05:00
2020-08-02 19:21:23 -05:00
}else {
2020-07-05 19:21:09 -05:00
2020-07-06 01:00:25 -05:00
console.log('Email sent: ' + info.response);
res.status(200).json({ error: false, msj: "exito"});
2020-07-06 01:13:56 -05:00
2020-07-06 01:00:25 -05:00
return;
2020-07-05 19:21:09 -05:00
2020-07-06 01:00:25 -05:00
}
});
2020-08-02 19:21:23 -05:00
// );
2020-07-06 01:00:25 -05:00
}
catch(error){
let men = `Error al enviar mensaje: ${error}`;
console.log(men);
res.status(400).json({ error: true, msj: men });
2020-07-06 01:13:56 -05:00
2020-07-06 01:00:25 -05:00
return;
2020-07-05 19:21:09 -05:00
2020-07-06 01:00:25 -05:00
}
2020-07-05 19:21:09 -05:00
});
module.exports = app