route y controller de los cuestionarios, aun en desarrollo

This commit is contained in:
Tu Nombre
2025-01-28 12:10:52 -06:00
parent 493fd83e0e
commit d181790277
6 changed files with 417 additions and 11 deletions
+57 -2
View File
@@ -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;
+100 -1
View File
@@ -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;