Files

71 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2021-05-14 14:13:03 -05:00
const moment = require('moment');
2020-11-17 13:49:31 -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 Programa = require(`${dbPath}/Programa`);
const Servicio = require(`${dbPath}/Servicio`);
const Status = require(`${dbPath}/Status`);
const Usuario = require(`${dbPath}/Usuario`);
2020-11-17 13:49:31 -06:00
const serviciosResponsable = async (body) => {
2022-01-02 18:25:31 -06:00
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
2022-01-02 15:07:49 -06:00
const idUsuario = validar.validarNumeroEntero(body.idUsuario, 'id usuario');
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
: { idStatus: { [Op.ne]: 10 } };
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 13:49:31 -06:00
: '';
2021-01-14 03:13:37 -06:00
const numeroCuenta = body.numeroCuenta
2020-11-17 13:49:31 -06:00
? validar.validarNumeroCuenta(body.numeroCuenta)
: '';
return Usuario.findOne({ where: { idUsuario } })
.then((res) => {
if (!res) throw new Error('No existe este usuario.');
2020-11-19 12:23:39 -06:00
if (res.idTipoUsuario !== 2)
2020-11-17 13:49:31 -06:00
throw new Error('No es un usuario tipo responsable.');
2021-05-05 19:16:15 -05:00
return Servicio.findAndCountAll({
2020-11-17 13:49:31 -06:00
include: [
2022-01-17 18:28:01 -06:00
{ model: Programa, where: { idUsuario }, attributes: [] },
2020-11-17 13:49:31 -06:00
{
model: Usuario,
2020-11-17 17:27:12 -06:00
where: {
2021-06-07 23:52:42 -05:00
[Op.and]: [
{ usuario: { [Op.like]: `%${numeroCuenta}%` } },
{ nombre: { [Op.like]: `%${nombre}%` } },
],
2020-11-17 17:27:12 -06:00
},
2022-01-17 18:28:01 -06:00
attributes: ['idUsuario', 'usuario', 'nombre'],
2020-11-17 13:49:31 -06:00
},
{ model: Carrera },
2021-02-10 01:11:11 -06:00
{ model: Status, where },
2020-11-17 13:49:31 -06:00
],
2022-01-17 18:56:54 -06:00
attributes: [
'idServicio',
'fechaInicio',
'fechaFin',
'cartaTermino',
'createdAt',
'idCuestionarioPrograma',
'idCuestionarioPrograma2'
2022-01-17 18:56:54 -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-17 13:49:31 -06:00
});
})
2022-01-17 18:28:01 -06:00
.then((res) => ({ count: res.count, serviciosResponsable: res.rows }));
2020-11-17 13:49:31 -06:00
};
module.exports = serviciosResponsable;