Skip to content

Commit

Permalink
Merge pull request #9 from jhonatancunha/development
Browse files Browse the repository at this point in the history
Subindo versão estável com login com Google 

Versão 2.0.0
  • Loading branch information
iagocarmona authored Jun 13, 2023
2 parents 3feefca + b3252c2 commit 1bb2c5b
Show file tree
Hide file tree
Showing 92 changed files with 9,884 additions and 6,867 deletions.
7 changes: 0 additions & 7 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
APP_URL=http://localhost:3333


DB_HOST="HOST_AQUI"
DB_PORT="PORTA_MYSQL_AQUI"
DB_USER="USUARIO_MYSQL_AQUI"
Expand All @@ -12,11 +11,5 @@ SECRET="SECRET_AQUI"
EXPIRE_IN="DIAS_PARA_EXPIRAR_TOKEN"
TIMEZONE="+03:00"


# LDAP CONFIG
LDAP_URL="URL_LDAP_AQUI"
LDAP_USERNAME="USUARIO_LDAP"
LDAP_PASSWORD="SENHA_LDAP"

NODE_ENV=development
PORT=3333
Empty file modified backend/drop_database.sh
100644 → 100755
Empty file.
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"dev:debug": "nodemon --inspect src/server",
"debug": "nodemon --inspect src/server",
"test": "jest",
"migrate": "npx sequelize-cli db:migrate",
"migrate": "npx sequelize-cli migration:generate",
"migrate:run": "npx sequelize-cli db:migrate",
"migrate:reset": "npx sequelize-cli db:migrate:undo:all && npm run migrate",
"pretest": "cross-env NODE_ENV=test npm run migrate:reset"
},
Expand Down Expand Up @@ -45,6 +46,7 @@
"eslint-plugin-import": "2.24.2",
"eslint-plugin-prettier": "4.0.0",
"jest": "27.5.1",
"mysql": "2.18.1",
"nodemon": "2.0.12",
"prettier": "2.4.1",
"supertest": "6.2.2"
Expand Down

This file was deleted.

25 changes: 0 additions & 25 deletions backend/src/app/controllers/Student/SessionStudentController.js

This file was deleted.

28 changes: 25 additions & 3 deletions backend/src/app/controllers/Student/StudentController.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
// SERVICES
import UpdateStudentService from '../../services/Student/UpdateStudent';
import CreateStudentService from '../../services/Student/CreateStudent';

class StudentController {
// Cadastra um único registro
async store(req, res) {
const { name, email, password } = req.body;
const { email, name, picture, isLocalImage } = req.body;
try {
const student = await CreateStudentService.execute({
name,
email,
password,
name,
picture,
isLocalImage,
});

return res.status(200).json(student);
} catch (error) {
return (
(!!error.status && res.status(error.status).json(error)) ||
res.status(500).json(error)
);
}
}

async update(req, res) {
try {
const { name, id, avatar, isLocalImage } = req.body;

const student = await UpdateStudentService.execute({
name,
id,
avatar,
isLocalImage,
});

return res.status(200).json(student);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class SessionTeacherController {
} catch (error) {
console.log(error);

if (error?.response?.data?.status === 401) {
const RAOrPasswordErrorLdap = new Error();
RAOrPasswordErrorLdap.status = 403;
RAOrPasswordErrorLdap.response =
'Dados de usuário ou senha incorretos!';
return res.status(403).json(RAOrPasswordErrorLdap);
}
// if (error?.response?.data?.status === 401) {
// const RAOrPasswordErrorLdap = new Error();
// RAOrPasswordErrorLdap.status = 403;
// RAOrPasswordErrorLdap.response =
// 'Dados de usuário ou senha incorretos!';
// return res.status(403).json(RAOrPasswordErrorLdap);
// }

return res.status(500).json(error);
}
Expand Down
5 changes: 5 additions & 0 deletions backend/src/app/models/StudentModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class Student extends Model {
password: Sequelize.VIRTUAL,
passwordHash: Sequelize.STRING,
idImage: Sequelize.INTEGER,
urlImage: Sequelize.STRING,
isLocalImage: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
},
{
sequelize,
Expand Down
13 changes: 11 additions & 2 deletions backend/src/app/models/TeacherModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ class Teacher extends Model {
static init(sequelize) {
super.init(
{
// id: {
// type: Sequelize.INTEGER,
// autoIncrement: true,
// primaryKey: true
// },
name: Sequelize.STRING,
email: Sequelize.STRING,
uid: Sequelize.STRING,
picture: Sequelize.STRING,
// uid: {
// type: Sequelize.UUID,
// defaultValue: Sequelize.UUIDV4,
// primaryKey: true,
// },
// O campo 'virtual' nao existe no db, apenas na execução
// password: Sequelize.VIRTUAL,
// passwordHash: Sequelize.STRING,
Expand All @@ -18,7 +28,6 @@ class Teacher extends Model {
underscored: true,
}
);

// this.addHook('beforeSave', async (teacher) => {
// if (teacher.password) {
// // eslint-disable-next-line no-param-reassign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Yup from 'yup';

// REPOSITORIES
import ClassRepository from '../../../repositories/Class';
import File from '../../../models/FileModel';

class GetAllStudentsFromClassService {
constructor() {
Expand Down Expand Up @@ -32,14 +31,7 @@ class GetAllStudentsFromClassService {
}

const students = await this.classRepository.getAllStudents(classInstance, {
attributes: ['id', 'name', 'ra', 'email'],
include: [
{
model: File,
as: 'imageProfile',
attributes: ['url', 'path'],
},
],
attributes: ['id', 'name', 'url_image', 'email'],
});

return students;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Yup from 'yup';

// MODELS
import StudentQuiz from '../../models/StudentQuiz';
import File from '../../models/FileModel';

// REPOSITORIES
import ClassRepository from '../../repositories/Class';
Expand Down Expand Up @@ -38,13 +37,8 @@ class GetStudentWhoHitMostQuestions {
const studentsFromClass = await this.classRepository.getAllStudents(
classInstance,
{
attributes: ['id', 'name', 'ra', 'email'],
attributes: ['id', 'name', 'url_image', 'email'],
include: [
{
model: File,
as: 'imageProfile',
attributes: ['url', 'path'],
},
{
model: StudentQuiz,
as: 'studentQuiz',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Yup from 'yup';

// MODELS
import StudentQuiz from '../../models/StudentQuiz';
import File from '../../models/FileModel';

// REPOSITORIES
import ClassRepository from '../../repositories/Class';
Expand Down Expand Up @@ -38,13 +37,8 @@ class GetFilteredStudentQuizStatisticsService {
const studentsFromClass = await this.classRepository.getAllStudents(
classInstance,
{
attributes: ['id', 'name', 'ra', 'email'],
attributes: ['id', 'name', 'url_image', 'email'],
include: [
{
model: File,
as: 'imageProfile',
attributes: ['url', 'path'],
},
{
model: StudentQuiz,
as: 'studentQuiz',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Yup from 'yup';

// MODELS
import StudentQuiz from '../../models/StudentQuiz';
import File from '../../models/FileModel';

// REPOSITORIES
import ClassRepository from '../../repositories/Class';
Expand Down Expand Up @@ -50,13 +49,8 @@ class GetStudentWhoHitMostQuestions {
const studentsFromClass = await this.classRepository.getAllStudents(
classInstance,
{
attributes: ['id', 'name', 'ra', 'email'],
attributes: ['id', 'name', 'url_image', 'email'],
include: [
{
model: File,
as: 'imageProfile',
attributes: ['url', 'path'],
},
{
model: StudentQuiz,
as: 'studentQuiz',
Expand Down
48 changes: 28 additions & 20 deletions backend/src/app/services/Student/CreateStudent.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,65 @@
import * as Yup from 'yup';
import GenerateTokenProvider from '../../provider/GenerateTokenProvider';
import GenerateRefreshTokenProvider from '../../provider/GenerateRefreshTokenProvider';
import StudentRepository from '../../repositories/Student';
import DeleteRefreshTokenService from '../RefreshToken/DeleteRefreshToken';
import GenerateRefreshTokenProvider from '../../provider/GenerateRefreshTokenProvider';
import GenerateTokenProvider from '../../provider/GenerateTokenProvider';

class CreateStudentService {
class StudentSessionService {
constructor() {
this.studentRepository = new StudentRepository();
}

async execute(data) {
const { name, email, picture, isLocalImage } = data;

const schema = Yup.object().shape({
name: Yup.string().required(),
email: Yup.string().email().required(),
password: Yup.string().required(),
picture: Yup.string().required(),
isLocalImage: Yup.boolean().required(),
});

if (!(await schema.isValid(data))) {
const error = new Error();
error.response = 'Falha na validação!';
error.status = 403;
error.response = 'Falha na validação!';
throw error;
}

if (
await this.studentRepository.findOne({ where: { email: data.email } })
) {
const error = new Error();
error.status = 403;
error.response = 'E-mail já cadastrado!';
throw error;
let student = await this.studentRepository.findOne({
where: { email },
});

if (!student) {
const idImage = isLocalImage ? picture : null;
const urlImage = !isLocalImage ? picture : null;

student = await this.studentRepository.create({
email,
name,
idImage,
urlImage,
isLocalImage,
});
}

const student = await this.studentRepository.create(data);
const { id, email, name } = student;
const { id } = student;

// REMOVE REFRESH TOKENS ANTIGOS SALVOS NO BANCO
await DeleteRefreshTokenService.execute({
where: { userId: id },
});

const token = await GenerateTokenProvider.execute(id);

const refreshToken = await GenerateRefreshTokenProvider.execute(id);

return {
student: {
email,
name,
},
student,
token,
refreshToken: refreshToken.id,
isFirstLogin: !name,
};
}
}

export default new CreateStudentService();
export default new StudentSessionService();
Loading

0 comments on commit 1bb2c5b

Please sign in to comment.