Files
servicio_social_api/server/controller/Servicio/gustavoBazPrada.js
T

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-04-05 15:24:18 -05:00
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);
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-04-05 15:24:18 -05:00
const path = `csv/${year}_gustavo_baz_prada.csv`;
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 }],
}).then((res) => {
for (let i = 0; i < res.length; i++)
data.push({
2021-04-06 10:09:37 -05:00
numeroCuenta: res[i].Usuario.usuario,
2021-04-05 15:24:18 -05:00
nombre: res[i].Usuario.nombre,
licenciatura: res[i].Carrera.carrera,
correo: res[i].correo,
2021-04-12 13:37:50 -05:00
fechaLiberacion: validar.crearDDMMAAAA(res[i].fechaLiberacion),
2021-04-05 15:24:18 -05:00
});
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;