60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
const moment = require('moment');
|
|
const { Op } = require('sequelize');
|
|
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
const Servicio = require(`${dbPath}/Servicio`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const serviciosAdmin = async (body) => {
|
|
const pagina = validar.validarNumero(body.pagina, true);
|
|
const where = body.idStatus
|
|
? { idStatus: validar.validarId(body.idStatus) }
|
|
: {};
|
|
const nombre = body.nombre
|
|
? validar.validarTexto(body.nombre, 'El nombre', 60)
|
|
: '';
|
|
const numeroCuenta = body.numeroCuenta
|
|
? validar.validarNumeroCuenta(body.numeroCuenta)
|
|
: '';
|
|
|
|
return Servicio.findAndCountAll({
|
|
include: [
|
|
{
|
|
model: Usuario,
|
|
where: {
|
|
usuario: { [Op.like]: `%${numeroCuenta}%` },
|
|
nombre: { [Op.like]: `%${nombre}%` },
|
|
},
|
|
},
|
|
{ model: Carrera },
|
|
{ model: Status, where },
|
|
],
|
|
order: [['updatedAt', 'DESC']],
|
|
limit: 25,
|
|
offset: 25 * (pagina - 1),
|
|
}).then((res) => {
|
|
let data = [];
|
|
|
|
for (let i = 0; i < res.rows.length; i++) {
|
|
data.push({
|
|
idServicio: res.rows[i].idServicio,
|
|
fechaInicio: moment(res.rows[i].fechaInicio).format(),
|
|
fechaFin: moment(res.rows[i].fechaFin).format(),
|
|
createdAt: moment(res.rows[i].fechaFin).format(),
|
|
Usuario: {
|
|
idUsuario: res.rows[i].Usuario.idUsuario,
|
|
usuario: res.rows[i].Usuario.usuario,
|
|
nombre: res.rows[i].Usuario.nombre,
|
|
},
|
|
Carrera: res.rows[i].Carrera,
|
|
Status: res.rows[i].Status,
|
|
});
|
|
}
|
|
return { count: res.count, serviciosAdmin: data };
|
|
});
|
|
};
|
|
|
|
module.exports = serviciosAdmin;
|