Files
citas_recorridos_api/server/controller/TercerSemestre/csv.js
T
2021-07-21 10:46:35 -05:00

60 lines
2.0 KiB
JavaScript

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 Carrera = require(`${dbPath}/Carrera`);
const Grupo = require(`${dbPath}/Grupo`);
const GrupoHorario = require(`${dbPath}/GrupoHorario`);
const Horario = require(`${dbPath}/Horario`);
const TercerSemestre = require(`${dbPath}/TercerSemestre`);
const csv = async (body) => {
const path = `server/uploads/tercer_semestre.csv`;
const data = [];
if (
!encriptar.comparar(
body.password,
'$2b$10$8FDx15tey9s9ITXNNE/Qourk1EfyrC6W8/ROqI0sGMuyOAuZ8oHMO'
)
)
throw new Error('Contraseña incorrecta.');
return TercerSemestre.findAll({
where: { deseaAsistir: true },
include: [{ model: Grupo, include: [{ model: Carrera }] }],
})
.then(async (res) => {
for (let i = 0; i < res.length; i++) {
const horario = await GrupoHorario.findOne({
where: { idGrupo: res[i].idGrupo },
include: [{ model: Horario }],
}).then((res) => moment(res.Horario.horario));
data.push({
numeroCuenta: res[i].numeroCuenta,
nombre: res[i].nombre,
carrera: res[i].Grupo.Carrera.carrera,
grupo: res[i].Grupo.grupo,
horario: `${
horario.date() < 10 ? '0' + horario.date() : horario.date()
}/${
horario.month() + 1 < 10
? '0' + (horario.month() + 1)
: horario.month() + 1
}/${horario.year()} ${
horario.hour() < 10 ? '0' + horario.hour() : horario.hour()
}:${
horario.minute() < 10 ? '0' + horario.minute() : horario.minute()
}`,
});
}
await helper.eliminarArchivo(path).catch((err) => {});
return helper.crearArchivo(path, convertArrayToCSV(data));
})
.then((res) => path);
};
module.exports = csv;