const validar = require(`../../helper/validar`); const Seccion = require(`../../db/tablas/Seccion`); const TipoPregunta = require(`../../db/tablas/TipoPregunta`); const Pregunta = require(`../../db/tablas/Pregunta`); const crear = async (body) => { const idSeccion = validar.validarNumeroEntero(body.idSeccion, 'idSeccion'); const idTipoPregunta = validar.validarNumeroEntero( body.idTipoPregunta, 'idTipoPregunta' ); const idPreguntaDepende = body.idPreguntaDepende ? validar.validarNumeroEntero(body.idPreguntaDepende, 'idPreguntaDepende') : null; const pregunta = validar.validacionBasicaStr( body.pregunta, 'pregunta', false, 256 ); const otra = body.otra ? true : false; const obligatoria = body.obligatoria ? true : false; return Seccion.findOne({ where: { idSeccion } }).then(async (res) => { if (!res) throw new Error('Esta sección no existe en la db'); return TipoPregunta.findOne({ where: idTipoPregunta }).then(async (res) => { if (!res) throw new Error('Este tipo de pregunta no existe en la db.'); return Pregunta.findOne({ where: { idPregunta: idPreguntaDepende }, }).then(async (res) => { if (!res && body.idPreguntaDepende) throw new Error('La pregunta de la que depende no existe.'); return Pregunta.create({ idSeccion, idTipoPregunta, idPreguntaDepende, pregunta, otra, obligatoria, }); }); }); }); }; module.exports = crear;