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

58 lines
1.9 KiB
JavaScript
Raw Normal View History

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 TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
2020-11-17 13:49:31 -06:00
const serviciosResponsable = 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 idUsuario = validar.validarId(body.idUsuario);
const where = body.idStatus
? { idStatus: validar.validarId(body.idStatus) }
2021-02-16 15:44:19 -06:00
: { idStatus: { [Op.ne]: 10 } };
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 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,
include: { model: TipoUsuario },
2020-11-17 17:27:12 -06:00
where: {
usuario: { [Op.like]: `%${numeroCuenta}%` },
nombre: { [Op.like]: `%${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
],
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-05 19:16:15 -05:00
for (let i = 0; i < res.rows.length; i++) {
// delete res.rows[i].dataValues.idUsuario
}
return { count: res.count, serviciosResponsable: res.rows };
2020-11-17 13:49:31 -06:00
});
};
module.exports = serviciosResponsable;