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

[PC-607] 회원 탈퇴 기능 구현 #59

Merged
merged 9 commits into from
Feb 15, 2025
129 changes: 67 additions & 62 deletions api/src/main/java/org/yapp/domain/user/application/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,80 @@
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final UserRejectHistoryRepository userRejectHistoryRepository;
private final JwtUtil jwtUtil;
private final UserRepository userRepository;
private final UserRejectHistoryRepository userRejectHistoryRepository;
private final JwtUtil jwtUtil;

/**
* Role을 USER로 바꾸고 변경된 토큰을 반환한다.
*
* @return 액세스토큰과 리프레시 토큰
*/
@Transactional
public OauthLoginResponse completeProfileInitialize(Long userId, Profile profile) {
User user =
userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
user.setProfile(profile);
user.updateUserRole(RoleStatus.PENDING.getStatus());
String oauthId = user.getOauthId();
String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(),
600000L);
String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(),
864000000L);
return new OauthLoginResponse(RoleStatus.PENDING.getStatus(), accessToken, refreshToken);
}

public User getUserById(Long userId) {
return userRepository.findById(userId)
/**
* Role을 USER로 바꾸고 변경된 토큰을 반환한다.
*
* @return 액세스토큰과 리프레시 토큰
*/
@Transactional
public OauthLoginResponse completeProfileInitialize(Long userId, Profile profile) {
User user =
userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
}
user.setProfile(profile);
user.updateUserRole(RoleStatus.PENDING.getStatus());
String oauthId = user.getOauthId();
String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(),
600000L);
String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(),
864000000L);
return new OauthLoginResponse(RoleStatus.PENDING.getStatus(), accessToken, refreshToken);
}

/**
* Role을 Register로 바꾸고 변경된 토큰을 반환한다.
*
* @return 액세스토큰과 리프레시 토큰
*/
@Transactional
public OauthLoginResponse registerPhoneNumber(Long userId, String phoneNumber) {
User user =
userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
user.updateUserRole(RoleStatus.REGISTER.getStatus());
user.initializePhoneNumber(phoneNumber);
String oauthId = user.getOauthId();
String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(),
600000L);
String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(),
864000000L);
return new OauthLoginResponse(RoleStatus.REGISTER.getStatus(), accessToken, refreshToken);
}
public User getUserById(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
}

@Transactional(readOnly = true)
public UserRejectHistoryResponse getUserRejectHistoryLatest(Long userId) {
User user = this.getUserById(userId);
Profile profile = user.getProfile();
/**
* Role을 Register로 바꾸고 변경된 토큰을 반환한다.
*
* @return 액세스토큰과 리프레시 토큰
*/
@Transactional
public OauthLoginResponse registerPhoneNumber(Long userId, String phoneNumber) {
User user =
userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
user.updateUserRole(RoleStatus.REGISTER.getStatus());
user.initializePhoneNumber(phoneNumber);
String oauthId = user.getOauthId();
String accessToken = jwtUtil.createJwt("access_token", userId, oauthId, user.getRole(),
600000L);
String refreshToken = jwtUtil.createJwt("refresh_token", userId, oauthId, user.getRole(),
864000000L);
return new OauthLoginResponse(RoleStatus.REGISTER.getStatus(), accessToken, refreshToken);
}

boolean reasonImage = false;
boolean reasonDescription = false;
@Transactional(readOnly = true)
public UserRejectHistoryResponse getUserRejectHistoryLatest(Long userId) {
User user = this.getUserById(userId);
Profile profile = user.getProfile();

UserRejectHistory userRejectHistory = userRejectHistoryRepository.findTopByUserIdOrderByCreatedAtDesc(
userId).orElse(null);
boolean reasonImage = false;
boolean reasonDescription = false;

if (userRejectHistory != null) {
reasonImage = userRejectHistory.isReasonImage();
reasonDescription = userRejectHistory.isReasonDescription();
}
UserRejectHistory userRejectHistory = userRejectHistoryRepository.findTopByUserIdOrderByCreatedAtDesc(
userId).orElse(null);

return new UserRejectHistoryResponse(
profile.getProfileStatus(),
reasonImage,
reasonDescription
);
if (userRejectHistory != null) {
reasonImage = userRejectHistory.isReasonImage();
reasonDescription = userRejectHistory.isReasonDescription();
}

return new UserRejectHistoryResponse(
profile.getProfileStatus(),
reasonImage,
reasonDescription
);
}

@Transactional
public void deleteUser(Long userId) {
userRepository.deleteById(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -18,17 +19,25 @@
@RequestMapping("/api/users")
public class UserController {

private final UserService userService;
private final UserService userService;

@PreAuthorize(value = "hasRole('USER')")
@GetMapping("/reject")
@Operation(summary = "사용자 거절 사유 조회", description = "사용자의 최근 거절 사유를 조회합니다.", tags = {"사용자"})
public ResponseEntity<CommonResponse<UserRejectHistoryResponse>> getUserRejectHistory(
@AuthenticationPrincipal Long userId) {
UserRejectHistoryResponse response = userService.getUserRejectHistoryLatest(
userId);

return ResponseEntity.status(HttpStatus.OK)
.body(CommonResponse.createSuccess(response));
}
@PreAuthorize(value = "hasRole('USER')")
@GetMapping("/reject")
@Operation(summary = "사용자 거절 사유 조회", description = "사용자의 최근 거절 사유를 조회합니다.", tags = {"사용자"})
public ResponseEntity<CommonResponse<UserRejectHistoryResponse>> getUserRejectHistory(
@AuthenticationPrincipal Long userId) {
UserRejectHistoryResponse response = userService.getUserRejectHistoryLatest(
userId);

return ResponseEntity.status(HttpStatus.OK)
.body(CommonResponse.createSuccess(response));
}

@DeleteMapping
@PreAuthorize(value = "hasRole('USER')")
@Operation(summary = "회원 탈퇴", description = "회원 탈퇴합니다.", tags = {"사용자"})
public ResponseEntity<CommonResponse<Void>> deleteUser(@AuthenticationPrincipal Long userId) {
userService.deleteUser(userId);
return ResponseEntity.ok(CommonResponse.createSuccessWithNoContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.yapp.core.domain.BaseEntity;
import org.yapp.core.domain.user.User;

Expand All @@ -33,14 +35,15 @@
)
public class BlockContact extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;

@Column(nullable = false)
private String phoneNumber;
@Column(nullable = false)
private String phoneNumber;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.time.LocalDate;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.yapp.core.domain.match.enums.UserMatchStatus;
import org.yapp.core.domain.user.User;
import org.yapp.core.exception.ApplicationException;
Expand All @@ -31,6 +33,7 @@ public class MatchInfo {

@ManyToOne
@JoinColumn(name = "user_1")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user1;

@Column(name = "user_1_match_status")
Expand All @@ -39,6 +42,7 @@ public class MatchInfo {

@ManyToOne
@JoinColumn(name = "user_2")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user2;

@Column(name = "user_2_match_status")
Expand Down
28 changes: 16 additions & 12 deletions core/domain/src/main/java/org/yapp/core/domain/report/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.yapp.core.domain.BaseEntity;
import org.yapp.core.domain.user.User;

Expand All @@ -21,19 +23,21 @@
@Getter
public class Report extends BaseEntity {

@Id
@Column(name = "report_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@Column(name = "report_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "reporter_user_id", nullable = false)
private User reporter;
@ManyToOne
@JoinColumn(name = "reporter_user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User reporter;

@ManyToOne
@JoinColumn(name = "reported_user_id", nullable = false)
private User reportedUser;
@ManyToOne
@JoinColumn(name = "reported_user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User reportedUser;

@Column(length = 500)
private String reason;
@Column(length = 500)
private String reason;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.yapp.core.domain.user.User;

@Entity
Expand All @@ -22,19 +24,20 @@
@Builder
public class TermAgreement {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "agreement_id")
private long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "agreement_id")
private long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "term_id", nullable = false)
private Term term;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "term_id", nullable = false)
private Term term;

@Column(nullable = false)
private LocalDateTime agreedAt;
@Column(nullable = false)
private LocalDateTime agreedAt;
}
Loading
Loading