Skip to content

Commit

Permalink
registro de estudantes
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskisoba committed Dec 11, 2023
1 parent 3d837ab commit 69cebc7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
35 changes: 21 additions & 14 deletions backend/controllers/ExtractStudents.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
12 changes: 12 additions & 0 deletions backend/controllers/UserControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 69cebc7

Please sign in to comment.