nuevos cuestionarios de programa y alumno, sql de update, falta revisar el proceso de servicio para actualizar el get de los id cuestionarios

This commit is contained in:
Your Name
2025-01-22 11:24:19 -06:00
parent 2fc5c1f5fa
commit bea8a7b266
25 changed files with 3114 additions and 2156 deletions
@@ -0,0 +1,105 @@
const fs = require('fs');
const { convertArrayToCSV } = require('convert-array-to-csv');
const { Op } = require('sequelize');
const helperPath = '../../helper';
const helper = require(`${helperPath}/helper`);
const { validarNumero } = require(`${helperPath}/validar`);
const dbPath = '../../db/tablas';
const Carrera = require(`${dbPath}/Carrera`);
const CuestionarioPrograma2 = require(`${dbPath}/CuestionarioPrograma`);
const Servicio = require(`${dbPath}/Servicio`);
const Programa = require(`${dbPath}/Programa`);
const Usuario = require(`${dbPath}/Usuario`);
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);
}
/* return Servicio.findAll({
include: [
{
model: CuestionarioPrograma,
where: { idCuestionarioPrograma: { [Op.not]: null } },
},
{ model: Programa, where: { clavePrograma: { [Op.like]: `${year}-%` } } },
{ model: Usuario },
{ model: Carrera },
],
order: [['createdAt']],
})
.then(async (res) => {
for (let i = 0; i < res.length; i++)
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,
idCuestionarioPrograma:
res[i].CuestionarioPrograma.idCuestionarioPrograma,
actividad1: res[i].CuestionarioPrograma.actividad1,
actividad2: res[i].CuestionarioPrograma.actividad2,
actividad3: res[i].CuestionarioPrograma.actividad3,
actividad4: res[i].CuestionarioPrograma.actividad4,
actividad5: res[i].CuestionarioPrograma.actividad5,
retroalimentacion: res[i].CuestionarioPrograma.retroalimentacion,
p6: res[i].CuestionarioPrograma.p6,
p7: res[i].CuestionarioPrograma.p7,
createdAt: res[i].CuestionarioPrograma.createdAt,
});
await helper.eliminarArchivo(path).catch((err) => {
console.log(err);
});
return helper.crearArchivo(path, convertArrayToCSV(data));
})
.then((res) => path);
*/
module.exports = get;
@@ -0,0 +1,235 @@
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const CuestionarioAlumno = require(`${dbPath}/CuestionarioAlumno`);
const CuestionarioPrograma2 = require(`${dbPath}/CuestionarioPrograma2`);
const Cuestionario = require(`${dbPath}/Cuestionario`);
const Servicio = require(`${dbPath}/Servicio`);
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}`,
}));
};
const convertirSTR = () => {
for (let i = 0; i < body.p3.length; i++) {
p3 += body.p3[i];
if (i < body.p3.length - 1) p3 += ',';
}
for (let i = 0; i < body.p5.length; i++) {
p5 += body.p5[i];
if (i < body.p5.length - 1) p5 += ',';
}
for (let i = 0; i < body.p8.length; i++) {
p8 += body.p8[i];
if (i < body.p8.length - 1) p8 += ',';
}
for (let i = 0; i < body.p14.length; i++) {
p14 += body.p14[i];
if (i < body.p14.length - 1) p14 += ',';
}
for (let i = 0; i < body.p15.length; i++) {
p15 += body.p15[i];
if (i < body.p15.length - 1) p15 += ',';
}
for (let i = 0; i < body.p17.length; i++) {
p17 += body.p17[i];
if (i < body.p17.length - 1) p17 += ',';
}
for (let i = 0; i < body.p21.length; i++) {
p21 += body.p21[i];
if (i < body.p21.length - 1) p21 += ',';
}
for (let i = 0; i < body.p22.length; i++) {
p22 += body.p22[i];
if (i < body.p22.length - 1) p22 += ',';
}
for (let i = 0; i < body.p23.length; i++) {
p23 += body.p23[i];
if (i < body.p23.length - 1) p23 += ',';
}
for (let i = 0; i < body.p24.length; i++) {
p24 += body.p24[i];
if (i < body.p24.length - 1) p24 += ',';
}
};
module.exports = nuevo;
/*
<!-- respuestas de cuestionario formulario2
todas las preguntas se guardan correctamente
{
"p3_A_1": "Deficiente",
"p3_A_2": "Deficiente",
"p3_A_3": "Deficiente",
"p3_A_4": "Deficiente",
"p3_B_1": "Básico",
"p3_B_2": "Básico",
"p3_B_3": "Básico",
"p3_B_4": "Básico",
"p3_C_1": "Intermedio",
"p3_C_2": "Intermedio",
"p3_C_3": "Intermedio",
"p3_C_4": "Intermedio",
"p3_C_5": "Intermedio",
"p3_D_1": "Destacado",
"p3_D_2": "Destacado",
"p3_D_3": "Destacado",
"p3_D_4": "Destacado",
"p3_D_5": "Destacado",
"p3_E_1": "Deficiente",
"p1": "respuesta1",
"p2": "Excelente",
"p4": "respuesta4",
"p5": "Sí",
"p6": "respuesta 6"
}
-->
<!-- respuestas de cuestionario formulario alumno
{
"p1": [
"Dependencia de la UNAM",
"Asesoría académica",
"Oportunidad de desarrollo de tesis"
],
"p12_A_1": "Deficiente",
"p12_A_2": "Deficiente",
"p12_A_3": "Deficiente",
"p12_A_4": "Deficiente",
"p12_B_1": "Básico",
"p12_B_2": "Básico",
"p12_B_3": "Básico",
"p12_B_4": "Básico",
"p12_C_1": "Intermedio",
"p12_C_2": "Intermedio",
"p12_C_3": "Intermedio",
"p12_C_4": "Intermedio",
"p10_1": "si",
"p10_2": "si",
"p10_3": "si",
"p11_1": "Nunca",
"p11_2": "Rara vez",
"p11_3": "Algunas veces",
"p11_4": "Siempre",
"p17_1": "si",
"p17_2": "no",
"p17_3": "si",
"p3_1": "si",
"p3_2": "si",
"p3_3": "si",
"p3_4": "si",
"p12_D_1": "Destacado",
"p12_D_2": "Destacado",
"p12_D_3": "Destacado",
"p12_D_4": "Destacado",
"p12_D_5": "Destacado",
"p2": "Deficiente",
"p4": "No",
"p5": "respuesta5",
"p6": "Sí",
"p7": "Sí",
"p8": "respuesta8",
"p9": "Sí",
"p13": "Responsable directo del programa",
"p14": "Se te otorgó de acuerdo a lo establecido",
"p15": "Sí",
"p16": "respuesta 16",
"p18": "respuesta18"
}
-->
*/