const { convertArrayToCSV } = require('convert-array-to-csv'); const { Op } = require('sequelize'); const helperPath = '../../helper'; const helper = require(`${helperPath}/helper`); const { validarFecha } = require(`${helperPath}/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 reporte = async (body) => { const inicio = validarFecha(body.inicio, 'fecha de inicio', false); const fin = validarFecha(body.fin, 'fecha fin', false); const path = `server/uploads/reporte.csv`; const data = []; return Servicio.findAll({ where: { [Op.and]: [ { fechaInicio: { [Op.gte]: inicio.format() } }, { fechaInicio: { [Op.lte]: fin.format() } }, ], }, include: [ { model: Programa }, { model: Usuario }, { model: Carrera }, { model: Status }, ], 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, fechaInicio: helper.crearDDMMAAAA(res[i].fechaInicio), fechaFin: helper.crearDDMMAAAA(res[i].fechaFin), fechaLiberacion: helper.crearDDMMAAAA(res[i].fechaLiberacion), 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, }); await helper.eliminarArchivo(path).catch((err) => { console.log(err); }); return helper.crearArchivo(path, convertArrayToCSV(data)); }) .then((res) => { return path; }); }; module.exports = reporte;