//RetrospectiveController
@Operation(summary = "회고 리더 권한 양도")
@PostMapping("/{retrospectiveId}/transferLeadership")
public CommonApiResponse<CreateRetrospectiveResponseDto> transferLeaderdhip(@CurrentUser User user,
@PathVariable Long retrospectiveId,
@RequestParam Long newLeaderId) {
CreateRetrospectiveResponseDto response = retrospectiveService.transferRetrospectiveLeadership(user, retrospectiveId, newLeaderId);
return CommonApiResponse.successResponse(HttpStatus.CREATED, response);
}
@RequestParam으로 Long newLeaderId 를 받기 때문에 URL을 아래와 같이 보내야 함
<http://api.pastforward.link/retrospectives/1/transferLeadership**?newLeaderId=2**>
FE로 부터 받은 자료

⇒ “?newLeaderId=2”와 같이 newLeaderId를 설정하지 않은 것이 500에러의 원인일까요?
// RetrospectiveService
//회고 권한 양도 메서드
@Transactional
public CreateRetrospectiveResponseDto transferRetrospectiveLeadership(User user, Long retrospectiveId, Long newLeaderId) {
// 회고를 조회하고, 회고가 존재하지 않을 경우 에러를 발생시킨다.
Retrospective retrospective = retrospectiveRepository.findById(retrospectiveId)
.orElseThrow(() -> new ResourceNotFoundException("Retrospective not found"));
// 현재 사용자가 해당 회고 팀의 멤버인지 확인하고, 아니면 에러를 발생시킨다.
User currentUser = userService.getCurrentUser();
UserTeam currentUserTeam = userTeamRepository.findByTeamIdAndUserId(retrospective.getTeam().getId(), currentUser.getId())
.orElseThrow(() -> new NoSuchElementException("Current user is not part of the team"));
// 현재 사용자가 팀의 리더인지 확인하고 아니면 에러를 발생시킨다.
if(!currentUserTeam.getRole().equals(UserTeamRole.LEADER)) {
throw new NoSuchElementException("You do not have permission to transfer leadership");
}
// 새로운 리더 조회하고 해당 ID의 사용자가 없으면 에러를 발생시킨다.
User newLeader = userRepository.findById(newLeaderId).orElseThrow(() -> new ResourceNotFoundException("User not found"));
UserTeam newLeaderTeam = userTeamRepository.findByTeamIdAndUserId(retrospective.getTeam().getId(), newLeader.getId())
.orElseThrow(() -> new ResourceNotFoundException("New leader is not part of the team"));
// 현재 리더 역할 변경 (리더 -> 멤버)
currentUserTeam.updateMember();
userTeamRepository.save(currentUserTeam);
// 새로운 리더 역할 변경 (멤버 -> 리더)
newLeaderTeam.updateLeader();
userTeamRepository.save(newLeaderTeam);
Retrospective savedRetrospective = retrospectiveRepository.save(retrospective);
return toResponseDto(savedRetrospective);
}
// CreateRetrospectiveResponseDto
@Getter
@Builder
public class CreateRetrospectiveResponseDto {
private Long id;
private String title;
private Long teamId;
private Long userId;
private Long templateId;
private ProjectStatus status;
private UUID thumbnail;
private LocalDateTime startDate;
private String description;
}