Fix stuck pet status
This commit is contained in:
@@ -22,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AdoptionService {
|
public class AdoptionService {
|
||||||
@@ -159,15 +160,37 @@ public class AdoptionService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAdoption(Long id) {
|
public void deleteAdoption(Long id) {
|
||||||
if (!adoptionRepository.existsById(id)) {
|
Adoption adoption = adoptionRepository.findById(id)
|
||||||
throw new ResourceNotFoundException("Adoption not found with id: " + id);
|
.orElseThrow(() -> new ResourceNotFoundException("Adoption not found with id: " + id));
|
||||||
}
|
Pet pet = adoption.getPet();
|
||||||
adoptionRepository.deleteById(id);
|
String status = adoption.getAdoptionStatus();
|
||||||
|
adoptionRepository.delete(adoption);
|
||||||
|
resetPetIfPending(pet, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void bulkDeleteAdoptions(BulkDeleteRequest request) {
|
public void bulkDeleteAdoptions(BulkDeleteRequest request) {
|
||||||
adoptionRepository.deleteAllById(request.getIds());
|
List<Adoption> adoptions = adoptionRepository.findAllById(request.getIds());
|
||||||
|
adoptionRepository.deleteAll(adoptions);
|
||||||
|
for (Adoption adoption : adoptions) {
|
||||||
|
resetPetIfPending(adoption.getPet(), adoption.getAdoptionStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetPetIfPending(Pet pet, String deletedAdoptionStatus) {
|
||||||
|
if (!ADOPTION_STATUS_PENDING.equalsIgnoreCase(deletedAdoptionStatus)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!PET_STATUS_PENDING.equalsIgnoreCase(pet.getPetStatus())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean completedElsewhere = adoptionRepository.existsByPet_IdAndAdoptionStatusIgnoreCase(
|
||||||
|
pet.getPetId(), ADOPTION_STATUS_COMPLETED);
|
||||||
|
if (!completedElsewhere) {
|
||||||
|
pet.setPetStatus(PET_STATUS_AVAILABLE);
|
||||||
|
pet.setOwner(null);
|
||||||
|
petRepository.save(pet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String normalizeFilter(String value) {
|
private String normalizeFilter(String value) {
|
||||||
|
|||||||
Reference in New Issue
Block a user