Files

62 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2021-03-01 11:13:22 -06:00
const { convertArrayToCSV } = require('convert-array-to-csv');
const { Op } = require('sequelize');
2021-05-13 14:06:07 -05:00
const helperPath = '../../helper';
const helper = require(`${helperPath}/helper`);
const { validarFecha } = require(`${helperPath}/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`);
2021-03-01 11:13:22 -06:00
2021-04-05 15:24:18 -05:00
const reporte = async (body) => {
2022-01-02 17:56:42 -06:00
const inicio = validarFecha(body.inicio, 'fecha de inicio', false);
const fin = validarFecha(body.fin, 'fecha fin', false);
2021-05-12 19:08:28 -05:00
const path = `server/uploads/reporte.csv`;
2021-03-01 11:13:22 -06:00
const data = [];
return Servicio.findAll({
where: {
2021-04-06 10:06:53 -05:00
[Op.and]: [
2021-05-12 19:08:28 -05:00
{ fechaInicio: { [Op.gte]: inicio.format() } },
{ fechaInicio: { [Op.lte]: fin.format() } },
2021-04-06 10:06:53 -05:00
],
2021-03-01 11:13:22 -06:00
},
2021-04-06 10:13:37 -05:00
include: [
{ model: Programa },
{ model: Usuario },
{ model: Carrera },
{ model: Status },
],
2021-05-12 19:08:28 -05:00
order: [['fechaInicio']],
})
.then(async (res) => {
for (let i = 0; i < res.length; i++)
data.push({
numeroCuenta: res[i].Usuario.usuario,
nombre: res[i].Usuario.nombre,
licenciatura: res[i].Carrera.carrera,
correo: res[i].correo,
2021-05-13 14:06:07 -05:00
fechaInicio: helper.crearDDMMAAAA(res[i].fechaInicio),
fechaFin: helper.crearDDMMAAAA(res[i].fechaFin),
fechaLiberacion: helper.crearDDMMAAAA(res[i].fechaLiberacion),
2021-05-12 19:08:28 -05:00
institucion: res[i].Programa.institucion,
dependencia: res[i].Programa.dependencia,
programa: res[i].Programa.programa,
profesor: res[i].profesor,
clave: res[i].Programa.clavePrograma,
status: res[i].Status.status,
});
2021-05-13 14:06:07 -05:00
await helper.eliminarArchivo(path).catch((err) => {
2021-05-12 19:08:28 -05:00
console.log(err);
2021-03-01 11:13:22 -06:00
});
2021-05-13 14:06:07 -05:00
return helper.crearArchivo(path, convertArrayToCSV(data));
2021-05-12 19:08:28 -05:00
})
.then((res) => {
return path;
2021-03-01 11:13:22 -06:00
});
};
2021-04-05 15:24:18 -05:00
module.exports = reporte;