From 69cebc79a65b5416543652b63ddd8839b68c409b Mon Sep 17 00:00:00 2001 From: Yasmim Rosa Date: Mon, 11 Dec 2023 10:02:57 -0300 Subject: [PATCH] registro de estudantes --- backend/controllers/ExtractStudents.js | 35 +++++++++++++++----------- backend/controllers/UserControllers.js | 12 +++++++++ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/backend/controllers/ExtractStudents.js b/backend/controllers/ExtractStudents.js index ae43af9c..3a12c228 100644 --- a/backend/controllers/ExtractStudents.js +++ b/backend/controllers/ExtractStudents.js @@ -1,29 +1,36 @@ const exceljs = require('exceljs'); +const user = require('./UserControllers') exports.extractStudents = async (req, res) => { - const arquivo = req.file; + const file_input = req.file; const workbook = new exceljs.Workbook(); try { - await workbook.xlsx.load(arquivo.buffer); - const primeiraPlanilha = workbook.worksheets[0]; + await workbook.xlsx.load(file_input.buffer); + const spreadsheet = workbook.worksheets[0]; - const nomesColunas = primeiraPlanilha.getRow(1).values; + const columns = spreadsheet.getRow(1).values; - const dados = []; + const data = []; - for (let rowIndex = 2; rowIndex <= primeiraPlanilha.rowCount; rowIndex++) { - const linha = primeiraPlanilha.getRow(rowIndex); - const dadosLinha = {}; - nomesColunas.forEach((nomeColuna, columnIndex) => { - dadosLinha[nomeColuna] = linha.getCell(columnIndex + 1).value; + for (let rowIndex = 2; rowIndex <= spreadsheet.rowCount; rowIndex++) { + const row = spreadsheet.getRow(rowIndex); + const dataRow = {}; + columns.forEach((columnName, columnIndex) => { + dataRow[columnName] = row.getCell(columnIndex).value; }); - dados.push(dadosLinha); + data.push(dataRow); } - - console.log('Dados extraídos:', dados); - + data.forEach( async (student) => { + try{ + await user.studentRegister(student) + } + catch(err){ + console.log(err) + } + }); + res.json({ mensagem: 'Arquivo Excel recebido e processado com sucesso!' }); } catch (erro) { console.error('Erro ao processar arquivo Excel:', erro); diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index d02ff935..08294dcb 100644 --- a/backend/controllers/UserControllers.js +++ b/backend/controllers/UserControllers.js @@ -27,6 +27,18 @@ exports.userRegister = async (req, res) => { } }; +exports.studentRegister = async(student) => { + const hash = await bcrypt.hash(password, 15); + await Users.create({ + superuser: false, + name: student['nome completo'], + registry: student['matrícula'], + school_year: student['ano'], + email: student['email'], + password: hash, + }); +} + exports.userLogin = async (req, res) => { try { const { email, password } = req.body;