const fs = require('fs'); const moment = require('moment'); const { convertArrayToCSV } = require('convert-array-to-csv'); const { Op } = require('sequelize'); const validar = require('../../helper/validar'); const Servicio = require('../../db/tablas/Servicio'); const Usuario = require('../../db/tablas/Usuario'); const Carrera = require('../../db/tablas/Carrera'); const gustavoBazPrada = async (body) => { const year = validar.validarNumero(body.year, 4); const path = `csv/${year}_gustavo_baz_prada.csv`; const data = []; return Servicio.findAll({ where: { [Op.and]: { fechaLiberacion: { [Op.gte]: moment(`${year}-01-31`) }, fechaLiberacion: { [Op.lte]: moment(`${year + 1}-01-31`) }, }, }, include: [{ model: Usuario }, { model: Carrera }], }).then((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, fechaLiberacion: res[i].fechaLiberacion, }); try { fs.unlinkSync(path); } catch (err) { console.log(err); } fs.appendFile(path, convertArrayToCSV(data), (err) => { if (err) throw new Error(err); }); return { message: `Se creo/actualizo correctamente el csv del reporte Gustavo Baz Prada.`, }; }); }; module.exports = gustavoBazPrada;