Files
2021-10-28 16:53:53 -05:00

56 lines
1.8 KiB
JavaScript

const { Op } = require('sequelize');
const { convertArrayToCSV } = require('convert-array-to-csv');
const moment = require('moment');
const helperPath = '../../helper';
const helper = require(`${helperPath}/helper`);
const encriptar = require(`${helperPath}/encriptar`);
const dbPath = '../../db/tablas';
const Alumno = require(`${dbPath}/Alumno`);
const Carrera = require(`${dbPath}/Carrera`);
const Horario = require(`${dbPath}/Horario`);
const csv = async (body) => {
const path = `server/uploads/reporte.csv`;
const data = [];
if (
!encriptar.comparar(
body.password,
'$2b$10$hYcczDkvhkcwNo7G80qr3OFsRFXU3tbNBX8Znx.88o2l3nJtOiQi6'
)
)
throw new Error('Contraseña incorrecta.');
return Alumno.findAll({
where: { idHorario: { [Op.ne]: null } },
include: [{ model: Horario }, { model: Carrera }],
})
.then(async (res) => {
for (let i = 0; i < res.length; i++) {
const horario = moment(res[i].Horario.horario);
data.push({
numeroCuenta: res[i].numeroCuenta,
carrera: res[i].Carrera.carrera,
dia: `${
horario.date() < 10 ? '0' + horario.date() : horario.date()
}/${
horario.month() + 1 < 10
? '0' + (horario.month() + 1)
: horario.month() + 1
}/${horario.year()}`,
hora: `${
horario.hour() < 10 ? '0' + horario.hour() : horario.hour()
}:${
horario.minute() < 10 ? '0' + horario.minute() : horario.minute()
}`,
asistio: res[i].asistio ? 'si' : 'no',
});
}
await helper.eliminarArchivo(path).catch((err) => {});
return helper.crearArchivo(path, convertArrayToCSV(data));
})
.then((res) => path);
};
module.exports = csv;