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

[Feature] 모집자 회원 탈퇴 API 구현 #231

Merged
merged 3 commits into from
Jun 3, 2024
Merged
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
Expand Up @@ -56,4 +56,8 @@ public void updateDeletedVolunteer(Volunteer deletedVolunteer) {
this.volunteerName = deletedVolunteer.getName();
this.phone = deletedVolunteer.getPhone();
}

public void updateDeletedIntermediary(Intermediary deletedIntermediary) {
this.intermediary = deletedIntermediary;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.pawwithu.connectdog.domain.application.entity.Application;
import com.pawwithu.connectdog.domain.application.entity.ApplicationStatus;
import com.pawwithu.connectdog.domain.intermediary.entity.Intermediary;
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -18,4 +19,6 @@ public interface ApplicationRepository extends JpaRepository<Application, Long>
List<Application> findByVolunteer(Volunteer volunteer);
Optional<Application> findByPostIdAndStatusNot(Long postId, ApplicationStatus status);

List<Application> findByIntermediary(Intermediary intermediary);

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,17 @@ public ResponseEntity<Void> volunteersWithdraw(HttpServletRequest request, @Auth
return ResponseEntity.noContent().build();
}

@Operation(summary = "모집자 - 탈퇴", description = "모집자가 탈퇴를 합니다.",
security = {@SecurityRequirement(name = "bearer-key") },
responses = {@ApiResponse(responseCode = "200", description = "모집자 탈퇴 성공")
, @ApiResponse(responseCode = "400"
, description = "T1, 토큰이 존재하지 않습니다. \t\n M2, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@DeleteMapping("/intermediaries/my")
public ResponseEntity<Void> intermediariesWithdraw(HttpServletRequest request, @AuthenticationPrincipal UserDetails loginUser) {
authService.intermediariesWithdraw(request, loginUser.getUsername());
return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
import com.pawwithu.connectdog.domain.auth.dto.response.*;
import com.pawwithu.connectdog.domain.badge.repository.VolunteerBadgeRepository;
import com.pawwithu.connectdog.domain.bookmark.repository.BookmarkRepository;
import com.pawwithu.connectdog.domain.dogStatus.repository.DogStatusImageRepository;
import com.pawwithu.connectdog.domain.dogStatus.repository.DogStatusRepository;
import com.pawwithu.connectdog.domain.fcm.repository.IntermediaryFcmRepository;
import com.pawwithu.connectdog.domain.fcm.repository.VolunteerFcmRepository;
import com.pawwithu.connectdog.domain.intermediary.entity.Intermediary;
import com.pawwithu.connectdog.domain.intermediary.repository.IntermediaryRepository;
import com.pawwithu.connectdog.domain.notification.repository.IntermediaryNotificationRepository;
import com.pawwithu.connectdog.domain.notification.repository.VolunteerNotificationRepository;
import com.pawwithu.connectdog.domain.post.entity.Post;
import com.pawwithu.connectdog.domain.post.repository.PostRepository;
import com.pawwithu.connectdog.domain.review.entity.Review;
import com.pawwithu.connectdog.domain.review.repository.ReviewRepository;
import com.pawwithu.connectdog.domain.volunteer.entity.SocialType;
Expand Down Expand Up @@ -52,6 +58,11 @@ public class AuthService {
private final IntermediaryFcmRepository intermediaryFcmRepository;
private final BookmarkRepository bookmarkRepository;
private final VolunteerBadgeRepository volunteerBadgeRepository;
private final PostRepository postRepository;
private final IntermediaryNotificationRepository intermediaryNotificationRepository;
private final VolunteerNotificationRepository volunteerNotificationRepository;
private final DogStatusRepository dogStatusRepository;
private final DogStatusImageRepository dogStatusImageRepository;

public void volunteerSignUp(VolunteerSignUpRequest request) {

Expand Down Expand Up @@ -173,7 +184,7 @@ public void volunteersWithdraw(HttpServletRequest request, String email) {
volunteerFcmRepository.deleteByVolunteerId(volunteer.getId());
redisUtil.setBlackList(accessToken, "accessToken", jwtService.getAccessTokenExpirationPeriod());

Volunteer deletedVolunteer = volunteerRepository.findByEmail("deleted@connectdog.com").orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));
Volunteer deletedVolunteer = volunteerRepository.findByEmail("deletedVolunteer@connectdog.com").orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));
List<Review> reviews = reviewRepository.findByVolunteer(volunteer);
for (Review review : reviews) {
review.updateDeletedVolunteer(deletedVolunteer);
Expand All @@ -188,13 +199,48 @@ public void volunteersWithdraw(HttpServletRequest request, String email) {

bookmarkRepository.deleteByVolunteerId(volunteer.getId());
volunteerBadgeRepository.deleteByVolunteerId(volunteer.getId());
volunteerNotificationRepository.deleteByVolunteerId(volunteer.getId());
volunteerFcmRepository.deleteByVolunteerId(volunteer.getId());
volunteerRepository.delete(volunteer);
} catch (Exception e) {
log.error("봉사자 탈퇴 도중에 에러가 발생했습니다. {}", e.getMessage());
throw new BadRequestException(VOLUNTEER_WITHDRAW_FAILED);
}
}

public void intermediariesWithdraw(HttpServletRequest request, String email) {
String accessToken = jwtService.extractAccessToken(request).orElseThrow(() -> new BadRequestException(TOKEN_NOT_EXIST));
Intermediary intermediary = intermediaryRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));
String roleName = jwtService.extractRoleName(accessToken).orElseThrow(() -> new BadRequestException(NOT_FOUND_ROLE_NAME));

try {
redisUtil.delete(roleName, intermediary.getId());
volunteerFcmRepository.deleteByVolunteerId(intermediary.getId());
redisUtil.setBlackList(accessToken, "accessToken", jwtService.getAccessTokenExpirationPeriod());

Intermediary deletedIntermediary = intermediaryRepository.findByEmail("deletedIntermediary@connectdog.com").orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));

List<Post> posts = postRepository.findByIntermediary(intermediary);
for (Post post : posts) {
post.updateDeletedIntermediary(deletedIntermediary);
}

List<Application> applications = applicationRepository.findByIntermediary(intermediary);
for (Application application : applications) {
application.updateDeletedIntermediary(deletedIntermediary);
}

entityManager.flush();

intermediaryNotificationRepository.deleteByIntermediaryId(intermediary.getId());
intermediaryFcmRepository.deleteByIntermediaryId(intermediary.getId());
intermediaryRepository.delete(intermediary);
} catch (Exception e) {
log.error("모집자 탈퇴 도중에 에러가 발생했습니다. {}", e.getMessage());
throw new BadRequestException(INTERMEDIARY_WITHDRAW_FAILED);
}
}

@Transactional(readOnly = true)
public IntermediaryNameResponse isIntermediaryNameDuplicated(IntermediaryNameRequest request) {
Boolean isDuplicated = intermediaryRepository.existsByName(request.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface IntermediaryNotificationRepository extends JpaRepository<IntermediaryNotification, Long> {

Optional<IntermediaryNotification> findByIdAndIntermediaryId(Long id, Long intermediaryId);

void deleteByIntermediaryId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface VolunteerNotificationRepository extends JpaRepository<VolunteerNotification, Long> {

Optional<VolunteerNotification> findByIdAndVolunteerId(Long id, Long intermediaryId);

void deleteByVolunteerId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public void updatePost(String departureLoc, String arrivalLoc, LocalDate startDa
this.isKennel = isKennel;
this.content = content;
}

public void updateDeletedIntermediary(Intermediary deletedIntermediary) {
this.intermediary = deletedIntermediary;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.pawwithu.connectdog.domain.post.repository;

import com.pawwithu.connectdog.domain.intermediary.entity.Intermediary;
import com.pawwithu.connectdog.domain.post.entity.Post;
import com.pawwithu.connectdog.domain.post.entity.PostStatus;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface PostRepository extends JpaRepository<Post, Long> {

Optional<Post> findByIdAndIntermediaryId(Long id, Long intermediaryId);
Optional<Post> findByIdAndStatus(Long id, PostStatus postStatus);

List<Post> findByIntermediary(Intermediary intermediary);
}
Loading
Loading