Skip to content

Commit

Permalink
Merge pull request #55 from AR-TTUBEOG/feature/50
Browse files Browse the repository at this point in the history
[Feat] 회원탈퇴 API 구현
  • Loading branch information
arinming authored Feb 14, 2024
2 parents 40d7e5e + 2cdc335 commit d6e6aac
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ttubeog.domain.member.domain.repository.MemberRepository;
import com.ttubeog.domain.member.dto.request.ProduceNicknameRequest;
import com.ttubeog.domain.member.dto.response.MemberDetailRes;
import com.ttubeog.domain.member.exception.FailureMemberDeleteException;
import com.ttubeog.domain.member.exception.InvalidAccessTokenExpiredException;
import com.ttubeog.domain.member.exception.InvalidMemberException;
import com.ttubeog.global.DefaultAssert;
Expand All @@ -18,12 +19,14 @@
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.spi.ResolveResult;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
Expand Down Expand Up @@ -160,7 +163,6 @@ public void deleteValueByKey(String key) {
}

@Transactional
// 회원탈퇴
public ResponseEntity<?> deleteUser(HttpServletRequest request) {
Long memberId = jwtTokenProvider.getMemberId(request);
Optional<Member> checkMember = memberRepository.findById(memberId);
Expand All @@ -170,20 +172,45 @@ public ResponseEntity<?> deleteUser(HttpServletRequest request) {
}

Member member = checkMember.get();
if (member.getStatus() == Status.INACTIVE) {
return ResponseEntity.badRequest().body(new InvalidMemberException("이미 탈퇴한 회원입니다."));
if (member.getStatus() != Status.INACTIVE) {
member = Member.builder()
.id(member.getId())
.oAuthId(member.getOAuthId())
.nickname(member.getNickname())
.memberNumber(member.getMemberNumber())
.email(member.getEmail())
.platformId(member.getPlatformId())
.platform(member.getPlatform())
.status(Status.INACTIVE) // 상태를 비활성화로 설정
.refreshToken(member.getRefreshToken())
.build();
memberRepository.save(member);
}

member = Member.builder().status(Status.INACTIVE).build();
memberRepository.save(member);

LocalDateTime deleteTime = LocalDateTime.now().plusDays(WAITING_PERIOD_DAYS);

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(Message.builder().message("성공적으로 회원탈퇴 되었습니다.").build())
.build();

return ResponseEntity.ok(apiResponse);
}



@Transactional
public ResponseEntity<?> deleteInactiveMember() {
try {
// 비활성화 된 회원 찾기
List<Member> memberStatus = memberRepository.findByStatus(Status.INACTIVE);

// 비활성회 된 회원 탈퇴
for (Member member : memberStatus) {
memberRepository.delete(member);
}

return ResponseEntity.ok("비활성화된 회원 탈퇴 완료");
} catch (Exception e) {
throw new FailureMemberDeleteException();
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/ttubeog/domain/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public Member(String email, Platform platform, Status status, String memberNumbe
this.memberNumber = memberNumber;
}

public Member(Status status) {
this.status = status;
}
public boolean isRegisteredOAuthMember() {
return nickname != null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ttubeog.domain.member.domain.repository;

import com.ttubeog.domain.auth.domain.Platform;
import com.ttubeog.domain.auth.domain.Status;
import com.ttubeog.domain.member.domain.Member;
import com.ttubeog.domain.member.dto.request.ProduceNicknameRequest;
import io.lettuce.core.dynamic.annotation.Param;
Expand All @@ -10,6 +11,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -21,8 +23,7 @@ public interface MemberRepository extends JpaRepository<Member,Long>{
Optional<Member> findByEmail(String email);
Optional<Member> findByMemberNumber(String memberNumber);

@Query("SELECT m.status FROM Member m WHERE m.id = :memberId")
Optional<Member> findMemberStatus(@Param("memberId") Long memberId);
List<Member> findByStatus(Status status);

Optional<Member> findByPlatformAndPlatformId(Platform platform, String platformId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ttubeog.domain.member.exception;


public class FailureMemberDeleteException extends RuntimeException {

public FailureMemberDeleteException(){
super("회원 탈퇴를 실패하였습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Tag(name = "Member", description = "Member API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/member")
public class MemberController {
private final MemberService memberService;
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

@Operation(summary = "멤버 정보 확인", description = "현재 접속된 멤버 정보를 확인합니다.")
@ApiResponses(value = {
Expand Down Expand Up @@ -73,4 +79,31 @@ public ResponseEntity<?> logout(
) {
return memberService.deleteLogout(request);
}

// @Operation(summary = "회원탈퇴", description = "현재 접속된 회원이 탈퇴 합니다.")
// @ApiResponses(value = {
// @ApiResponse(responseCode = "200", description = "회원탈퇴 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = Message.class))}),
// @ApiResponse(responseCode = "400", description = "회원탈퇴 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}),
// })
// @DeleteMapping("/delete")
// public ResponseEntity<?> deleteUser(
// HttpServletRequest request
// ) {
// ResponseEntity<?> responseEntity = memberService.deleteUser(request);
//
// if (responseEntity.getStatusCode().is2xxSuccessful()) {
// LocalDateTime localDateTime = LocalDateTime.now().plusDays(3);
//
// scheduledExecutorService.schedule(() -> {
// ResponseEntity<?> deleteResponse = memberService.deleteInactiveMember();
//
// if (deleteResponse.getStatusCode().is2xxSuccessful()) {
// System.out.println("회원 삭제 성공");
// } else {
// System.out.println("회원 삭제 실패");
// }
// }, 3, TimeUnit.DAYS);
// }
// return responseEntity;
// }
}

0 comments on commit d6e6aac

Please sign in to comment.