Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #89 UserUseCase 분리 및 리프레시 토큰 수정 #89

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
package leets.weeth.domain.user.application.usecase;

import jakarta.servlet.http.HttpServletRequest;
import leets.weeth.domain.user.application.dto.request.UserRequestDto;
import leets.weeth.domain.user.application.dto.response.UserResponseDto;
import leets.weeth.global.auth.jwt.application.dto.JwtDto;

import java.util.List;
import java.util.Map;

import static leets.weeth.domain.user.application.dto.request.UserRequestDto.refreshRequest;

public interface UserManageUseCase {
Copy link
Member

@huncozyboy huncozyboy Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생많으셨습니다 어드민 페이지에서 관리할 수 있는 정보(회원조회, 가입 승인, 유저 추방, 관리자 승격 + 강등, 다음 기수도 진행, 비번 초기화)들은 PR에 적어주셨듯이 어드민 기준이기 때문에 userManageUseCase라는 이름과 별도로 구성한 목적에 맞다고 판단이 드는데, 동아리 멤버 전체 조회라던지 내 정보와 관련된 부분도 userManageUseCase로 들어가는 것은 추가적인 고민이 필요해보입니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 조회같은 부분은 manage 말고 UserUseCase에 들어가야 한다고 생각합니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러면 admin 관련한 메서드만 Manage에 넣도록 하겟습니당

JwtDto refresh(HttpServletRequest request);

UserResponseDto.Response find(Long userId);

Map<Integer, List<UserResponseDto.Response>> findAll();

Map<Integer, List<UserResponseDto.SummaryResponse>> findAllUser();

List<UserResponseDto.AdminResponse> findAllByAdmin();

UserResponseDto.UserResponse findUserDetails(Long userId);

void update(UserRequestDto.Update dto, Long userId);

void accept(Long userId);

void update(Long userId, String role);

void leave(Long userId);

void ban(Long userId);

void applyOB(Long userId, Integer cardinal);

void reset(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,166 @@
package leets.weeth.domain.user.application.usecase;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.Transactional;
import leets.weeth.domain.attendance.domain.service.AttendanceSaveService;
import leets.weeth.domain.schedule.domain.entity.Meeting;
import leets.weeth.domain.schedule.domain.service.MeetingGetService;
import leets.weeth.domain.user.application.exception.StudentIdExistsException;
import leets.weeth.domain.user.application.exception.TelExistsException;
import leets.weeth.domain.user.application.mapper.UserMapper;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.domain.user.domain.service.UserDeleteService;
import leets.weeth.domain.user.domain.service.UserGetService;
import leets.weeth.domain.user.domain.service.UserSaveService;
import leets.weeth.domain.user.domain.service.UserUpdateService;
import leets.weeth.global.auth.jwt.application.dto.JwtDto;
import leets.weeth.global.auth.jwt.application.usecase.JwtManageUseCase;
import leets.weeth.global.auth.jwt.service.JwtRedisService;
import leets.weeth.global.auth.kakao.KakaoAuthService;
import leets.weeth.global.auth.kakao.dto.KakaoTokenResponse;
import leets.weeth.global.auth.kakao.dto.KakaoUserInfoResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static leets.weeth.domain.user.application.dto.request.UserRequestDto.refreshRequest;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static leets.weeth.domain.user.application.dto.request.UserRequestDto.*;
import static leets.weeth.domain.user.application.dto.response.UserResponseDto.*;
import static leets.weeth.domain.user.domain.entity.enums.LoginStatus.LOGIN;
import static leets.weeth.domain.user.domain.entity.enums.LoginStatus.REGISTER;
import static leets.weeth.domain.user.domain.entity.enums.Status.ACTIVE;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserManageUseCaseImpl implements UserManageUseCase {
private final JwtManageUseCase jwtManageUseCase;

private final UserGetService userGetService;
private final UserUpdateService userUpdateService;
private final UserDeleteService userDeleteService;

private final AttendanceSaveService attendanceSaveService;
private final MeetingGetService meetingGetService;
private final JwtRedisService jwtRedisService;

private final UserMapper mapper;
private final PasswordEncoder passwordEncoder;

@Override
public Map<Integer, List<Response>> findAll() {
return userGetService.findAllByStatus(ACTIVE).stream()
.flatMap(user -> Stream.concat(
user.getCardinals().stream()
.map(cardinal -> new AbstractMap.SimpleEntry<>(cardinal, mapper.to(user))), // 기수별 Map
Stream.of(new AbstractMap.SimpleEntry<>(0, mapper.to(user))) // 모든 기수는 cardinal 0에 저장
))
.collect(Collectors.groupingBy(Map.Entry::getKey, // key = 기수, value = 유저 정보
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}

@Override
public Map<Integer, List<SummaryResponse>> findAllUser() {
return userGetService.findAllByStatus(ACTIVE).stream()
.map(user -> new AbstractMap.SimpleEntry<>(user.getCardinals(), mapper.toSummaryResponse(user)))
.flatMap(entry -> Stream.concat(
entry.getKey().stream().map(cardinal -> new AbstractMap.SimpleEntry<>(cardinal, entry.getValue())), // 기수별 Map
Stream.of(new AbstractMap.SimpleEntry<>(0, entry.getValue())) // 모든 기수는 cardinal 0에 저장
))
.collect(Collectors.groupingBy(
Map.Entry::getKey, // key = 기수
Collectors.mapping(Map.Entry::getValue, Collectors.toList()) // value = 요약 정보 리스트
));
}

@Override
public List<AdminResponse> findAllByAdmin() {
return userGetService.findAll().stream()
.map(mapper::toAdminResponse)
.toList();
}

@Override
public UserResponse findUserDetails(Long userId) {
User user = userGetService.find(userId);
return mapper.toUserResponse(user);
}

@Override
public Response find(Long userId) {
return mapper.to(userGetService.find(userId));
}

@Override
public void update(Update dto, Long userId) {
validate(dto, userId);
User user = userGetService.find(userId);
userUpdateService.update(user, dto, passwordEncoder);
}

@Override
@Transactional
public void accept(Long userId) {
User user = userGetService.find(userId);

if (user.isInactive()) {
userUpdateService.accept(user);
List<Meeting> meetings = meetingGetService.find(user.getCardinals().get(0));
attendanceSaveService.save(user, meetings);
}
}

@Override
public void update(Long userId, String role) {
User user = userGetService.find(userId);
userUpdateService.update(user, role);
jwtRedisService.updateRole(user.getId(), role);
}

@Override
public void leave(Long userId) {
User user = userGetService.find(userId);
// 탈퇴하는 경우 리프레시 토큰 삭제
jwtRedisService.delete(user.getId());
userDeleteService.leave(user);
}

@Override
public void ban(Long userId) {
User user = userGetService.find(userId);
jwtRedisService.delete(user.getId());
userDeleteService.ban(user);
}

@Override
@Transactional
public JwtDto refresh(HttpServletRequest request) {
public void applyOB(Long userId, Integer cardinal) {
User user = userGetService.find(userId);

JwtDto token = jwtManageUseCase.reIssueToken(request);
if (user.notContains(cardinal)) {
if (user.isCurrent(cardinal)) {
user.initAttendance();
List<Meeting> meetings = meetingGetService.find(cardinal);
attendanceSaveService.save(user, meetings);
}

userUpdateService.applyOB(user, cardinal);
}
}

@Override
public void reset(Long userId) {
User user = userGetService.find(userId);
userUpdateService.reset(user, passwordEncoder);
}

log.info("RefreshToken 발급 완료: {}", token);
return new JwtDto(token.accessToken(), token.refreshToken());
private void validate(Update dto, Long userId) {
if (userGetService.validateStudentId(dto.studentId(), userId))
throw new StudentIdExistsException();
if (userGetService.validateTel(dto.tel(), userId))
throw new TelExistsException();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package leets.weeth.domain.user.application.usecase;

import jakarta.servlet.http.HttpServletRequest;
import leets.weeth.domain.user.application.dto.request.UserRequestDto;
import leets.weeth.global.auth.jwt.application.dto.JwtDto;

import java.util.List;
import java.util.Map;
Expand All @@ -17,27 +19,6 @@ public interface UserUseCase {

void register(Register dto, Long userId);

Response find(Long userId);
JwtDto refresh(String refreshToken);

Map<Integer, List<Response>> findAll();

Map<Integer, List<SummaryResponse>> findAllUser();

List<AdminResponse> findAllByAdmin();

UserResponse findUserDetails(Long userId);

void update(Update dto, Long userId);

void accept(Long userId);

void update(Long userId, String role);

void leave(Long userId);

void ban(Long userId);

void applyOB(Long userId, Integer cardinal);

void reset(Long userId);
}
Loading
Loading