Files
2021-11-02 15:18:42 -06:00

52 lines
1.5 KiB
JavaScript

const csv = require('csvtojson');
const dbPath = './tablas';
const Carrera = require(`${dbPath}/Carrera`);
const Alumno = require(`${dbPath}/Alumno`);
const cargaMasiva = async () => {
const path = `server/db/generacion2021.csv`;
await csv()
.fromFile(path)
.then(async (alumnos) => {
for (let i = 0; i < alumnos.length; i++) {
const carrera = await Carrera.findOne({
where: { carrera: alumnos[i].nombreCarrera },
}).then((res) => {
if (!res)
return Carrera.create({ carrera: alumnos[i].nombreCarrera });
return res;
});
await Alumno.findOne({
where: { numeroCuenta: alumnos[i].numeroCuenta },
}).then(async (res) => {
if (res)
console.log(
`El alumno ${alumnos[i].numeroCuenta} ya existe. Linea ${i + 2}.`
);
else {
if (alumnos[i].inscrito != 'SI')
console.log(
`El alumno ${alumnos[i].numeroCuenta} no esta inscrito.`
);
else
await Alumno.create({
numeroCuenta: alumnos[i].numeroCuenta,
turno: alumnos[i].turno,
idCarrera: carrera.idCarrera,
}).then((res) => {
console.log(
`Se creo al alumno ${res.numeroCuenta} correctamente. Linea ${
i + 2
}.`
);
});
}
});
}
});
};
cargaMasiva();