route y controller de los cuestionarios, aun en desarrollo
This commit is contained in:
@@ -10,7 +10,12 @@ const Programa = require(`${dbPath}/Programa`);
|
||||
const Servicio = require(`${dbPath}/Servicio`);
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
|
||||
const get = async (body) => {
|
||||
const CuestionarioAlumno2 = require(`${dbPath}/CuestionarioAlumno2`);
|
||||
|
||||
|
||||
|
||||
|
||||
const getOld = async (body) => {
|
||||
const year = validarNumero(body.year, 'año', true, 4);
|
||||
const path = `server/uploads/${year}_cuestionario_alumno.csv`;
|
||||
const data = [];
|
||||
@@ -79,4 +84,140 @@ const get = async (body) => {
|
||||
.then((res) => path);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
const get = async (body) => {
|
||||
const year = validarNumero(body.year, 'año', true, 4);
|
||||
const path = `server/uploads/${year}_cuestionario_alumno.csv`;
|
||||
const data = [];
|
||||
|
||||
console.log(year);
|
||||
|
||||
|
||||
/*
|
||||
return Servicio.findAll({
|
||||
include: [
|
||||
{
|
||||
model: CuestionarioAlumno2,
|
||||
where: { idCuestionarioAlumno: { [Op.not]: null } },
|
||||
},
|
||||
{ model: Programa, where: { clavePrograma: { [Op.like]: `${year}%` } } },
|
||||
{ model: Usuario },
|
||||
{ model: Carrera },
|
||||
],
|
||||
order: [['createdAt']],
|
||||
})
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/* return CuestionarioAlumno2.findAll({
|
||||
where: {
|
||||
idCuestionarioAlumno2: {
|
||||
[Op.not]: null,
|
||||
},
|
||||
},
|
||||
// order: [['createdAt', 'ASC']], // Descomentar si se requiere ordenamiento
|
||||
})
|
||||
.then(async (res) => {
|
||||
|
||||
console.log("segunda parte",res);
|
||||
|
||||
for (let i = 0; i < res.length; i++)
|
||||
data.push({ ...res})
|
||||
|
||||
|
||||
data.push({
|
||||
idServicio: res[i].idServicio,
|
||||
clavePrograma: res[i].Programa.clavePrograma,
|
||||
usuario: res[i].Usuario.usuario,
|
||||
nombre: res[i].Usuario.nombre,
|
||||
carrera: res[i].Carrera.carrera,
|
||||
idCuestionarioAlumno: res[i].CuestionarioAlumno.idCuestionarioAlumno,
|
||||
sexo: res[i].CuestionarioAlumno.sexo,
|
||||
edad: res[i].CuestionarioAlumno.edad,
|
||||
servicioMedico: res[i].CuestionarioAlumno.servicioMedico,
|
||||
p1: res[i].CuestionarioAlumno.p1,
|
||||
p2: res[i].CuestionarioAlumno.p2,
|
||||
p3: res[i].CuestionarioAlumno.p3,
|
||||
p4: res[i].CuestionarioAlumno.p4,
|
||||
p5: res[i].CuestionarioAlumno.p5,
|
||||
p6: res[i].CuestionarioAlumno.p6,
|
||||
p7: res[i].CuestionarioAlumno.p7,
|
||||
p8: res[i].CuestionarioAlumno.p8,
|
||||
p9: res[i].CuestionarioAlumno.p9,
|
||||
p10: res[i].CuestionarioAlumno.p10,
|
||||
p11: res[i].CuestionarioAlumno.p11,
|
||||
p12: res[i].CuestionarioAlumno.p12,
|
||||
p13: res[i].CuestionarioAlumno.p13,
|
||||
p14: res[i].CuestionarioAlumno.p14,
|
||||
p15: res[i].CuestionarioAlumno.p15,
|
||||
p16: res[i].CuestionarioAlumno.p16,
|
||||
p17: res[i].CuestionarioAlumno.p17,
|
||||
p18: res[i].CuestionarioAlumno.p18,
|
||||
p19: res[i].CuestionarioAlumno.p19,
|
||||
p20: res[i].CuestionarioAlumno.p20,
|
||||
p21: res[i].CuestionarioAlumno.p21,
|
||||
p22: res[i].CuestionarioAlumno.p22,
|
||||
p23: res[i].CuestionarioAlumno.p23,
|
||||
p24: res[i].CuestionarioAlumno.p24,
|
||||
p25: res[i].CuestionarioAlumno.p25,
|
||||
p26: res[i].CuestionarioAlumno.p26,
|
||||
p27: res[i].CuestionarioAlumno.p27,
|
||||
p28: res[i].CuestionarioAlumno.p28,
|
||||
p29: res[i].CuestionarioAlumno.p29,
|
||||
p30: res[i].CuestionarioAlumno.p30,
|
||||
createdAt: res[i].CuestionarioAlumno.createdAt,
|
||||
});
|
||||
await helper.eliminarArchivo(path).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
return helper.crearArchivo(path, convertArrayToCSV(data));
|
||||
})
|
||||
.then((res) => path);
|
||||
}; */
|
||||
|
||||
|
||||
|
||||
return CuestionarioAlumno2.findAll({
|
||||
where: {
|
||||
idCuestionarioAlumno2: {
|
||||
[Op.not]: null,
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(async (res) => {
|
||||
console.log("segunda parte", res);
|
||||
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
data.push(res[i].get({ plain: true }));
|
||||
}
|
||||
|
||||
// Alternativa manual si necesitas campos específicos
|
||||
/* for (let i = 0; i < res.length; i++) {
|
||||
const row = res[i].get({ plain: true });
|
||||
data.push({
|
||||
idCuestionarioAlumno2: row.idCuestionarioAlumno2,
|
||||
p1: row.p1,
|
||||
p2: row.p2,
|
||||
p3: row.p3,
|
||||
p4: row.p4,
|
||||
p5: row.p5,
|
||||
// Agrega los demás campos según sea necesario
|
||||
});
|
||||
} */
|
||||
|
||||
await helper.eliminarArchivo(path).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
return helper.crearArchivo(path, convertArrayToCSV(data));
|
||||
})
|
||||
.then((res) => path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = get;
|
||||
|
||||
@@ -3,7 +3,12 @@ const dbPath = '../../db/tablas';
|
||||
const CuestionarioAlumno = require(`${dbPath}/CuestionarioAlumno`);
|
||||
const Servicio = require(`${dbPath}/Servicio`);
|
||||
|
||||
const nuevo = async (body) => {
|
||||
|
||||
const CuestionarioAlumno2 = require(`${dbPath}/CuestionarioAlumno2`);
|
||||
const Cuestionario = require(`${dbPath}/Cuestionario`);
|
||||
|
||||
|
||||
const nuevoOld = async (body) => {
|
||||
const idServicio = validar.validarNumeroEntero(
|
||||
body.idServicio,
|
||||
'id servicio'
|
||||
@@ -161,4 +166,101 @@ const nuevo = async (body) => {
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
const nuevo = async (body) => {
|
||||
// console.log(body);
|
||||
|
||||
let idServicio = body.idServicio;
|
||||
/*
|
||||
const idServicio = validar.validarNumeroEntero(
|
||||
body.idServicio,
|
||||
'id servicio'
|
||||
); */
|
||||
|
||||
|
||||
body = validar.validarCuestionarioAlumno2(body);
|
||||
|
||||
|
||||
// pasar preguntas de array a string
|
||||
|
||||
return Servicio.findOne({ where: { idServicio } })
|
||||
.then(async (res) => {
|
||||
if (!res) throw new Error('No existe este Servicio Social.');
|
||||
/*
|
||||
if (res.idCuestionarioAlumno)
|
||||
throw new Error(
|
||||
'Este Servicio Social ya cuenta con cuestionario de alumno'
|
||||
);
|
||||
*/
|
||||
|
||||
console.log('el servicio social si existe');
|
||||
// console.log(res);
|
||||
|
||||
/**
|
||||
* existe el servicio social?
|
||||
* tiene cuestionario alumno en la tabla cuestionario? select * from cuestionario where idServicio = 1
|
||||
* si no existe, entonces se puede crear el cuestionario
|
||||
* creamos el cuestionario y guardamos las respuestas
|
||||
*/
|
||||
|
||||
const messageByStatus = {
|
||||
1: 'Aun no se puede contestar el cuestionario este Servicio Social.',
|
||||
2: 'Aun no se puede contestar el cuestionario este Servicio Social.',
|
||||
3: 'Aun no se puede contestar el cuestionario este Servicio Social.',
|
||||
5: 'Este Servicio Social ya paso la fase de contestar el cuestionario.',
|
||||
6: 'Este Servicio Social ya paso la fase de contestar el cuestionario.',
|
||||
7: 'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corrija lo necesario.',
|
||||
8: 'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corrija lo necesario.',
|
||||
9: 'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corrija lo necesario.',
|
||||
10: 'Este Servicio Social fue cancelado.',
|
||||
};
|
||||
|
||||
// if (res.idStatus === 4) return CuestionarioAlumno2.create({ ...body });
|
||||
console.log('antes de create ques2');
|
||||
|
||||
// if (res.idStatus) return CuestionarioAlumno2.create({ ...body });
|
||||
|
||||
// si idStatus está en el objeto, arrojamos el mensaje
|
||||
|
||||
/* if (messageByStatus.hasOwnProperty(res.idStatus))
|
||||
throw new Error(messageByStatus[res.idStatus]); */
|
||||
|
||||
// throw new Error('Id status no valido.'); // si no coincide con nada, error por defecto
|
||||
|
||||
Cuestionario.findOne({});
|
||||
|
||||
let dataCuestionario = await Cuestionario.findOne({
|
||||
where: { idServicio, dirigidoA: 'estudiantes' },
|
||||
});
|
||||
console.log('data cuestionario', dataCuestionario);
|
||||
|
||||
if (dataCuestionario) {
|
||||
throw new Error('Ya existe un cuestionario para este servicio social');
|
||||
}
|
||||
|
||||
let respuestasRegistradas = await CuestionarioAlumno2.create({ ...body });
|
||||
console.log('despues de registrar cuestionario', respuestasRegistradas);
|
||||
|
||||
let cuestionarioRegistro = await Cuestionario.create({
|
||||
idServicio,
|
||||
idCuestionarioAlumno: respuestasRegistradas.idCuestionarioAlumno,
|
||||
version: '2',
|
||||
dirigidoA: 'estudiantes',
|
||||
titulo: 'Cuestionario de Alumn v2',
|
||||
desc: 'Cuestionario de Alumno v2',
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
console.log('cuestionario registrado', cuestionarioRegistro);
|
||||
|
||||
return cuestionarioRegistro;
|
||||
})
|
||||
|
||||
.then((res) => ({
|
||||
message: `Se guardaron tus respuestas correctamente. ${res}`,
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = nuevo;
|
||||
|
||||
@@ -6,12 +6,17 @@ const helper = require(`${helperPath}/helper`);
|
||||
const { validarNumero } = require(`${helperPath}/validar`);
|
||||
const dbPath = '../../db/tablas';
|
||||
const Carrera = require(`${dbPath}/Carrera`);
|
||||
const CuestionarioPrograma = require(`${dbPath}/CuestionarioPrograma`);
|
||||
const Servicio = require(`${dbPath}/Servicio`);
|
||||
const Programa = require(`${dbPath}/Programa`);
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
|
||||
const get = async (body) => {
|
||||
|
||||
const CuestionarioPrograma2 = require(`${dbPath}/CuestionarioPrograma`);
|
||||
|
||||
|
||||
|
||||
const CuestionarioPrograma = require(`${dbPath}/CuestionarioPrograma`);
|
||||
const getOld = async (body) => {
|
||||
const year = validarNumero(body.year, 'año', true, 4);
|
||||
const path = `server/uploads/${year}_cuestionario_programa.csv`;
|
||||
const data = [];
|
||||
@@ -56,4 +61,54 @@ const get = async (body) => {
|
||||
.then((res) => path);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const get = async (body) => {
|
||||
const year = validarNumero(body.year, 'año', true, 4);
|
||||
const path = `server/uploads/${year}_cuestionario_programa.csv`;
|
||||
const data = [];
|
||||
|
||||
|
||||
|
||||
return CuestionarioPrograma2.findAll({
|
||||
where: {
|
||||
idCuestionarioPrograma2: {
|
||||
[Op.not]: null,
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(async (res) => {
|
||||
console.log("segunda parte", res);
|
||||
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
data.push(res[i].get({ plain: true }));
|
||||
}
|
||||
|
||||
// Alternativa manual si necesitas campos específicos
|
||||
/* for (let i = 0; i < res.length; i++) {
|
||||
const row = res[i].get({ plain: true });
|
||||
data.push({
|
||||
idCuestionarioPrograma2: row.idCuestionarioPrograma2,
|
||||
p1: row.p1,
|
||||
p2: row.p2,
|
||||
p3: row.p3,
|
||||
p4: row.p4,
|
||||
p5: row.p5,
|
||||
// Agrega los demás campos según sea necesario
|
||||
});
|
||||
} */
|
||||
|
||||
await helper.eliminarArchivo(path).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
return helper.crearArchivo(path, convertArrayToCSV(data));
|
||||
})
|
||||
.then((res) => path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = get;
|
||||
|
||||
@@ -3,7 +3,13 @@ const dbPath = '../../db/tablas';
|
||||
const CuestionarioPrograma = require(`${dbPath}/CuestionarioPrograma`);
|
||||
const Servicio = require(`${dbPath}/Servicio`);
|
||||
|
||||
const nuevo = async (body) => {
|
||||
|
||||
const CuestionarioPrograma2 = require(`${dbPath}/CuestionarioPrograma2`);
|
||||
const Cuestionario = require(`${dbPath}/Cuestionario`);
|
||||
|
||||
|
||||
|
||||
const nuevoOld = async (body) => {
|
||||
const idServicio = validar.validarNumeroEntero(
|
||||
body.idServicio,
|
||||
'id servicio'
|
||||
@@ -76,4 +82,97 @@ const nuevo = async (body) => {
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
|
||||
const nuevo = async (body) => {
|
||||
// console.log(body);
|
||||
|
||||
let idServicio = body.idServicio;
|
||||
/*
|
||||
const idServicio = validar.validarNumeroEntero(
|
||||
body.idServicio,
|
||||
'id servicio'
|
||||
); */
|
||||
|
||||
// pasar preguntas de array a string
|
||||
|
||||
return Servicio.findOne({ where: { idServicio } })
|
||||
.then(async (res) => {
|
||||
if (!res) throw new Error('No existe este Servicio Social.');
|
||||
/* if (res.idCuestionarioAlumno)
|
||||
throw new Error(
|
||||
'Este Servicio Social ya cuenta con cuestionario de programa'
|
||||
); */
|
||||
|
||||
console.log('el servicio social si existe');
|
||||
// console.log(res);
|
||||
|
||||
/**
|
||||
* existe el servicio social?
|
||||
* tiene cuestionario alumno en la tabla cuestionario? select * from cuestionario where idServicio = 1
|
||||
* si no existe, entonces se puede crear el cuestionario
|
||||
* creamos el cuestionario y guardamos las respuestas
|
||||
*/
|
||||
|
||||
const messageByStatus = {
|
||||
1: 'Aun no se puede contestar el cuestionario este Servicio Social.',
|
||||
2: 'Aun no se puede contestar el cuestionario este Servicio Social.',
|
||||
3: 'Aun no se puede contestar el cuestionario este Servicio Social.',
|
||||
5: 'Este Servicio Social ya paso la fase de contestar el cuestionario.',
|
||||
6: 'Este Servicio Social ya paso la fase de contestar el cuestionario.',
|
||||
7: 'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corrija lo necesario.',
|
||||
8: 'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corrija lo necesario.',
|
||||
9: 'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corrija lo necesario.',
|
||||
10: 'Este Servicio Social fue cancelado.',
|
||||
};
|
||||
|
||||
// if (res.idStatus === 4) return CuestionarioPrograma2.create({ ...body });
|
||||
console.log('antes de create ques2');
|
||||
|
||||
// if (res.idStatus) return CuestionarioPrograma2.create({ ...body });
|
||||
// si idStatus está en el objeto, arrojamos el mensaje
|
||||
/* if (messageByStatus.hasOwnProperty(res.idStatus))
|
||||
throw new Error(messageByStatus[res.idStatus]); */
|
||||
// throw new Error('Id status no valido.'); // si no coincide con nada, error por defecto
|
||||
|
||||
let dataCuestionario = await Cuestionario.findOne({
|
||||
where: { idServicio, dirigidoA: 'programas' },
|
||||
});
|
||||
console.log('data cuestionario', dataCuestionario);
|
||||
|
||||
if (dataCuestionario) {
|
||||
throw new Error('Ya existe un cuestionario para este servicio social');
|
||||
}
|
||||
|
||||
let respuestasRegistradas = await CuestionarioPrograma2.create({
|
||||
...body,
|
||||
});
|
||||
|
||||
console.log('despues de registrar cuestionario', respuestasRegistradas);
|
||||
|
||||
// si el cuestionario ya existe solo hay que actualizar el id de cuestionarioprograma o alumno
|
||||
|
||||
let cuestionarioRegistro = await Cuestionario.create({
|
||||
idServicio,
|
||||
idCuestionarioPrograma: respuestasRegistradas.idCuestionarioPrograma,
|
||||
version: '2',
|
||||
dirigidoA: 'programas',
|
||||
titulo: 'Cuestionario de programa v2',
|
||||
desc: 'Cuestionario de programa v2',
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
console.log('cuestionario registrado', cuestionarioRegistro);
|
||||
|
||||
return cuestionarioRegistro;
|
||||
})
|
||||
|
||||
.then((res) => ({
|
||||
message: `Se guardaron tus respuestas correctamente. ${res}`,
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = nuevo;
|
||||
|
||||
@@ -2,12 +2,15 @@ const express = require('express');
|
||||
const app = express();
|
||||
const { verificaToken } = require('../middleware/autentificacion');
|
||||
const route = '/cuestionario_alumno';
|
||||
const controllerPath = '../controller/CuestionarioAlumno';
|
||||
//const controllerPath = '../controller/CuestionarioAlumno';
|
||||
const controllerPath = '../controller/CuestionarioAlumno2';
|
||||
|
||||
const get = require(`${controllerPath}/get`);
|
||||
const nuevo = require(`${controllerPath}/nuevo`);
|
||||
|
||||
// POST
|
||||
app.post(`${route}`, verificaToken, (req, res) => {
|
||||
app.post(`${route}`, // verificaToken,
|
||||
(req, res) => {
|
||||
return nuevo(req.body)
|
||||
.then((data) => {
|
||||
res.status(200).json(data);
|
||||
@@ -18,7 +21,9 @@ app.post(`${route}`, verificaToken, (req, res) => {
|
||||
});
|
||||
|
||||
// GET
|
||||
app.get(`${route}`, verificaToken, (req, res) => {
|
||||
app.get(`${route}`, // verificaToken,
|
||||
|
||||
(req, res) => {
|
||||
return get(req.query)
|
||||
.then((data) => {
|
||||
res.download(data);
|
||||
|
||||
@@ -2,12 +2,15 @@ const express = require('express');
|
||||
const app = express();
|
||||
const { verificaToken } = require('../middleware/autentificacion');
|
||||
const route = '/cuestionario_programa';
|
||||
const controllerPath = '../controller/CuestionarioPrograma';
|
||||
//const controllerPath = '../controller/CuestionarioPrograma';
|
||||
const controllerPath = '../controller/CuestionarioPrograma2';
|
||||
|
||||
const get = require(`${controllerPath}/get`);
|
||||
const nuevo = require(`${controllerPath}/nuevo`);
|
||||
|
||||
// POST
|
||||
app.post(`${route}`, verificaToken, (req, res) => {
|
||||
app.post(`${route}`, //verificaToken,
|
||||
(req, res) => {
|
||||
return nuevo(req.body)
|
||||
.then((data) => {
|
||||
res.status(200).json(data);
|
||||
@@ -18,7 +21,8 @@ app.post(`${route}`, verificaToken, (req, res) => {
|
||||
});
|
||||
|
||||
// GET
|
||||
app.get(`${route}`, verificaToken, (req, res) => {
|
||||
app.get(`${route}`, //verificaToken,
|
||||
(req, res) => {
|
||||
return get(req.query)
|
||||
.then((data) => {
|
||||
res.download(data);
|
||||
|
||||
Reference in New Issue
Block a user