cuestionario, seccion, pregunta y opcion listos
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
const validar = require(`../../helper/validar`);
|
||||
const Cuestionario = require(`../../db/tablas/Cuestionario`);
|
||||
const Usuario = require(`../../db/tablas/Usuario`);
|
||||
const TipoCuestionario = require(`../../db/tablas/TipoCuestionario`);
|
||||
|
||||
const crear = async (body) => {
|
||||
const idUsuario = validar.validarNumeroEntero(body.idUsuario, 'idUsuario');
|
||||
const idTipoCuestionario = validar.validarNumeroEntero(
|
||||
body.idTipoCuestionario,
|
||||
'idTipoCuestionario'
|
||||
);
|
||||
const fechaInicio = validar.validarFecha(body.fechaInicio, 'fecha inicio');
|
||||
const fechaFin = validar.validarFecha(body.fechaFin, 'fecha fin');
|
||||
const titulo = validar.validacionBasicaStr(body.titulo, 'titulo', true, 100);
|
||||
const descripcion = validar.validacionBasicaStr(
|
||||
body.descripcion,
|
||||
'descripcion',
|
||||
false,
|
||||
100
|
||||
);
|
||||
|
||||
return Usuario.findOne({ where: { idUsuario } }).then(async (res) => {
|
||||
if (!res) throw new Error('Este usuario no existe en la db');
|
||||
|
||||
return TipoCuestionario.findOne({ where: { idTipoCuestionario } }).then(
|
||||
async (res) => {
|
||||
if (!res)
|
||||
throw new Error('Este tipo de cuestionario no existe en la db');
|
||||
|
||||
return Cuestionario.create({
|
||||
idUsuario,
|
||||
idTipoCuestionario,
|
||||
fechaInicio,
|
||||
fechaFin,
|
||||
titulo,
|
||||
descripcion,
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = crear;
|
||||
@@ -0,0 +1,47 @@
|
||||
const Cuestionario = require(`../../db/tablas/Cuestionario`);
|
||||
const validar = require(`../../helper/validar`);
|
||||
|
||||
const editar = async (body) => {
|
||||
const idCuestionario = validar.validarNumeroEntero(
|
||||
body.idCuestionario,
|
||||
'idCuestionario'
|
||||
);
|
||||
const data = {};
|
||||
|
||||
if (!!body.titulo) {
|
||||
data.titulo = validar.validacionBasicaStr(body.titulo, 'titulo', true, 100);
|
||||
}
|
||||
if (!!body.descripcion) {
|
||||
data.descripcion = validar.validacionBasicaStr(
|
||||
body.descripcion,
|
||||
'descripcion',
|
||||
false,
|
||||
100
|
||||
);
|
||||
}
|
||||
if (!!body.fechaInicio) {
|
||||
data.fechaInicio = validar.validarFecha(body.fechaInicio, 'fechaInicio');
|
||||
}
|
||||
if (!!body.fechaFin) {
|
||||
data.fechaFin = validar.validarFecha(body.fechaFin, 'fechaFin');
|
||||
}
|
||||
|
||||
return Cuestionario.findOne({ where: { idCuestionario } })
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe este cuestionario.');
|
||||
return Cuestionario.update(
|
||||
{
|
||||
titulo: data.titulo,
|
||||
descripcion: data.descripcion,
|
||||
fechaInicio: data.fechaInicio,
|
||||
fechaFin: data.fechaFin,
|
||||
},
|
||||
{ where: { idCuestionario } }
|
||||
);
|
||||
})
|
||||
.then((res) => ({
|
||||
message: 'Se actualizó correctamente el cuestionario.',
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = editar;
|
||||
@@ -0,0 +1,18 @@
|
||||
const Cuestionario = require('../../db/tablas/Cuestionario');
|
||||
const validar = require('../../helper/validar');
|
||||
|
||||
const traerPorId = async (query) => {
|
||||
const idCuestionario = validar.validarNumeroEntero(
|
||||
query.idCuestionario,
|
||||
'idCuestionario'
|
||||
);
|
||||
|
||||
return Cuestionario.findOne({
|
||||
where: { idCuestionario }
|
||||
}).then(res => {
|
||||
if(!res) throw new Error("No hay ningun cuestionario con este id.")
|
||||
return res;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = traerPorId;
|
||||
@@ -0,0 +1,5 @@
|
||||
const Cuestionario = require('../../db/tablas/Cuestionario');
|
||||
|
||||
const traerTodos = async () => Cuestionario.findAll();
|
||||
|
||||
module.exports = traerTodos;
|
||||
@@ -0,0 +1,19 @@
|
||||
const validar = require(`../../helper/validar`);
|
||||
const Pregunta = require(`../../db/tablas/Pregunta`);
|
||||
const Opcion = require(`../../db/tablas/Opcion`);
|
||||
|
||||
const crear = async (body) => {
|
||||
const idPregunta = validar.validarNumeroEntero(body.idPregunta, 'idPregunta');
|
||||
const opcion = validar.validacionBasicaStr(body.opcion, 'opcion', false, 256);
|
||||
|
||||
return Pregunta.findOne({ where: { idPregunta } }).then(async (res) => {
|
||||
if (!res) throw new Error('Esta pregunta no existe en la db');
|
||||
|
||||
return Opcion.create({
|
||||
idPregunta,
|
||||
opcion,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = crear;
|
||||
@@ -0,0 +1,23 @@
|
||||
const Opcion = require(`../../db/tablas/Opcion`);
|
||||
const validar = require(`../../helper/validar`);
|
||||
|
||||
const editar = async (body) => {
|
||||
const idOpcion = validar.validarNumeroEntero(body.idOpcion, 'idOpcion');
|
||||
const opcion = validar.validacionBasicaStr(body.opcion, 'opcion', false, 256);
|
||||
|
||||
return Opcion.findOne({ where: { idOpcion } })
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe esta opción.');
|
||||
return Opcion.update(
|
||||
{
|
||||
opcion,
|
||||
},
|
||||
{ where: { idOpcion } }
|
||||
);
|
||||
})
|
||||
.then((res) => ({
|
||||
message: 'Se actualizó correctamente la opción.',
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = editar;
|
||||
@@ -0,0 +1,18 @@
|
||||
const Opcion = require('../../db/tablas/Opcion');
|
||||
const validar = require('../../helper/validar');
|
||||
|
||||
const traerPoridPregunta = async (query) => {
|
||||
const idPregunta = validar.validarNumeroEntero(
|
||||
query.idPregunta,
|
||||
'idPregunta'
|
||||
);
|
||||
|
||||
return Opcion.findAll({
|
||||
where: { idPregunta }
|
||||
}).then(res => {
|
||||
if(!res) throw new Error("No hay ninguna Opcion con este idPregunta.")
|
||||
return res;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = traerPoridPregunta;
|
||||
@@ -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;
|
||||
@@ -0,0 +1,30 @@
|
||||
const validar = require(`../../helper/validar`);
|
||||
const Seccion = require(`../../db/tablas/Seccion`);
|
||||
const Cuestionario = require(`../../db/tablas/Cuestionario`);
|
||||
|
||||
const crear = async (body) => {
|
||||
const idCuestionario = validar.validarNumeroEntero(
|
||||
body.idCuestionario,
|
||||
'idCuestionario'
|
||||
);
|
||||
const titulo = validar.validacionBasicaStr(body.titulo, 'titulo', true, 100);
|
||||
const descripcion = validar.validacionBasicaStr(
|
||||
body.descripcion,
|
||||
'descripcion',
|
||||
false,
|
||||
100
|
||||
);
|
||||
|
||||
return Cuestionario.findOne({ where: { idCuestionario } }).then(
|
||||
async (res) => {
|
||||
if (!res) throw new Error('Este cuestionario no existe en la db');
|
||||
return Seccion.create({
|
||||
idCuestionario,
|
||||
titulo,
|
||||
descripcion,
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = crear;
|
||||
@@ -0,0 +1,39 @@
|
||||
const Seccion = require(`../../db/tablas/Seccion`);
|
||||
const validar = require(`../../helper/validar`);
|
||||
|
||||
const editar = async (body) => {
|
||||
const idSeccion = validar.validarNumeroEntero(
|
||||
body.idSeccion,
|
||||
'idSeccion'
|
||||
);
|
||||
const data = {};
|
||||
|
||||
if (!!body.titulo) {
|
||||
data.titulo = validar.validacionBasicaStr(body.titulo, 'titulo', true, 100);
|
||||
}
|
||||
if (!!body.descripcion) {
|
||||
data.descripcion = validar.validacionBasicaStr(
|
||||
body.descripcion,
|
||||
'descripcion',
|
||||
false,
|
||||
100
|
||||
);
|
||||
}
|
||||
|
||||
return Seccion.findOne({ where: { idSeccion } })
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe esta sección.');
|
||||
return Seccion.update(
|
||||
{
|
||||
titulo: data.titulo,
|
||||
descripcion: data.descripcion,
|
||||
},
|
||||
{ where: { idSeccion } }
|
||||
);
|
||||
})
|
||||
.then((res) => ({
|
||||
message: 'Se actualizó correctamente la sección.',
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = editar;
|
||||
@@ -0,0 +1,18 @@
|
||||
const Seccion = require('../../db/tablas/Seccion');
|
||||
const validar = require('../../helper/validar');
|
||||
|
||||
const traerPorIdCuestionario = async (query) => {
|
||||
const idCuestionario = validar.validarNumeroEntero(
|
||||
query.idCuestionario,
|
||||
'idCuestionario'
|
||||
);
|
||||
|
||||
return Seccion.findAll({
|
||||
where: { idCuestionario }
|
||||
}).then(res => {
|
||||
if(!res) throw new Error("No hay ninguna sección con este idCuestionario.")
|
||||
return res;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = traerPorIdCuestionario;
|
||||
@@ -0,0 +1,17 @@
|
||||
const TipoCuestionario = require('../../db/tablas/TipoCuestionario');
|
||||
const DirigidoA = require('../../db/tablas/DirigidoA');
|
||||
const validar = require('../../helper/validar');
|
||||
|
||||
const traerPorId = (query) => {
|
||||
const idTipoCuestionario = validar.validarNumeroEntero(
|
||||
query.idTipoCuestionario,
|
||||
'idTipoCuestionario'
|
||||
);
|
||||
|
||||
return TipoCuestionario.findOne({
|
||||
where: { idTipoCuestionario },
|
||||
include: [{ model: DirigidoA }],
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = traerPorId;
|
||||
@@ -0,0 +1,7 @@
|
||||
const TipoCuestionario = require('../../db/tablas/TipoCuestionario');
|
||||
const DirigidoA = require('../../db/tablas/DirigidoA');
|
||||
|
||||
const traerTodos = () =>
|
||||
TipoCuestionario.findAll({ include: [{ model: DirigidoA }] });
|
||||
|
||||
module.exports = traerTodos;
|
||||
Reference in New Issue
Block a user