Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 136 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"husky": "^7.0.4",
"nodemon": "^2.0.15",
"prettier": "^2.6.2",
"prisma": "^3.12.0"
"prisma": "^5.19.1"
},
"dependencies": {
"@prisma/client": "^3.12.0",
"@prisma/client": "^5.19.1",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand Down
94 changes: 30 additions & 64 deletions src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,78 +35,44 @@ export const getById = async (req, res) => {
}
}

export const getCompletionById = async (req, res) => {
const id = parseInt(req.params.id)
if (isNaN(id)) {
return res.status(400).json({ error: 'Invalid user ID' })
}

try {
const userWithGrades = await User.findUserWithGradesById(id)
export const getAll = async (req, res) => {
// Destructure first_name and last_name from query parameters
const { first_name: firstName, last_name: lastName } = req.query

if (!userWithGrades) {
return res.status(404).send('User not found')
}
let foundAllUsers

const user = userWithGrades.user
const grades = userWithGrades.user.grades
const modules = userWithGrades.modules

const formattedGrades = modules.map((module) => {
return {
moduleId: module.id,
moduleName: module.name,
units: module.units.map((unit) => {
return {
unitId: unit.id,
unitName: unit.name,
exercises: unit.exercises.map((exercise) => {
const userGrade = grades.find(
(grade) => grade.exerciseId === exercise.id
)
return {
exerciseId: exercise.id,
exerciseName: exercise.name,
grade: userGrade ? userGrade.grade : null,
completedAt: userGrade ? userGrade.completedAt : null
}
})
}
})
// If both firstName and lastName are provided, search by both
if (firstName && lastName) {
foundAllUsers = await User.findAll({
where: {
first_name: firstName,
last_name: lastName
}
})

return res.status(200).json({
user: {
id: user.id,
email: user.email,
firstName: user.profile.firstName,
lastName: user.profile.lastName,
cohortName: user.cohort.cohortName
},
grades: formattedGrades
}
// If only firstName is provided, search by first name
else if (firstName) {
foundAllUsers = await User.findAll({
where: {
first_name: firstName
}
})
} catch (error) {
console.error('Error:', error)
return res.status(500).send('Error retrieving user grades')
}
}

export const getAll = async (req, res) => {
// eslint-disable-next-line camelcase
const { search } = req.query

if (!search || !search.trim()) {
const allUsers = await User.findAll()
return sendDataResponse(res, 200, { users: allUsers })
// If only lastName is provided, search by last name
else if (lastName) {
foundAllUsers = await User.findAll({
where: {
last_name: lastName
}
})
}
// If no search criteria, fetch all users
else {
foundAllUsers = await User.findAll()
}

// remove any spaces around query, then split by one or more empty spaces
const formattedSearch = search.trim().split(/\s+/)

const foundUsers = await User.findManyByName(formattedSearch)

const formattedUsers = foundUsers.map((user) => {
// Format the users for response
const formattedUsers = foundAllUsers.map((user) => {
return {
...user.toJSON().user
}
Expand Down