70 lines
2.1 KiB
JavaScript
70 lines
2.1 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 Programa = require(`${dbPath}/Programa`);
|
|
const Servicio = require(`${dbPath}/Servicio`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const serviciosResponsable = async (body) => {
|
|
const pagina = validar.validarNumero(
|
|
body.pagina,
|
|
'página',
|
|
false,
|
|
null,
|
|
true,
|
|
true
|
|
);
|
|
const idUsuario = validar.validarNumeroEntero(body.idUsuario, 'id usuario');
|
|
const where = body.idStatus
|
|
? { idStatus: validar.validarNumeroEntero(body.idStatus, 'id status') }
|
|
: { idStatus: { [Op.ne]: 10 } };
|
|
|
|
const nombre = body.nombre
|
|
? validar.validarTexto(body.nombre, 'nombre', true, 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 }, attributes: [] },
|
|
{
|
|
model: Usuario,
|
|
where: {
|
|
[Op.and]: [
|
|
{ usuario: { [Op.like]: `%${numeroCuenta}%` } },
|
|
{ nombre: { [Op.like]: `%${nombre}%` } },
|
|
],
|
|
},
|
|
attributes: ['idUsuario', 'usuario', 'nombre'],
|
|
},
|
|
{ model: Carrera },
|
|
{ model: Status, where },
|
|
],
|
|
attributes: [
|
|
'idServicio',
|
|
'fechaInicio',
|
|
'fechaFin',
|
|
'cartaTermino',
|
|
'createdAt',
|
|
'idCuestionarioPrograma',
|
|
],
|
|
order: [['updatedAt', 'DESC']],
|
|
limit: 25,
|
|
offset: 25 * (pagina - 1),
|
|
});
|
|
})
|
|
.then((res) => ({ count: res.count, serviciosResponsable: res.rows }));
|
|
};
|
|
|
|
module.exports = serviciosResponsable;
|