From c4cd8d0840f07c55e389f7dfa1bd08339b431076 Mon Sep 17 00:00:00 2001 From: RavenLLevitt <103865865+RavenLLevitt@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:47:44 -0400 Subject: [PATCH 1/2] Adding Checked-in values for friends call. --- backend/routes/friendRoutes.js | 39 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/backend/routes/friendRoutes.js b/backend/routes/friendRoutes.js index 0d591cd0..97954759 100644 --- a/backend/routes/friendRoutes.js +++ b/backend/routes/friendRoutes.js @@ -198,7 +198,6 @@ router.get('/getFriends', verifyToken, async (req, res) => { ] }).populate('requester recipient', 'username'); - const friends = friendships.map(friendship => { return friendship.requester._id.toString() === userId.toString() ? friendship.recipient @@ -207,22 +206,46 @@ router.get('/getFriends', verifyToken, async (req, res) => { const friendIds = friends.map(friend => friend._id); - // fetch friends objects - const friendsObjects= await User.find({ + // Fetch friends objects + const friendsObjects = await User.find({ _id: { $in: friendIds } }); - - console.log(`GET: /friends friends found`); + + // Fetch check-in information for all friends - need to double check this is correct checked-in syntax + const checkedInFriends = await Classroom.find( + { checked_in: { $in: friendIds } }, + { checked_in: 1, _id: 1 } + ); + + const checkedInMap = new Map(); + checkedInFriends.forEach(classroom => { + classroom.checked_in.forEach(friendId => { + checkedInMap.set(friendId.toString(), classroom._id); + }); + }); + + // Attach check-in information to each friend - need to double check this is reformatted correctly. + const friendsWithCheckInData = friendsObjects.map(friend => { + const friendData = friend.toObject(); + const classroomId = checkedInMap.get(friend._id.toString()); + if (classroomId) { + friendData.checkedIn = classroomId.toString(); + } + return friendData; + }); + + console.log(`GET: /getFriends friends found`); res.json({ success: true, message: 'Friends found', - data: friendsObjects - }) + data: friendsWithCheckInData + }); } catch (error) { + console.log(`GET: /getFriends failed`); res.json({ success: false, message: error.message - }) + }); } }); From 613e88ffb98b4a20261d0596b74de896afbce50e Mon Sep 17 00:00:00 2001 From: RavenLLevitt <103865865+RavenLLevitt@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:58:18 -0500 Subject: [PATCH 2/2] Fixxed issue --- backend/routes/friendRoutes.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/backend/routes/friendRoutes.js b/backend/routes/friendRoutes.js index 97954759..a1f727ad 100644 --- a/backend/routes/friendRoutes.js +++ b/backend/routes/friendRoutes.js @@ -206,14 +206,12 @@ router.get('/getFriends', verifyToken, async (req, res) => { const friendIds = friends.map(friend => friend._id); - // Fetch friends objects const friendsObjects = await User.find({ - _id: { $in: friendIds } + _id: { $in: friendIds.map(id => new mongoose.Types.ObjectId(id)) } }); - // Fetch check-in information for all friends - need to double check this is correct checked-in syntax const checkedInFriends = await Classroom.find( - { checked_in: { $in: friendIds } }, + { checked_in: { $in: friendIds.map(id => new mongoose.Types.ObjectId(id)) } }, { checked_in: 1, _id: 1 } ); @@ -224,7 +222,6 @@ router.get('/getFriends', verifyToken, async (req, res) => { }); }); - // Attach check-in information to each friend - need to double check this is reformatted correctly. const friendsWithCheckInData = friendsObjects.map(friend => { const friendData = friend.toObject(); const classroomId = checkedInMap.get(friend._id.toString());