Skip to content

Commit

Permalink
refactor front and back b00tc4mp#182
Browse files Browse the repository at this point in the history
  • Loading branch information
AgusBirman committed Aug 19, 2024
1 parent f29ec20 commit e19159d
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 27 deletions.
2 changes: 1 addition & 1 deletion staff/agustin-birman/api/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.node_modules
node_modules
6 changes: 4 additions & 2 deletions staff/agustin-birman/api/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default (req, res, next) => {

try {
logic.authenticateUser(username, password)
.then(userId => {
jwt.sign({ sub: userId }, JWT_SECRET, { expiresIn: '1h' })
.then(user => {
const { id: userId, userType } = user

jwt.sign({ sub: userId, userType }, JWT_SECRET, { expiresIn: '1h' })
.then(token => res.json(token))
.catch(error => {
next(new SystemError(error.message))
Expand Down
2 changes: 1 addition & 1 deletion staff/agustin-birman/api/handlers/getUserNameHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default (req, res, next) => {

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload
const { sub: { id: userId } } = payload

const { targetUserId } = req.params

Expand Down
4 changes: 2 additions & 2 deletions staff/agustin-birman/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ mongoose.connect(MONGODB_URL)

api.delete('/exercise/:exerciseId', deleteExerciseHandler)

api.patch('/exercise/:exerciseId', jsonBodyParser, editExerciseHandler)
api.patch('/exercise/:exerciseId', editExerciseHandler)

api.post('/answer', jsonBodyParser, submitAnswerHandler)

api.get('/answer/:exerciseId', jsonBodyParser, getAnswersHandler)
api.get('/answer/:exerciseId', getAnswersHandler)

api.delete('/answer/:activityId', deleteAnswersHandler)

Expand Down
14 changes: 0 additions & 14 deletions staff/agustin-birman/api/logic/answer/deleteAnswers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ describe('deleteAnswers', () => {

beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany(), Exercise.deleteMany(), Answer.deleteMany()]))

// it('suceeds on deleting answers', () => {
// return bcrypt.hash('12345678', 8)
// .then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' }))
// .then(user => Activity.create({ teacher: user.id, title: 'title', description: 'description' })
// .then(activity => ({ user, activity })))
// .then(({ user, activity }) => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
// .then(exercise1 => Exercise.create({ teacher: user.id, activity: activity.id, sentence: 'alan (hat) es gegessen', answer: 'hat', index: 0 })
// .then(exercise2 => ({ user, activity, exercise1, exercise2 }))))
// .then(({ user, activity, exercise1, exercise2 }) => Answer.create({ student: user.id, exercise: exercise1.id, activity: activity.id, answer: 'hat' })
// .then(answer1 => Answer.create({ student: user.id, exercise: exercise2.id, activity: activity.id, answer: 'haben' })
// .then(answer2 => ({ user, activity, exercise1, exercise2, answer1, answer2 }))))
// .then(({ user, activity, exercise1, exercise2, answer1, answer2 }) => console.log(answer1, answer2))
// })

it('suceeds on deleting answers', () => {
return bcrypt.hash('12345678', 8)
.then(hash => User.create({ name: 'Mocha', surname: 'Chai', email: '[email protected]', username: 'mochachai', password: hash, userType: 'teacher' }))
Expand Down
2 changes: 1 addition & 1 deletion staff/agustin-birman/api/logic/user/authenticateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const authenticateUser = (username, password) => {
throw new CredentialsError('wrong password')
}

return user._id.toString()
return { id: user._id.toString(), userType: user.userType }

})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@

.Header .Button {
padding: 0.3rem;
}

}
3 changes: 3 additions & 0 deletions staff/agustin-birman/app/src/logic/getActivity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import validate from 'com/validate'
import errors, { SystemError } from '../../../com/errors'

const getActivity = activityId => {
validate.id(activityId, 'activityId')

return fetch(`${import.meta.env.VITE_API_URL}/activity/${activityId}`, {
headers: {
Authorization: `Bearer ${localStorage.token}`
Expand Down
2 changes: 1 addition & 1 deletion staff/agustin-birman/app/src/logic/getUserName.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import extractPayloadFromJWT from '../utils/extractPayloadFromJWT'

const getUserName = () => {

const { sub: userId } = extractPayloadFromJWT(localStorage.token)
const { sub: { id: userId } } = extractPayloadFromJWT(localStorage.token)

return fetch(`${import.meta.env.VITE_API_URL}/users/${userId}`, {
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
width: 90%;
height: 15%;
padding: 10px;
border: 1px solid black;
border-radius: 20px;
margin-bottom: 1rem;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ function MenuItem() {
<Button className='MenuItem'><Link to='/activities/list'>View activities</Link></Button>
<Button className='MenuItem'><Link to='/users/students'>View students</Link></Button>
<Button className='MenuItem'><Link to={`/users/${userId}`}>Share ID</Link></Button>

</View >

}

export default MenuItem

0 comments on commit e19159d

Please sign in to comment.