diff --git a/src/main/java/com/moneymong/domain/agency/api/AgencyController.java b/src/main/java/com/moneymong/domain/agency/api/AgencyController.java index 70480fd..185ae1e 100644 --- a/src/main/java/com/moneymong/domain/agency/api/AgencyController.java +++ b/src/main/java/com/moneymong/domain/agency/api/AgencyController.java @@ -81,4 +81,13 @@ public void blockMember( public List getMyAgency(@AuthenticationPrincipal JwtAuthentication user) { return agencyService.getMyAgency(user.getId()); } + + @Operation(summary = "소속 삭제") + @DeleteMapping("/{agencyId}") + public void delete( + @AuthenticationPrincipal JwtAuthentication user, + @PathVariable("agencyId") Long agencyId + ) { + agencyService.delete(user.getId(), agencyId); + } } diff --git a/src/main/java/com/moneymong/domain/agency/service/AgencyService.java b/src/main/java/com/moneymong/domain/agency/service/AgencyService.java index 1bbcf28..02d90d1 100644 --- a/src/main/java/com/moneymong/domain/agency/service/AgencyService.java +++ b/src/main/java/com/moneymong/domain/agency/service/AgencyService.java @@ -4,7 +4,6 @@ import com.moneymong.domain.agency.api.response.*; import com.moneymong.domain.agency.entity.Agency; import com.moneymong.domain.agency.entity.AgencyUser; -import com.moneymong.domain.agency.entity.enums.AgencyType; import com.moneymong.domain.agency.entity.enums.AgencyUserRole; import com.moneymong.domain.agency.exception.BlockedAgencyUserException; import com.moneymong.domain.agency.repository.AgencyRepository; @@ -17,6 +16,7 @@ import com.moneymong.domain.invitationcode.repository.InvitationCodeRepository; import com.moneymong.domain.ledger.entity.Ledger; import com.moneymong.domain.ledger.repository.LedgerRepository; +import com.moneymong.domain.ledger.service.manager.LedgerService; import com.moneymong.domain.user.entity.UserUniversity; import com.moneymong.domain.user.repository.UserUniversityRepository; import com.moneymong.global.exception.custom.NotFoundException; @@ -45,6 +45,7 @@ public class AgencyService { private final LedgerRepository ledgerRepository; private final InvitationCodeRepository invitationCodeRepository; private final InvitationCodeCertificationRepository invitationCodeCertificationRepository; + private final LedgerService ledgerService; public SearchAgencyResponse getAgencyList(Long userId, Pageable pageable) { String universityName = getUniversityName(userId); @@ -133,4 +134,16 @@ public List getMyAgency(Long userId) { .map(AgencyResponse::from) .toList(); } + + @Transactional + public void delete(Long userId, Long agencyId) { + validateAgencyUserRole(userId, agencyId); + + Agency agency = agencyRepository.findById(agencyId) + .orElseThrow(() -> new NotFoundException(ErrorCode.AGENCY_NOT_FOUND)); + + ledgerService.deleteLedger(agencyId); + + agencyRepository.delete(agency); + } } diff --git a/src/main/java/com/moneymong/domain/ledger/service/manager/LedgerService.java b/src/main/java/com/moneymong/domain/ledger/service/manager/LedgerService.java index f31a77b..dccc9f2 100644 --- a/src/main/java/com/moneymong/domain/ledger/service/manager/LedgerService.java +++ b/src/main/java/com/moneymong/domain/ledger/service/manager/LedgerService.java @@ -139,4 +139,14 @@ private LedgerDetail getLedgerDetail(Long ledgerDetailId) { .findById(ledgerDetailId) .orElseThrow(() -> new NotFoundException(ErrorCode.LEDGER_DETAIL_NOT_FOUND)); } + + public void deleteLedger(Long agencyId) { + Ledger ledger = ledgerRepository.findByAgencyId(agencyId) + .orElseThrow(() -> new NotFoundException(ErrorCode.LEDGER_NOT_FOUND)); + + List ledgerDetails = ledgerDetailRepository.findAllByLedger(ledger); + + ledgerDetailRepository.deleteAll(ledgerDetails); + ledgerRepository.delete(ledger); + } }