Files
servicio_social_api/server/controller/Servicio/gustavoBazPrada.js
T
2021-04-12 13:37:50 -05:00

49 lines
1.5 KiB
JavaScript

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 inicio = moment(`${year}-01-31`);
const fin = moment(`${parseInt(year) + 1}-01-31`);
const path = `csv/${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 }],
}).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: validar.crearDDMMAAAA(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;