cuestionario, seccion, pregunta y opcion listos
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
@@ -0,0 +1,66 @@
|
||||
const Pregunta = require(`../../db/tablas/Pregunta`);
|
||||
const TipoPregunta = require('../../db/tablas/TipoPregunta');
|
||||
const validar = require(`../../helper/validar`);
|
||||
|
||||
const editar = async (body) => {
|
||||
const idPregunta = validar.validarNumeroEntero(body.idPregunta, 'idPregunta');
|
||||
const data = {};
|
||||
|
||||
if (!!body.pregunta) {
|
||||
data.pregunta = validar.validacionBasicaStr(
|
||||
body.pregunta,
|
||||
'pregunta',
|
||||
false,
|
||||
265
|
||||
);
|
||||
}
|
||||
if (!!body.idPreguntaDepende) {
|
||||
data.idPreguntaDepende = validar.validarNumeroEntero(
|
||||
body.idPreguntaDepende,
|
||||
'idPreguntaDepende',
|
||||
true
|
||||
);
|
||||
}
|
||||
if (!!body.idTipoPregunta) {
|
||||
data.idTipoPregunta = validar.validarNumeroEntero(
|
||||
body.idTipoPregunta,
|
||||
'idTipoPregunta',
|
||||
true
|
||||
);
|
||||
}
|
||||
if (!!body.otra !== null) data.otra = body.obligatoria;
|
||||
if (!!body.obligatoria !== null) data.obligatoria = body.obligatoria;
|
||||
|
||||
const extistePregunta = await Pregunta.findOne({ where: { idPregunta } });
|
||||
if (!extistePregunta) throw new Error('No existe esta pregunta.');
|
||||
|
||||
if (data.idPreguntaDepende) {
|
||||
const extistePreguntaDepende = await Pregunta.findOne({
|
||||
where: { idPregunta: data.idPreguntaDepende },
|
||||
});
|
||||
if (!extistePreguntaDepende)
|
||||
throw new Error('No existe la pregunta de la que depende.');
|
||||
}
|
||||
|
||||
if (data.idTipoPregunta) {
|
||||
const existeTipoPregunta = await TipoPregunta.findOne({
|
||||
where: { idTipoPregunta: data.idTipoPregunta },
|
||||
});
|
||||
if (!existeTipoPregunta) throw new Error('No existe el tipo pregunta.');
|
||||
}
|
||||
|
||||
const res = await Pregunta.update(
|
||||
{
|
||||
pregunta: data.pregunta,
|
||||
idTipoPregunta: data.idTipoPregunta,
|
||||
idPreguntaDepende: data.idPreguntaDepende,
|
||||
otra: data.otra,
|
||||
obligatoria: data.obligatoria,
|
||||
},
|
||||
{ where: { idPregunta } }
|
||||
);
|
||||
if (!res) throw new Error('Se ha actualizado la pregunta con éxito.');
|
||||
return {message: 'Se ha actualizado la pregunta con éxito.'};
|
||||
};
|
||||
|
||||
module.exports = editar;
|
||||
@@ -0,0 +1,18 @@
|
||||
const Pregunta = require('../../db/tablas/Pregunta');
|
||||
const validar = require('../../helper/validar');
|
||||
|
||||
const traerPoridSeccion = async (query) => {
|
||||
const idSeccion = validar.validarNumeroEntero(
|
||||
query.idSeccion,
|
||||
'idSeccion'
|
||||
);
|
||||
|
||||
return Pregunta.findAll({
|
||||
where: { idSeccion }
|
||||
}).then(res => {
|
||||
if(!res) throw new Error("No hay ninguna pregunta con este idSeccion.")
|
||||
return res;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = traerPoridSeccion;
|
||||
Reference in New Issue
Block a user