paginacion con find and caount all
2
This commit is contained in:
@@ -8,7 +8,7 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
|
||||
const serviciosAdmin = async (body) => {
|
||||
const pagina = validar.validarId(body.pagina);
|
||||
const pagina = validar.validarNumero(body.pagina, true);
|
||||
const where = body.idStatus
|
||||
? { idStatus: validar.validarId(body.idStatus) }
|
||||
: {};
|
||||
@@ -18,9 +18,8 @@ const serviciosAdmin = async (body) => {
|
||||
const numeroCuenta = body.numeroCuenta
|
||||
? validar.validarNumeroCuenta(body.numeroCuenta)
|
||||
: '';
|
||||
const data = [];
|
||||
|
||||
return Servicio.findAll({
|
||||
return Servicio.findAndCountAll({
|
||||
include: [
|
||||
{
|
||||
model: Usuario,
|
||||
@@ -34,27 +33,13 @@ const serviciosAdmin = async (body) => {
|
||||
{ model: Status, where },
|
||||
],
|
||||
order: [['updatedAt', 'DESC']],
|
||||
limit: 25,
|
||||
offset: 25 * (pagina - 1),
|
||||
}).then((res) => {
|
||||
for (let i = pagina * 25 - 25; i < res.length && i < pagina * 25; i++)
|
||||
data.push({
|
||||
idServicio: res[i].idServicio,
|
||||
fechaInicio: res[i].fechaInicio,
|
||||
fechaFin: res[i].fechaFin,
|
||||
createdAt: res[i].createdAt,
|
||||
Usuario: {
|
||||
idUsuario: res[i].Usuario.idUsuario,
|
||||
usuario: res[i].Usuario.usuario,
|
||||
nombre: res[i].Usuario.nombre,
|
||||
TipoUsuario: res[i].Usuario.TipoUsuario,
|
||||
},
|
||||
Carrera: res[i].Carrera,
|
||||
Status: res[i].Status,
|
||||
});
|
||||
return {
|
||||
registros: res.length,
|
||||
faltantes: res.length - data.length - (pagina - 1) * 25,
|
||||
servicios: data,
|
||||
};
|
||||
for (let i = 0; i < res.rows.length; i++) {
|
||||
// delete res.rows[i].dataValues.idUsuario
|
||||
}
|
||||
return { count: res.count, serviciosAdmin: res.rows };
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
|
||||
const serviciosResponsable = async (body) => {
|
||||
const pagina = validar.validarId(body.pagina);
|
||||
const pagina = validar.validarNumero(body.pagina, true);
|
||||
const idUsuario = validar.validarId(body.idUsuario);
|
||||
const where = body.idStatus
|
||||
? { idStatus: validar.validarId(body.idStatus) }
|
||||
@@ -21,14 +21,13 @@ const serviciosResponsable = async (body) => {
|
||||
const numeroCuenta = body.numeroCuenta
|
||||
? validar.validarNumeroCuenta(body.numeroCuenta)
|
||||
: '';
|
||||
const data = [];
|
||||
|
||||
return Usuario.findOne({ where: { idUsuario } })
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe este usuario.');
|
||||
if (res.idTipoUsuario !== 2)
|
||||
throw new Error('No es un usuario tipo responsable.');
|
||||
return Servicio.findAll({
|
||||
return Servicio.findAndCountAll({
|
||||
include: [
|
||||
{ model: Programa, where: { idUsuario } },
|
||||
{
|
||||
@@ -43,30 +42,15 @@ const serviciosResponsable = async (body) => {
|
||||
{ model: Status, where },
|
||||
],
|
||||
order: [['updatedAt', 'DESC']],
|
||||
limit: 25,
|
||||
offset: 25 * (pagina - 1),
|
||||
});
|
||||
})
|
||||
.then((res) => {
|
||||
for (let i = pagina * 25 - 25; i < res.length && i < pagina * 25; i++)
|
||||
data.push({
|
||||
idServicio: res[i].idServicio,
|
||||
fechaInicio: res[i].fechaInicio,
|
||||
fechaFin: res[i].fechaFin,
|
||||
cartaTermino: res[i].cartaTermino,
|
||||
idCuestionarioPrograma: res[i].idCuestionarioPrograma,
|
||||
Usuario: {
|
||||
idUsuario: res[i].Usuario.idUsuario,
|
||||
usuario: res[i].Usuario.usuario,
|
||||
nombre: res[i].Usuario.nombre,
|
||||
TipoUsuario: res[i].Usuario.TipoUsuario,
|
||||
},
|
||||
Carrera: res[i].Carrera,
|
||||
Status: res[i].Status,
|
||||
});
|
||||
return {
|
||||
registros: res.length,
|
||||
faltantes: res.length - data.length - (pagina - 1) * 25,
|
||||
servicios: data,
|
||||
};
|
||||
for (let i = 0; i < res.rows.length; i++) {
|
||||
// delete res.rows[i].dataValues.idUsuario
|
||||
}
|
||||
return { count: res.count, serviciosResponsable: res.rows };
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user