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
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/controllers/.DS_Store
Binary file not shown.
96 changes: 31 additions & 65 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 Expand Up @@ -169,7 +135,7 @@ export const updateById = async (req, res) => {

if (req.user.role === 'TEACHER') {
if (cohortId) {
updateData.cohortId = Number(cohortId)
updateData.cohortId = cohortId
}

if (role) {
Expand Down