58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
const { Op } = require('sequelize');
|
|
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
const Servicio = require(`${dbPath}/Servicio`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const serviciosResponsable = async (body) => {
|
|
const pagina = validar.validarNumero(body.pagina, true);
|
|
const idUsuario = validar.validarId(body.idUsuario);
|
|
const where = body.idStatus
|
|
? { idStatus: validar.validarId(body.idStatus) }
|
|
: { idStatus: { [Op.ne]: 10 } };
|
|
|
|
const nombre = body.nombre
|
|
? validar.validarTexto(body.nombre, 'El nombre', 60)
|
|
: '';
|
|
const numeroCuenta = body.numeroCuenta
|
|
? validar.validarNumeroCuenta(body.numeroCuenta)
|
|
: '';
|
|
|
|
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.findAndCountAll({
|
|
include: [
|
|
{ model: Programa, where: { idUsuario } },
|
|
{
|
|
model: Usuario,
|
|
include: { model: TipoUsuario },
|
|
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) => {
|
|
for (let i = 0; i < res.rows.length; i++) {
|
|
// delete res.rows[i].dataValues.idUsuario
|
|
}
|
|
return { count: res.count, serviciosResponsable: res.rows };
|
|
});
|
|
};
|
|
|
|
module.exports = serviciosResponsable;
|