diff --git a/src/main/java/team9502/sinchulgwinong/domain/companyUser/controller/CpUserController.java b/src/main/java/team9502/sinchulgwinong/domain/companyUser/controller/CpUserController.java index 6723f53..5a234aa 100644 --- a/src/main/java/team9502/sinchulgwinong/domain/companyUser/controller/CpUserController.java +++ b/src/main/java/team9502/sinchulgwinong/domain/companyUser/controller/CpUserController.java @@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; +import team9502.sinchulgwinong.domain.companyUser.dto.request.CpUserDeleteRequestDTO; import team9502.sinchulgwinong.domain.companyUser.dto.request.CpUserPasswordUpdateRequestDTO; import team9502.sinchulgwinong.domain.companyUser.dto.request.CpUserProfileUpdateRequestDTO; import team9502.sinchulgwinong.domain.companyUser.dto.response.CpUserPageResponseDTO; @@ -217,4 +218,38 @@ public ResponseEntity> usePointsForBanner( ) ); } + + @DeleteMapping + @Operation(summary = "기업 회원 탈퇴", description = "기업 회원을 삭제합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "기업 회원 탈퇴 성공", + content = @Content(mediaType = "application/json", + examples = @ExampleObject(value = "{\"message\": \"기업 회원 탈퇴 성공\", \"data\": null }"))), + @ApiResponse(responseCode = "400", description = "요청 처리 중 오류 발생", + content = @Content(mediaType = "application/json", + examples = { + @ExampleObject(name = "비밀번호 불일치", + value = "{\"message\": \"입력한 비밀번호가 기존 비밀번호와 일치하지 않습니다.\", \"data\": null }"), + @ExampleObject(name = "잘못된 사용자 유형", + value = "{\"message\": \"잘못된 사용자 유형입니다.\", \"data\": null }") + })), + @ApiResponse(responseCode = "404", description = "사용자를 찾을 수 없음", + content = @Content(mediaType = "application/json", + examples = @ExampleObject(value = "{\"message\": \"사용자를 찾을 수 없습니다.\", \"data\": null }"))), + @ApiResponse(responseCode = "500", description = "서버 내부 오류", + content = @Content(mediaType = "application/json", + examples = @ExampleObject(value = "{\"message\": \"서버 오류가 발생했습니다.\", \"data\": null }"))) + }) + public ResponseEntity> deleteCompanyUser( + @AuthenticationPrincipal UserDetailsImpl userDetails, + @RequestBody CpUserDeleteRequestDTO requestDTO) { + + cpUserService.deleteCpUser(userDetails.getCpUserId(), requestDTO); + + return ResponseEntity.status(SUCCESS_CP_USER_DELETED.getHttpStatus()) + .body( + GlobalApiResponse.of( + SUCCESS_CP_USER_DELETED.getMessage(), + null)); + } } diff --git a/src/main/java/team9502/sinchulgwinong/domain/companyUser/dto/request/CpUserDeleteRequestDTO.java b/src/main/java/team9502/sinchulgwinong/domain/companyUser/dto/request/CpUserDeleteRequestDTO.java new file mode 100644 index 0000000..cda6284 --- /dev/null +++ b/src/main/java/team9502/sinchulgwinong/domain/companyUser/dto/request/CpUserDeleteRequestDTO.java @@ -0,0 +1,15 @@ +package team9502.sinchulgwinong.domain.companyUser.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class CpUserDeleteRequestDTO { + + @Schema(description = "기업 회원 비밀번호", example = "Password1!") + private String password; +} diff --git a/src/main/java/team9502/sinchulgwinong/domain/companyUser/entity/CompanyUser.java b/src/main/java/team9502/sinchulgwinong/domain/companyUser/entity/CompanyUser.java index 8d3d52f..0f244aa 100644 --- a/src/main/java/team9502/sinchulgwinong/domain/companyUser/entity/CompanyUser.java +++ b/src/main/java/team9502/sinchulgwinong/domain/companyUser/entity/CompanyUser.java @@ -2,11 +2,13 @@ import jakarta.persistence.*; import lombok.*; +import team9502.sinchulgwinong.domain.jobBoard.entity.JobBoard; import team9502.sinchulgwinong.domain.point.CommonPoint; import team9502.sinchulgwinong.domain.point.entity.Point; import team9502.sinchulgwinong.global.entity.BaseTimeEntity; import java.time.LocalDate; +import java.util.List; @Entity @Getter @@ -71,6 +73,9 @@ public class CompanyUser extends BaseTimeEntity implements CommonPoint { @Column(nullable = false) private Integer viewCount = 0; + @OneToMany(mappedBy = "companyUser", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) + private List jobBoards; + public void incrementViewCount() { this.viewCount++; } diff --git a/src/main/java/team9502/sinchulgwinong/domain/companyUser/service/CpUserService.java b/src/main/java/team9502/sinchulgwinong/domain/companyUser/service/CpUserService.java index 301e367..ed43641 100644 --- a/src/main/java/team9502/sinchulgwinong/domain/companyUser/service/CpUserService.java +++ b/src/main/java/team9502/sinchulgwinong/domain/companyUser/service/CpUserService.java @@ -6,6 +6,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import team9502.sinchulgwinong.domain.companyUser.dto.request.CpUserDeleteRequestDTO; import team9502.sinchulgwinong.domain.companyUser.dto.request.CpUserPasswordUpdateRequestDTO; import team9502.sinchulgwinong.domain.companyUser.dto.request.CpUserProfileUpdateRequestDTO; import team9502.sinchulgwinong.domain.companyUser.dto.response.CpUserPageResponseDTO; @@ -14,6 +15,7 @@ import team9502.sinchulgwinong.domain.companyUser.entity.CompanyUser; import team9502.sinchulgwinong.domain.companyUser.repository.CompanyUserRepository; import team9502.sinchulgwinong.domain.email.service.EmailVerificationService; +import team9502.sinchulgwinong.domain.jobBoard.repository.JobBoardRepository; import team9502.sinchulgwinong.domain.point.enums.UpType; import team9502.sinchulgwinong.domain.point.service.PointService; import team9502.sinchulgwinong.global.exception.ApiException; @@ -31,6 +33,7 @@ public class CpUserService { private final PasswordEncoder passwordEncoder; private final EmailVerificationService emailVerificationService; private final PointService pointService; + private final JobBoardRepository jobBoardRepository; @Transactional public CpUserProfileResponseDTO getCpUserProfile(Long cpUserId) { @@ -152,4 +155,21 @@ private CpUserResponseDTO convertToDTO(CompanyUser companyUser) { companyUser.getViewCount() ); } + + @Transactional + public void deleteCpUser(Long cpUserId, CpUserDeleteRequestDTO requestDTO) { + + CompanyUser companyUser = companyUserRepository.findById(cpUserId) + .orElseThrow(() -> new ApiException(ErrorCode.COMPANY_USER_NOT_FOUND)); + + if (!passwordEncoder.matches(requestDTO.getPassword(), companyUser.getCpPassword())) { + throw new ApiException(ErrorCode.PASSWORD_MISMATCH); + } + + jobBoardRepository.deleteAll(companyUser.getJobBoards()); + + pointService.deletePointData(companyUser.getPoint()); + + companyUserRepository.delete(companyUser); + } } diff --git a/src/main/java/team9502/sinchulgwinong/global/response/SuccessCode.java b/src/main/java/team9502/sinchulgwinong/global/response/SuccessCode.java index 53bf6da..0386c60 100644 --- a/src/main/java/team9502/sinchulgwinong/global/response/SuccessCode.java +++ b/src/main/java/team9502/sinchulgwinong/global/response/SuccessCode.java @@ -40,6 +40,7 @@ public enum SuccessCode { SUCCESS_CP_USER_PROFILE_UPDATED(HttpStatus.OK, "기업(회원) 프로필 수정 성공"), SUCCESS_CP_USER_PASSWORD_UPDATED(HttpStatus.OK, "기업(회원) 비밀번호 수정 성공"), SUCCESS_CP_USER_ALL_READ(HttpStatus.OK, "기업(회원) 전체 조회 성공"), + SUCCESS_CP_USER_DELETED(HttpStatus.OK, "기업(회원) 회원 탈퇴 성공"), //Chat SUCCESS_CREATE_CHAT_ROOM(HttpStatus.CREATED, "채팅방 생성 성공"),