Files

69 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2021-05-14 19:31:12 -05:00
const moment = require('moment');
2020-11-17 11:39:39 -06:00
const { Op } = require('sequelize');
const validar = require('../../helper/validar');
2021-05-05 18:49:23 -05:00
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) => {
2022-01-02 18:25:31 -06:00
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
2021-01-14 03:13:37 -06:00
const where = body.idStatus
2022-01-02 15:07:49 -06:00
? { idStatus: validar.validarNumeroEntero(body.idStatus, 'id status') }
2021-02-16 15:44:19 -06:00
: {};
2021-01-14 03:13:37 -06:00
const nombre = body.nombre
2022-01-02 15:27:30 -06:00
? validar.validarTexto(body.nombre, 'nombre', true, 60)
2020-11-17 11:39:39 -06:00
: '';
2021-01-14 03:13:37 -06:00
const numeroCuenta = body.numeroCuenta
2020-11-17 11:39:39 -06:00
? validar.validarNumeroCuenta(body.numeroCuenta)
: '';
2021-05-05 19:16:15 -05:00
return Servicio.findAndCountAll({
2020-11-27 20:26:24 -06:00
include: [
{
model: Usuario,
where: {
2021-05-25 13:06:57 -05:00
[Op.and]: [
{ usuario: { [Op.like]: `%${numeroCuenta}%` } },
{ nombre: { [Op.like]: `%${nombre}%` } },
],
2020-11-27 20:26:24 -06:00
},
},
{ model: Carrera },
2021-02-10 01:11:11 -06:00
{ model: Status, where },
2020-11-27 20:26:24 -06:00
],
2021-01-13 12:00:11 -06:00
order: [['updatedAt', 'DESC']],
2021-05-05 19:16:15 -05:00
limit: 25,
offset: 25 * (pagina - 1),
2020-11-27 20:26:24 -06:00
}).then((res) => {
2021-05-14 19:31:12 -05:00
let data = [];
2021-05-05 19:16:15 -05:00
for (let i = 0; i < res.rows.length; i++) {
2021-05-14 19:31:12 -05:00
data.push({
idServicio: res.rows[i].idServicio,
fechaInicio: moment(res.rows[i].fechaInicio).format(),
fechaFin: moment(res.rows[i].fechaFin).format(),
2021-11-29 00:46:38 -06:00
createdAt: moment(res.rows[i].createdAt).format(),
2021-05-14 19:31:12 -05:00
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,
});
2021-05-05 19:16:15 -05:00
}
2021-05-14 19:31:12 -05:00
return { count: res.count, serviciosAdmin: data };
2020-11-27 20:26:24 -06:00
});
};
module.exports = serviciosAdmin;