@@ -5,6 +5,7 @@ import { NextRequest, NextResponse } from 'next/server';
55import jwt from 'jsonwebtoken' ;
66import dbConnect from '@/lib/db' ;
77import SkillMatch from '@/lib/models/skillMatch' ;
8+ import User from '@/lib/models/userSchema' ;
89
910// Helper function to get user ID from the token
1011function getUserIdFromToken ( req : NextRequest ) : string | null {
@@ -67,26 +68,53 @@ export async function GET(request: NextRequest) {
6768 const matches = await SkillMatch . find ( query )
6869 . sort ( { createdAt : - 1 } ) ;
6970
70- // Transform matches to identify the current user's perspective
71+ // Get unique user IDs to fetch current avatar data
72+ const allUserIds = new Set < string > ( ) ;
73+ matches . forEach ( match => {
74+ allUserIds . add ( match . userOneId ) ;
75+ allUserIds . add ( match . userTwoId ) ;
76+ } ) ;
77+
78+ // Fetch current user data for all users involved in matches
79+ const currentUserData = await User . find (
80+ { _id : { $in : Array . from ( allUserIds ) } } ,
81+ 'firstName lastName avatar'
82+ ) . lean ( ) ;
83+
84+ // Create a map for quick lookup
85+ const userDataMap = new Map ( ) ;
86+ currentUserData . forEach ( user => {
87+ userDataMap . set ( user . _id . toString ( ) , user ) ;
88+ } ) ;
89+
90+ // Transform matches to identify the current user's perspective with updated avatars
7191 const transformedMatches = matches . map ( match => {
7292 const isUserOne = match . userOneId === userId ;
93+ const otherUserId = isUserOne ? match . userTwoId : match . userOneId ;
94+ const otherUserData = userDataMap . get ( otherUserId ) || { } ;
95+ const currentUserData = userDataMap . get ( userId ) || { } ;
7396
7497 return {
7598 id : match . id ,
7699 matchPercentage : match . matchPercentage ,
77100 matchType : match . matchType ,
78101 status : match . status ,
79102 createdAt : match . createdAt ,
80- // Current user's data
81- myDetails : isUserOne ? match . userOneDetails : match . userTwoDetails ,
103+ // Current user's data with updated avatar
104+ myDetails : {
105+ ...( isUserOne ? match . userOneDetails : match . userTwoDetails ) ,
106+ firstName : currentUserData . firstName || ( isUserOne ? match . userOneDetails . firstName : match . userTwoDetails . firstName ) ,
107+ lastName : currentUserData . lastName || ( isUserOne ? match . userOneDetails . lastName : match . userTwoDetails . lastName ) ,
108+ avatar : currentUserData . avatar
109+ } ,
82110 myListingId : isUserOne ? match . listingOneId : match . listingTwoId ,
83- // Other user's data
111+ // Other user's data with updated avatar
84112 otherUser : {
85- userId : isUserOne ? match . userTwoId : match . userOneId ,
113+ userId : otherUserId ,
86114 listingId : isUserOne ? match . listingTwoId : match . listingOneId ,
87- firstName : isUserOne ? match . userTwoDetails . firstName : match . userOneDetails . firstName ,
88- lastName : isUserOne ? match . userTwoDetails . lastName : match . userOneDetails . lastName ,
89- avatar : isUserOne ? match . userTwoDetails . avatar : match . userOneDetails . avatar ,
115+ firstName : otherUserData . firstName || ( isUserOne ? match . userTwoDetails . firstName : match . userOneDetails . firstName ) ,
116+ lastName : otherUserData . lastName || ( isUserOne ? match . userTwoDetails . lastName : match . userOneDetails . lastName ) ,
117+ avatar : otherUserData . avatar ,
90118 offeringSkill : isUserOne ? match . userTwoDetails . offeringSkill : match . userOneDetails . offeringSkill ,
91119 seekingSkill : isUserOne ? match . userTwoDetails . seekingSkill : match . userOneDetails . seekingSkill
92120 }
0 commit comments