286 lines
7.9 KiB
JavaScript
286 lines
7.9 KiB
JavaScript
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const CuestionarioAlumno = require(`${dbPath}/CuestionarioAlumno`);
|
|
const Servicio = require(`${dbPath}/Servicio`);
|
|
|
|
|
|
const CuestionarioAlumno2 = require(`${dbPath}/CuestionarioAlumno2`);
|
|
|
|
|
|
const nuevoOld = async (body) => {
|
|
const idServicio = validar.validarNumeroEntero(
|
|
body.idServicio,
|
|
'id servicio'
|
|
);
|
|
const sexo = body.sexo;
|
|
const edad = body.edad;
|
|
const servicioMedico = body.servicioMedico;
|
|
const p1 = body.p1;
|
|
const p2 = body.p2;
|
|
const p4 = body.p4;
|
|
const p6 = body.p6;
|
|
const p7 = body.p7;
|
|
const p9 = body.p9;
|
|
const p10 = body.p10;
|
|
const p11 = body.p11;
|
|
const p12 = body.p12;
|
|
const p13 = body.p13;
|
|
const p16 = body.p16;
|
|
const p18 = body.p18;
|
|
const p19 = body.p19;
|
|
const p20 = body.p20;
|
|
const p25 = body.p25;
|
|
const p26 = body.p26;
|
|
const p27 = body.p27;
|
|
const p28 = body.p28;
|
|
const p29 = body.p29;
|
|
const p30 = body.p30;
|
|
let p3 = '';
|
|
let p5 = '';
|
|
let p8 = '';
|
|
let p14 = '';
|
|
let p15 = '';
|
|
let p17 = '';
|
|
let p21 = '';
|
|
let p22 = '';
|
|
let p23 = '';
|
|
let p24 = '';
|
|
|
|
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 += ',';
|
|
}
|
|
return Servicio.findOne({ where: { idServicio } })
|
|
.then((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'
|
|
);
|
|
switch (res.idStatus) {
|
|
case 4:
|
|
return CuestionarioAlumno.create({
|
|
sexo,
|
|
edad,
|
|
servicioMedico,
|
|
p1,
|
|
p2,
|
|
p3,
|
|
p4,
|
|
p5,
|
|
p6,
|
|
p7,
|
|
p8,
|
|
p9,
|
|
p10,
|
|
p11,
|
|
p12,
|
|
p13,
|
|
p14,
|
|
p15,
|
|
p16,
|
|
p17,
|
|
p18,
|
|
p19,
|
|
p20,
|
|
p21,
|
|
p22,
|
|
p23,
|
|
p24,
|
|
p25,
|
|
p26,
|
|
p27,
|
|
p28,
|
|
p29,
|
|
p30,
|
|
});
|
|
case 1:
|
|
case 2:
|
|
case 3:
|
|
throw new Error(
|
|
'Aun no se puede contestar el cuestionario este Servicio Social.'
|
|
);
|
|
case 5:
|
|
case 6:
|
|
throw new Error(
|
|
'Este Servicio Social ya paso la fase de contestar el cuestionario.'
|
|
);
|
|
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(
|
|
{ idCuestionarioAlumno: res.idCuestionarioAlumno },
|
|
{ where: { idServicio } }
|
|
)
|
|
)
|
|
.then((res) => validar.validarPreTermino(idServicio))
|
|
.then((res) => ({
|
|
message: `Se guardaron tus respuestas correctamente. ${res}`,
|
|
}));
|
|
};
|
|
|
|
|
|
|
|
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.idCuestionarioAlumno2)
|
|
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 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("datos a registrar en la tabla cuestionarioAlumno2", { ...body} );
|
|
|
|
let respuestasRegistradas = await CuestionarioAlumno2.create({ ...body });
|
|
console.log('despues de registrar cuestionario', respuestasRegistradas);
|
|
|
|
|
|
|
|
|
|
console.log('agregar el id a la tabla servicio, esta validado en el front');
|
|
|
|
let resX = await Servicio.update(
|
|
{ idCuestionarioAlumno2: respuestasRegistradas.idCuestionarioAlumno2},
|
|
{ where: { idServicio } }
|
|
)
|
|
|
|
console.log('despues de registrar el id en la tabla servicio', resX);
|
|
|
|
|
|
|
|
return respuestasRegistradas;
|
|
})
|
|
|
|
|
|
.then((res) => validar.validarPreTermino(idServicio))
|
|
|
|
|
|
.then((res) => ({
|
|
message: `Se guardaron tus respuestas correctamente. ${res}`,
|
|
}));
|
|
};
|
|
|
|
|
|
module.exports = nuevo;
|