From b42a88e9ac5016636e9d756ff2bcec4a6fe90ffa Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Sat, 23 Oct 2021 00:23:17 -0500 Subject: [PATCH] carga masiva --- .gitignore | 2 ++ server/db/cargaMasiva.js | 49 ++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 93219f4..fdce87b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ node_modules/ server/db/alumnos_segundo.csv server/uploads/* + +server/db/generacion2021.csv \ No newline at end of file diff --git a/server/db/cargaMasiva.js b/server/db/cargaMasiva.js index b098251..ce0da20 100644 --- a/server/db/cargaMasiva.js +++ b/server/db/cargaMasiva.js @@ -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}.` - ); } }); };