forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved view components and create getUserStats logic b00tc4mp#182
- Loading branch information
1 parent
a6f8cfb
commit 30ab450
Showing
19 changed files
with
308 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
node_modules | ||
.node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import jwt from '../util/jsonwebtoken-promised.js' | ||
|
||
import logic from '../logic/index.js' | ||
|
||
import { CredentialsError } from 'com/errors.js' | ||
|
||
const { JWT_SECRET } = process.env | ||
|
||
export default (req, res, next) => { | ||
try { | ||
const token = req.headers.authorization.slice(7) | ||
|
||
jwt.verify(token, JWT_SECRET) | ||
.then(payload => { | ||
const { sub: userId } = payload | ||
|
||
const { targetUserId } = req.params | ||
|
||
try { | ||
logic.getUserStats(userId, targetUserId) | ||
.then(stats => res.json(stats)) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
}) | ||
.catch(error => { | ||
next(new CredentialsError(error.message)) | ||
}) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import validate from 'com/validate.js' | ||
import { Activity, Answer, Exercise, User } from '../../data/index.js' | ||
import { SystemError, NotFoundError } from 'com/errors.js' | ||
|
||
const getUserStats = (userId, targetUserId) => { | ||
validate.id(userId, 'userId') | ||
validate.id(targetUserId, 'targetUserId') | ||
|
||
return User.findById(userId).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) { | ||
throw new NotFoundError('user not found') | ||
} | ||
|
||
return User.findById(targetUserId).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) | ||
throw new NotFoundError('targetUser not found') | ||
|
||
return Answer.find({ student: targetUserId }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(answers => { | ||
const exerciseIds = answers.map(answer => answer.exercise) | ||
|
||
return Exercise.find({ _id: { $in: exerciseIds } }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(exercises => { | ||
|
||
let countCorrectExercises = 0 | ||
exercises.forEach(exercise => { | ||
answers.forEach(answer => { | ||
if (answer.exercise.toString() === exercise._id.toString()) { | ||
switch (exercise.type) { | ||
case 'completeSentence': | ||
if (answer.answer === exercise.answer) { | ||
countCorrectExercises += 1 | ||
} | ||
break | ||
case 'orderSentence': | ||
if (answer.answer === exercise.sentence) { | ||
countCorrectExercises += 1 | ||
} | ||
break | ||
} | ||
} | ||
}) | ||
}) | ||
const activityIds = exercises.map(exercise => exercise.activity) | ||
return Activity.countDocuments({ _id: { $in: activityIds } }) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(countActivities => { | ||
|
||
return { countActivities, countExercises: exercises.length, countCorrectExercises } | ||
}) | ||
|
||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
export default getUserStats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import getUserStats from './getUserStats.js' | ||
import mongoose from 'mongoose' | ||
import 'dotenv/config' | ||
|
||
const { MONGODB_URL } = process.env | ||
|
||
mongoose.connect(MONGODB_URL) | ||
.then(() => { | ||
try { | ||
getUserStats('66a94dcb34505782bcd8cfd0', '66a94dcb34505782bcd8cfd0') | ||
.then(userInfo => console.log(userInfo)) | ||
.catch(error => console.error(error)) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
}).catch(error => { console.error(error) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import errors from 'com/errors' | ||
import validate from 'com/validate' | ||
|
||
const getUserStats = (targetUserId) => { | ||
validate.id(targetUserId, 'targetUserId') | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users/student/${targetUserId}/stats`, { | ||
headers: { | ||
Authorization: `Bearer ${localStorage.token}` | ||
} | ||
}) | ||
.catch(() => { throw new SystemError('server error') }) | ||
.then(response => { | ||
if (response.status === 200) { | ||
return response.json() | ||
.catch(() => { throw new SystemError('server error') }) | ||
// .then(({ countActivities, countExercises, countCorrectExercises }) => ({ countActivities, countExercises, countCorrectExercises })) | ||
.then(stats => stats) | ||
} | ||
|
||
return response.json() | ||
.catch(() => { throw new SystemError('server error') }) | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} | ||
|
||
export default getUserStats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
staff/agustin-birman/app/src/views/components/AddStudent/index.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
.AddStudentText{ | ||
margin: 5%; | ||
} | ||
|
||
.AddStudentButton{ | ||
margin: 8%; | ||
} | ||
|
||
.AddStudentContainer{ | ||
margin-top: 30%; | ||
width: 90%; | ||
box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.3); | ||
border-radius: 20px; | ||
padding: 15px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.