Files
citas_recorridos_api/server/controller/PrimerSemestre/csv.js
T
Lemuel Marquez 407f292738 gurpo horario
2021-07-23 13:02:58 -05:00

60 lines
1.9 KiB
JavaScript

const moment = require('moment');
const { convertArrayToCSV } = require('convert-array-to-csv');
const helperPath = '../../helper';
const helper = require(`${helperPath}/helper`);
const encriptar = require(`${helperPath}/encriptar`);
const dbPath = '../../db/tablas';
const Carrera = require(`${dbPath}/Carrera`);
const Grupo = require(`${dbPath}/Grupo`);
const Horario = require(`${dbPath}/Horario`);
const PrimerSemestre = require(`${dbPath}/PrimerSemestre`);
const csv = async (body) => {
const path = `server/uploads/primer_semestre.csv`;
const data = [];
if (
!encriptar.comparar(
body.password,
'$2b$10$8FDx15tey9s9ITXNNE/Qourk1EfyrC6W8/ROqI0sGMuyOAuZ8oHMO'
)
)
throw new Error('Contraseña incorrecta.');
return PrimerSemestre.findAll({
include: [{ model: Grupo, include: [{ model: Carrera }] }],
})
.then(async (res) => {
for (let i = 0; i < res.length; i++) {
const horario = await Horario.findOne({
where: { idHorario: res[i].idHorario },
}).then((res) => moment(res.horario));
data.push({
numeroCuenta: res[i].numeroCuenta,
nombre: res[i].nombre,
carrera: res[i].Grupo.Carrera.carrera,
grupo: res[i].Grupo.grupo,
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()
}`,
correo: res[i].correo,
asistio: res[i].asistio ? 'si' : 'no',
});
}
await helper.eliminarArchivo(path).catch((err) => {});
return helper.crearArchivo(path, convertArrayToCSV(data));
})
.then((res) => path);
};
module.exports = csv;