Files
servicio_social_api/server/controller/Servicio/serviciosAdmin.js
T

60 lines
1.8 KiB
JavaScript
Raw 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) => {
2021-05-05 19:16:15 -05:00
const pagina = validar.validarNumero(body.pagina, true);
2021-01-14 03:13:37 -06:00
const where = body.idStatus
? { idStatus: validar.validarId(body.idStatus) }
2021-02-16 15:44:19 -06:00
: {};
2021-01-14 03:13:37 -06:00
const nombre = body.nombre
2020-11-27 12:36:53 -06:00
? validar.validarTexto(body.nombre, 'El nombre', 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: {
usuario: { [Op.like]: `%${numeroCuenta}%` },
nombre: { [Op.like]: `%${nombre}%` },
},
},
{ 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(),
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,
});
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;