Files

81 lines
2.5 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: [
{ model: Programa, where: { idUsuario } },
{
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
},
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
],
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
});
})
.then((res) => {
2021-05-14 14:13:03 -05:00
let data = [];
2021-05-05 19:16:15 -05:00
for (let i = 0; i < res.rows.length; i++) {
2021-05-14 14:13:03 -05:00
data.push({
idServicio: res.rows[i].idServicio,
fechaInicio: moment(res.rows[i].fechaInicio).format(),
fechaFin: moment(res.rows[i].fechaFin).format(),
cartaTermino: res.rows[i].cartaTermino,
idCuestionarioPrograma: res.rows[i].idCuestionarioPrograma,
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 14:13:03 -05:00
return { count: res.count, serviciosResponsable: data };
2020-11-17 13:49:31 -06:00
});
};
module.exports = serviciosResponsable;