From a13d6bcfb4834e329fab508c012085ed69517666 Mon Sep 17 00:00:00 2001 From: Olaoluwa Oyebode Date: Fri, 20 Sep 2024 14:48:15 +0100 Subject: [PATCH] user/getall and updateByID update --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 6148 bytes src/controllers/.DS_Store | Bin 0 -> 6148 bytes src/controllers/user.js | 96 ++++++++++++-------------------------- 4 files changed, 31 insertions(+), 65 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/controllers/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..01235d7e23b5e56f48d5c010fee9abd9049611a8 GIT binary patch literal 6148 zcmeHK!AiqG5Z$%aZYe?z3VK`cTCg>xAYMYOKVU=;Dm5_$gE3p0lpIPScl{xM#P4xt zcUvsBR}r0o**CK@lWZPjcZV^?{b}4~%wdcPP(+T#8o}wcp)H%t0YM#>kE2vY;Jrs< z8~g2mYp=1KP1s{peqDc>UnWVKL49aB_>@l~QT6u*2v& z8I?n4?_6a?m}H}gE=b}Lq}<&kS*%K5O|v-HwSftUrf3eG!`ZCcbKSOd`~7)a&Q4D{ zZP|1C^LbP3A03}v4xiJPOuZUDIW8Mz*T8aoTUulg1H=F^KnxHA8yK*Mh-hty(=;(a z4E$dPaDR}Xh@QpDpx!!Q&@BL99j3K_jlBfcNQ<7u${<8QxG4oRrQEg{+?2z-w0WMz z%AhG{+%_NFzIoBSaQ$|8zSQZAdj@GF28e-G25QE2@ch4kUuJESzgj{bF+dFbF$Q>j z5Da|yD0{Yk`#d~rCFlbv3g%U4fPlSn2>=K8kpmqxafvqMc@`^!_!OMi<$!b%(1g%H I4Ez8Cp9eTa6#xJL literal 0 HcmV?d00001 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e4aa49ea3e78035ad3e98ec279e18e89f9a62ed3 GIT binary patch literal 6148 zcmeHKQA@)x5Kgw~GKSCx1$_(nI&iaNAik73|9};JP?@bOSgg%hJNhsNeb+zaAMy8i zmt=$Lo(1KuT)s>0E=@jY(qoKqe=!{}<}k(tC?ZE=i{S89SHlK#Kv0-t&J;`GiNX3C zf@|-xCs=pJirV_~sLW?ksv?N^h-PV$7ybSxZ!}vwyKT`HUGWe+t1>9UVv+md{0@89 zN~O`-4x^hSE+@|Zh02OB$>O2LOo)D z82D!l@YXmO`>-i{xBl23-nA0+5flaEDl|aAuKff+2hWj1?KFOgHpF=rD}y)-j_Y(l O`Vr8CP(uv-0s~*v-bW7r literal 0 HcmV?d00001 diff --git a/src/controllers/.DS_Store b/src/controllers/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 { } } -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 } @@ -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) {