2222import java .util .Collections ;
2323import java .util .List ;
2424import java .util .ArrayList ;
25+ import java .util .Optional ;
2526import java .util .stream .Collectors ;
2627import java .time .LocalDateTime ;
2728
@@ -251,13 +252,18 @@ public List<MissionHistoryDto> getMissionHistoryByTeam(Long teamId) {
251252
252253 // MissionHistory를 DTO로 변환
253254 private MissionHistoryDto convertToDto (MissionHistory history ) {
255+ // 미션을 완료한 사용자와 매칭된 상대방들의 이름 조회
256+ List <String > matchedNames = getMatchedUserNames (history .getUser ().getId (), history .getTeam ().getTeamId ());
257+
254258 return MissionHistoryDto .builder ()
255259 .id (history .getId ())
256260 .subGroupId (history .getSubGroup ().getId ())
261+ .subGroupName (history .getSubGroup ().getName ())
257262 .teamId (history .getTeam ().getTeamId ())
258263 .teamName (history .getTeam ().getTeamName ())
259264 .userId (history .getUser ().getId ())
260265 .userName (history .getUser ().getName ())
266+ .matchedNames (matchedNames ) // 매칭된 상대방들의 이름 추가
261267 .missionTemplateId (history .getMissionTemplate ().getId ())
262268 .missionTitle (history .getMissionTemplate ().getTitle ())
263269 .missionDescription (history .getMissionTemplate ().getDescription ())
@@ -266,4 +272,31 @@ private MissionHistoryDto convertToDto(MissionHistory history) {
266272 .createdAt (history .getCreatedAt ())
267273 .build ();
268274 }
275+
276+ // 사용자와 매칭된 상대방들의 이름 조회
277+ private List <String > getMatchedUserNames (Long userId , Long teamId ) {
278+ try {
279+ // 사용자가 속한 서브그룹 조회
280+ Optional <Long > subGroupIdOpt = subGroupMemberRepository .findSubGroupIdByTeamIdAndUserId (teamId , userId );
281+ if (subGroupIdOpt .isEmpty ()) {
282+ return new ArrayList <>();
283+ }
284+
285+ Long subGroupId = subGroupIdOpt .get ();
286+
287+ // 같은 서브그룹의 다른 멤버들 조회 (본인 제외)
288+ List <SubGroupMember > members = subGroupMemberRepository .findBySubGroup_Id (subGroupId );
289+ return members .stream ()
290+ .map (member -> member .getUser ().getName ())
291+ .filter (name -> !name .equals (subGroupMemberRepository .findBySubGroup_Id (subGroupId )
292+ .stream ()
293+ .filter (m -> m .getUser ().getId ().equals (userId ))
294+ .findFirst ()
295+ .map (m -> m .getUser ().getName ())
296+ .orElse ("" )))
297+ .collect (Collectors .toList ());
298+ } catch (Exception e ) {
299+ return new ArrayList <>();
300+ }
301+ }
269302}
0 commit comments