Files
2022-01-02 18:25:31 -06:00

49 lines
1.6 KiB
JavaScript

const moment = require('moment');
const { convertArrayToCSV } = require('convert-array-to-csv');
const { Op } = require('sequelize');
const helperPath = '../../helper';
const helper = require(`${helperPath}/helper`);
const validar = require(`${helperPath}/validar`);
const dbPath = '../../db/tablas';
const Carrera = require(`${dbPath}/Carrera`);
const Servicio = require(`${dbPath}/Servicio`);
const Usuario = require(`${dbPath}/Usuario`);
const gustavoBazPrada = async (body) => {
const year = validar.validarNumero(body.year, 'año', true, 4);
const inicio = moment(`${year}-01-31`);
const fin = moment(`${parseInt(year) + 1}-01-31`);
const path = `server/uploads/${year}_gustavo_baz_prada.csv`;
const data = [];
return Servicio.findAll({
where: {
[Op.and]: [
{ fechaLiberacion: { [Op.gte]: inicio } },
{ fechaLiberacion: { [Op.lte]: fin } },
],
},
include: [{ model: Usuario }, { model: Carrera }],
order: [['fechaLiberacion']],
})
.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,
fechaLiberacion: helper.crearDDMMAAAA(res[i].fechaLiberacion),
});
await helper.eliminarArchivo(path).catch((err) => {
console.log(err);
});
return helper.crearArchivo(path, convertArrayToCSV(data));
})
.then((res) => {
return path;
});
};
module.exports = gustavoBazPrada;