const validar = require('../../helper/validar'); const CuestionarioAlumno = require('../../db/tablas/CuestionarioAlumno'); const Servicio = require('../../db/tablas/Servicio'); const nuevo = async (body) => { const idServicio = validar.validarId(body.idServicio); 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.'); } }) .then((res) => Servicio.update( { idCuestionarioAlumno: res.idCuestionarioAlumno }, { where: { idServicio } } ) ) .then((res) => validar.validarPreTermino(idServicio)) .then((res) => ({ message: `Se guardaron tus respuestas correctamente. ${res}`, })); }; module.exports = nuevo;