Files
citas_recorridos_api/server/db/cargaMasiva.js
T
2021-10-23 00:23:17 -05:00

49 lines
1.4 KiB
JavaScript

const csv = require('csvtojson');
const { Op } = require('sequelize');
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 {
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
}.`
);
if (alumnos[i].inscrito != 'SI') console.log(`No esta inscrito.`);
});
}
});
}
});
};
cargaMasiva();