const validar = require('../../helper/validar'); const CuestionarioPrograma = require('../../db/tablas/CuestionarioPrograma'); const Servicio = require('../../db/tablas/Servicio'); const nuevo = async (body) => { let idServicio = validar.validarId(body.idServicio); let actividad1 = body.actividad1; let actividad2 = body.actividad2; let actividad3 = body.actividad3; let actividad4 = body.actividad4; let actividad5 = body.actividad5; let retroalimentacion = body.retroalimentacion; let p6 = body.p6; let p7 = body.p7; 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.'); } }) .then((res) => { return Servicio.update( { idCuestionarioPrograma: res.idCuestionarioPrograma }, { where: { idServicio } } ); }) .then((res) => { return validar.validarPreTermino(idServicio); }) .then((res) => { return { message: `Se guradaron tus respuestas correctamente. ${res}` }; }); }; module.exports = nuevo;