285 lines
13 KiB
JavaScript
285 lines
13 KiB
JavaScript
const connection = require('../conf/connection.js');
|
|
const express = require('express');
|
|
const bcrypt = require('bcrypt');
|
|
const clave = require('./elegir_clave.js');
|
|
|
|
const email = require('./enviar_correo.js');
|
|
|
|
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('/datos_personales', async(req, res) => {
|
|
let body = req.body;
|
|
console.log(body);
|
|
let mandar = '';
|
|
try {
|
|
let n_correo = depurado(body.correo);
|
|
let cad = `select * from persona where correo='${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;
|
|
}
|
|
|
|
let curp = body.curp;
|
|
curp = depurado(curp);
|
|
cad = `select * from persona where curp='${curp}';`;
|
|
respuesta = await query(cad);
|
|
if (respuesta.length != 0) {
|
|
res.status(400).json({ error: false, msj: "El CURP ya esta registrado" });
|
|
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_curp = curp;
|
|
let n_genero = depurado(body.genero);
|
|
let n_edad = body.edad;
|
|
let n_telefono = depurado(body.telefono);
|
|
let n_password = await bcrypt.hash(body.password, 10);
|
|
let n_comunidad = depurado(body.comunidad);
|
|
let n_situacion = depurado(body.situacion);
|
|
let n_universidad = depurado(body.univrsidad);
|
|
// console.log(n_pasword);
|
|
|
|
|
|
// CREATE TABLE `persona` (
|
|
// `id_persona` integer PRIMARY KEY NOT NULL AUTO_INCREMENT,
|
|
// `nombre` varchar(100),
|
|
// `apellidp_p` varchar(100),
|
|
// `apellidp_m` varchar(100),
|
|
// `curp` varchar(200),
|
|
// `edad` integer,
|
|
// `genero` varchar(10),
|
|
// `telefono` varchar(50),
|
|
// `correo` varchar(100),
|
|
// `pasword` varchar(300),
|
|
// `comunidad` varchar(50),
|
|
// `situacion_academica` varchar(50)
|
|
// );
|
|
|
|
cad = `insert into persona values (null,'${n_nombre}', '${n_apellido_p}', '${n_apellido_m}', '${n_curp}', ${n_edad}, '${n_genero}',
|
|
'${n_telefono}','${n_correo}', '${n_password}', '${n_comunidad}', '${n_situacion}' ,'${ n_universidad}' );`;
|
|
respuesta = query(cad);
|
|
respuesta.then(async() => {
|
|
|
|
cad = `select id_persona from persona where correo='${n_correo}'`;
|
|
let n_res = await query(cad);
|
|
console.log(n_res);
|
|
|
|
|
|
let id_persona = n_res[0].id_persona;
|
|
|
|
if (n_situacion == 'Alumno') {
|
|
try {
|
|
let n_cuenta = null;
|
|
if (body.cuenta != undefined)
|
|
n_cuenta = depurado(body.cuenta.toString());
|
|
|
|
let n_year = depurado(body.año.toString());
|
|
let n_participacion = depurado(body.participacion);
|
|
|
|
// if (body.tipo_pago == 'ficha') {
|
|
// let n_respuesta = clave(id_persona, n_correo);
|
|
// n_respuesta.then(p => {
|
|
// if (p[0].error == true) {
|
|
// return res.status(400).json({ err: true, msj: p[0].msj, type: err });
|
|
// } else
|
|
// mandar = p[0].msj;
|
|
// })
|
|
// }
|
|
console.log("tipo pde pago: ", body.tipo_pago);
|
|
if (n_cuenta != null)
|
|
cad = `insert into alumno values( ${id_persona}, '${n_cuenta}', '${n_year}', '${body.tipo_pago}' ) ;`;
|
|
else
|
|
cad = `insert into alumno values( ${id_persona}, null, '${n_year}', '${body.tipo_pago}' ) ;`;
|
|
|
|
|
|
let n_ans = query(cad);
|
|
n_ans.then(async() => {
|
|
|
|
if (body.conversatorio != undefined) {
|
|
let con = body.conversatorio;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${con.id_evento}, false, false , false , false, false, '${con.participacion}' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${con.id_evento};`;
|
|
n_q = await query(cad);
|
|
}
|
|
if (body.taller_a != undefined) {
|
|
let ta = body.taller_a;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, 'Asistente' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
}
|
|
if (body.taller_p != undefined) {
|
|
let ta = body.taller_p;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, 'Asistente');`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
|
|
}
|
|
if (body.visitas != undefined) {
|
|
let ta = body.visitas;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, 'Asistente');`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
}
|
|
if (body.magistral != undefined) {
|
|
let ta = body.magistral;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, true, false , false , false, false, 'Asistente' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
}
|
|
|
|
let msj = "Bienvenido a ASINEA 2020\n\nEn el transcurso de dos días hábiles sera enviado a tu correo las instrucciones de pago."
|
|
let encabezado = "Registro ASINEA 2020";
|
|
|
|
email(n_correo, encabezado, msj);
|
|
// send.then(() => {
|
|
return res.status(200).json({ error: false, msj: "Exito", id_persona: id_persona, clave: mandar });
|
|
// }).catch(err => {
|
|
// return res.status(400).json({ error: false, msj: "no se puede enviar el correo" });
|
|
// })
|
|
|
|
|
|
}).catch(async(err) => {
|
|
console.log(err);
|
|
|
|
//se deberia borrar el registro anterio de persona;
|
|
|
|
return res.status(400).json({ err: true, msj: "Error al insertar en la base en alumno", type: err });
|
|
})
|
|
} catch (err) {
|
|
|
|
//se deberia borrar el registro anterio de persona;
|
|
console.log(err);
|
|
return res.status(400).json({ err: true, msj: "Error al insertar en la base en alumno", type: err });
|
|
|
|
}
|
|
} else if (n_situacion == 'Académico') {
|
|
try {
|
|
let n_grado = depurado(body.grado);
|
|
let n_asignatura = depurado(body.asignatura);
|
|
let n_participacion = depurado(body.participacion);
|
|
cad = `insert into academico values(${id_persona}, '${n_grado}', '${n_asignatura}', '${body.tipo_pago}' );`;
|
|
let n_ans = query(cad);
|
|
n_ans.then(async() => {
|
|
|
|
if (body.conversatorio != undefined) {
|
|
let con = body.conversatorio;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${con.id_evento}, false, false , false , false, false, '${con.participacion}');`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${con.id_evento};`;
|
|
n_q = await query(cad);
|
|
}
|
|
if (body.taller_a != undefined) {
|
|
let ta = body.taller_a;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, '${ta.participacion}' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
}
|
|
if (body.taller_p != undefined) {
|
|
let ta = body.taller_p;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, '${ta.participacion}' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
|
|
}
|
|
if (body.visitas != undefined) {
|
|
let ta = body.visitas;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, '${ta.participacion}' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
}
|
|
if (body.magistral != undefined) {
|
|
let ta = body.magistral;
|
|
cad = `insert into asistencia values(null, ${id_persona}, ${ta.id_evento}, false, false , false , false, false, '${ta.participacion}' );`;
|
|
let n_q = await query(cad);
|
|
cad = `update evento set cupo= cupo-1 where id_evento=${ta.id_evento};`;
|
|
n_q = await query(cad);
|
|
|
|
}
|
|
|
|
|
|
|
|
let msj = "Bienvenido a ASINEA 2020\n\nEn el transcurso de dos días hábiles sera enviado a tu correo las instrucciones de pago."
|
|
let encabezado = "Registro ASINEA 2020";
|
|
|
|
email(n_correo, encabezado, msj);
|
|
//send.then(() => {
|
|
return res.status(200).json({ error: false, msj: "Exito", id_persona: id_persona, clave: mandar });
|
|
// }).catch(err => {
|
|
// return res.status(400).json({ error: false, msj: "no se puede enviar el correo" });
|
|
// })
|
|
|
|
|
|
|
|
|
|
|
|
}).catch(async err => {
|
|
//se deberia borrar el registro anterio de persona;
|
|
//se deberia borrar el registro anterio de persona;
|
|
console.log(err);
|
|
return res.status(400).json({ err: true, msj: "Error al insertar en la base en academico", type: err });
|
|
})
|
|
|
|
} catch (err) {
|
|
//se deberia borrar el registro anterio de persona;
|
|
console.log(err);
|
|
return res.status(400).json({ err: true, msj: "Error al insertar en la base en academico", type: err });
|
|
|
|
}
|
|
} else {
|
|
|
|
res.status(200).json({ error: false, msj: "Exito", id_persona: id_persona, clave: mandar });
|
|
return;
|
|
}
|
|
}).catch(async err => {
|
|
console.log("entro ...", err);
|
|
|
|
//se deberia borrar el registro anterio de persona;
|
|
res.status(400).json({ error: true, msj: "Error al registrar en la base de datos", type: err });
|
|
return;
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
console.log("afuera ", err);
|
|
res.status(400).json({ error: true, msj: err });
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = app |