Files
servicio_social_api/server/controller/CuestionarioPrograma/nuevo.js
T

212 lines
6.3 KiB
JavaScript

const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const CuestionarioPrograma = require(`${dbPath}/CuestionarioPrograma`);
const Servicio = require(`${dbPath}/Servicio`);
const CuestionarioPrograma2 = require(`${dbPath}/CuestionarioPrograma2`);
const Cuestionario = require(`${dbPath}/Cuestionario`);
const nuevoOld = async (body) => {
const idServicio = validar.validarNumeroEntero(
body.idServicio,
'id servicio'
);
const actividad1 = body.actividad1;
const actividad2 = body.actividad2;
const actividad3 = body.actividad3;
const actividad4 = body.actividad4;
const actividad5 = body.actividad5;
const p6 = body.p6;
const p7 = body.p7;
let retroalimentacion = '';
for (let i = 0; i < body.retroalimentacion.length; i++) {
retroalimentacion += body.retroalimentacion[i];
if (i < body.retroalimentacion.length - 1) retroalimentacion += ',';
}
return Servicio.findOne({ where: { idServicio } })
.then((res) => {
if (!res) throw new Error('No existe este Servicio Social.');
if (res.idCuestionarioPrograma)
throw new Error(
'Este Servicio Social ya cuenta con cuestionario de programa'
);
switch (res.idStatus) {
case 4:
return CuestionarioPrograma.create({
actividad1,
actividad2,
actividad3,
actividad4,
actividad5,
retroalimentacion,
p6,
p7,
});
case 1:
case 2:
case 3:
throw new Error(
'Aun no se puede contestar el cuestionario este Servicio Social.'
);
case 5:
throw new Error(
'Este Servicio Social ya paso la fase de contestar el cuestionario.'
);
case 6:
throw new Error('Este Servicio Social ya finalizó.');
case 7:
case 8:
case 9:
throw new Error(
'Este Servicio Social se encuentra rechazado. No se puede avanzar hasta que se corriga lo necesario.'
);
case 10:
throw new Error('Este Servicio Social fue cancelado.');
default:
throw new Error('Id status no valido.');
}
})
.then((res) =>
Servicio.update(
{ idCuestionarioPrograma: res.idCuestionarioPrograma },
{ where: { idServicio } }
)
)
.then((res) => validar.validarPreTermino(idServicio))
.then((res) => ({
message: `Se guradaron tus respuestas correctamente. ${res}`,
}));
};
const nuevo = async (body) => {
// console.log(body);
let idServicio = body.idServicio;
body = validar.validarCuestionarioAlumno2(body);
/*
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 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);
let respuestasRegistradas = await CuestionarioPrograma2.create({
...body,
idCuestionario: cuestionarioRegistro.idCuestionario
});
console.log('despues de registrar cuestionario', respuestasRegistradas);
// si el cuestionario ya existe solo hay que actualizar el id de cuestionarioprograma o alumno
console.log("registrar id en servicio en cuestionario 2");
let x = await Servicio.update(
{ idCuestionarioPrograma2: respuestasRegistradas.idCuestionarioPrograma2 },
{ where: { idServicio } }
)
console.log(x);
return cuestionarioRegistro;
})
.then((res) => validar.validarPreTermino(idServicio))
.then((res) => ({
message: `Se guardaron tus respuestas correctamente. ${res}`,
}));
};
module.exports = nuevo;