Files
servicio_social_api/server/controller/CuestionarioAlumno/nuevo.js
T
2021-01-04 18:41:51 -06:00

172 lines
4.0 KiB
JavaScript

const validar = require('../../helper/validar');
const CuestionarioAlumno = require('../../db/tablas/CuestionarioAlumno');
const Servicio = require('../../db/tablas/Servicio');
const nuevo = async (body) => {
let idServicio = validar.validarId(body.idServicio);
let sexo = body.sexo;
let edad = body.edad;
let servicioMedico = body.servicioMedico;
let p1 = body.p1;
let p2 = body.p2;
let p4 = body.p4;
let p6 = body.p6;
let p7 = body.p7;
let p9 = body.p9;
let p10 = body.p10;
let p11 = body.p11;
let p12 = body.p12;
let p13 = body.p13;
let p16 = body.p16;
let p18 = body.p18;
let p19 = body.p19;
let p20 = body.p20;
let p25 = body.p25;
let p26 = body.p26;
let p27 = body.p27;
let p28 = body.p28;
let p29 = body.p29;
let 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.');
}
})
.then((res) => {
return Servicio.update(
{ idCuestionarioAlumno: res.idCuestionarioAlumno },
{ where: { idServicio } }
);
})
.then((res) => {
return validar.validarPreTermino(idServicio);
})
.then((res) => {
return { message: `Se guardaron tus respuestas correctamente. ${res}` };
});
};
module.exports = nuevo;