Files

49 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2021-04-05 15:24:18 -05:00
const moment = require('moment');
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 validar = require(`${helperPath}/validar`);
2021-05-05 18:49:23 -05:00
const dbPath = '../../db/tablas';
const Carrera = require(`${dbPath}/Carrera`);
const Servicio = require(`${dbPath}/Servicio`);
const Usuario = require(`${dbPath}/Usuario`);
2021-04-05 15:24:18 -05:00
const gustavoBazPrada = async (body) => {
2022-01-02 18:25:31 -06:00
const year = validar.validarNumero(body.year, 'año', true, 4);
2021-04-06 10:36:22 -05:00
const inicio = moment(`${year}-01-31`);
2021-04-06 22:00:33 -05:00
const fin = moment(`${parseInt(year) + 1}-01-31`);
2021-05-12 19:08:28 -05:00
const path = `server/uploads/${year}_gustavo_baz_prada.csv`;
2021-04-05 15:24:18 -05:00
const data = [];
return Servicio.findAll({
where: {
2021-04-06 10:06:53 -05:00
[Op.and]: [
2021-04-06 10:36:22 -05:00
{ fechaLiberacion: { [Op.gte]: inicio } },
{ fechaLiberacion: { [Op.lte]: fin } },
2021-04-06 10:06:53 -05:00
],
2021-04-05 15:24:18 -05:00
},
include: [{ model: Usuario }, { model: Carrera }],
2021-05-12 19:08:28 -05:00
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,
2021-05-13 14:06:07 -05:00
fechaLiberacion: helper.crearDDMMAAAA(res[i].fechaLiberacion),
2021-05-12 19:08:28 -05:00
});
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-04-05 15:24:18 -05: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-04-05 15:24:18 -05:00
});
};
module.exports = gustavoBazPrada;