carga masiva

This commit is contained in:
xXpuma99Xx
2021-10-23 00:23:17 -05:00
parent 64b94de562
commit b42a88e9ac
2 changed files with 31 additions and 20 deletions
+2
View File
@@ -5,3 +5,5 @@ node_modules/
server/db/alumnos_segundo.csv
server/uploads/*
server/db/generacion2021.csv
+29 -20
View File
@@ -2,36 +2,45 @@ const csv = require('csvtojson');
const { Op } = require('sequelize');
const dbPath = './tablas';
const Carrera = require(`${dbPath}/Carrera`);
const Grupo = require(`${dbPath}/Grupo`);
const TercerSemestre = require(`${dbPath}/TercerSemestre`);
const Alumno = require(`${dbPath}/Alumno`);
const cargaMasiva = async () => {
const path = `server/db/alumnos_segundo.csv`;
const path = `server/db/generacion2021.csv`;
await csv()
.fromFile(path)
.then(async (alumnos) => {
for (let i = 0; i < alumnos.length; i++) {
// for (let i = 0; i < 1; i++) {
const grupo = await Grupo.findOne({
where: { grupo: alumnos[i].grupo },
include: [{ model: Carrera, where: { carrera: alumnos[i].carrera } }],
const carrera = await Carrera.findOne({
where: { carrera: alumnos[i].nombreCarrera },
}).then((res) => {
if (!res)
return Carrera.create({ carrera: alumnos[i].nombreCarrera });
return res;
});
if (!grupo) {
console.log(
`No exite la carrera o el grupo del alumno ${alumnos[i].cuenta}.`
);
continue;
}
await TercerSemestre.create({
numeroCuenta: alumnos[i].cuenta,
nombre: alumnos[i].nombre,
idGrupo: grupo.idGrupo,
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.`);
});
}
});
console.log(
`Se añadio al alumno ${alumnos[i].cuenta} al grupo ${grupo.grupo} con id ${grupo.idGrupo}.`
);
}
});
};