90 lines
2.9 KiB
JavaScript
90 lines
2.9 KiB
JavaScript
const connection = require('../conf/connection.js');
|
|
const express = require('express');
|
|
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);
|
|
});
|
|
})
|
|
}
|
|
|
|
|
|
function depurado(cadena) {
|
|
if (cadena != null)
|
|
return cadena.replace(/'/g, "''");
|
|
else
|
|
return null;
|
|
}
|
|
app.post('/registro', async(req, res) => {
|
|
let body = req.body;
|
|
|
|
|
|
try {
|
|
let n_correo = depurado(body.correo);
|
|
let cad = `select * from persona where correo_electronico='${n_correo}';`;
|
|
let respuesta = await query(cad);
|
|
if (respuesta.length != 0) {
|
|
|
|
res.status(400).json({ error: false, msj: "el correo ya se encuentra registrado" });
|
|
return;
|
|
}
|
|
if (body.numero_cuenta != null) {
|
|
let n_cuenta = body.numero_cuenta.toString();
|
|
n_cuenta = depurado(n_cuenta);
|
|
cad = `select * from persona where numero_cuenta='${n_cuenta}';`;
|
|
respuesta = await query(cad);
|
|
if (respuesta.length != 0) {
|
|
res.status(400).json({ error: false, msj: "el numero de cuenta ya se registro" });
|
|
return;
|
|
}
|
|
}
|
|
let n_nombre = depurado(body.nombre);
|
|
let n_apellido_p = depurado(body.apellido_p);
|
|
let n_apellido_m = depurado(body.apellido_m);
|
|
let n_institucion = depurado(body.institucion);
|
|
let n_cuenta = depurado(body.numero_cuenta);
|
|
let po = false;
|
|
let as = false;
|
|
if (body.tipo_participacion == "ponente") {
|
|
po = true;
|
|
as = true;
|
|
} else
|
|
as = true;
|
|
if (n_cuenta != null)
|
|
n_cuenta = "'" + n_cuenta + "'";
|
|
let n_pasword = await bcrypt.hash(body.password, 10);
|
|
console.log(n_pasword);
|
|
cad = `insert into persona values (null,'${n_nombre}', '${n_apellido_p}', '${n_apellido_m}', ${po}, ${as}, '${n_institucion}', '${body.comunidad}',
|
|
'${body.situacion_academica}', '${body.pais}', ${n_cuenta}, '${n_correo}' ,'${n_pasword}', false );`;
|
|
respuesta = query(cad);
|
|
respuesta.then(async() => {
|
|
cad = `select id_persona from persona where correo_electronico='${n_correo}'`;
|
|
let n_res = await query(cad);
|
|
console.log(n_res);
|
|
res.status(200).json({ error: false, msj: "exito", id_persona: n_res[0].id_persona });
|
|
return;
|
|
}).catch(err => {
|
|
console.log("entro ...", err);
|
|
res.status(400).json({ error: true, msj: err });
|
|
return;
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
console.log("afuera ", err);
|
|
res.status(400).json({ error: true, msj: err });
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = app |