Fix dialog issues
This commit is contained in:
@@ -81,6 +81,9 @@ public class AdoptionService {
|
||||
: null;
|
||||
String adoptionStatus = normalizeAdoptionStatus(request.getAdoptionStatus());
|
||||
validatePetAvailability(pet, null, null);
|
||||
if (ADOPTION_STATUS_COMPLETED.equalsIgnoreCase(adoptionStatus) && request.getAdoptionDate() != null && request.getAdoptionDate().isAfter(LocalDate.now())) {
|
||||
throw new IllegalArgumentException("Cannot mark a future-dated adoption as Completed");
|
||||
}
|
||||
|
||||
Adoption adoption = new Adoption();
|
||||
adoption.setPet(pet);
|
||||
@@ -91,7 +94,7 @@ public class AdoptionService {
|
||||
adoption.setAdoptionStatus(adoptionStatus);
|
||||
|
||||
adoption = adoptionRepository.save(adoption);
|
||||
syncPetStatus(pet, adoptionStatus, adoption.getAdoptionId());
|
||||
syncPetStatus(pet, adoptionStatus, adoption.getAdoptionId(), customer);
|
||||
return mapToResponse(adoption);
|
||||
}
|
||||
|
||||
@@ -113,6 +116,9 @@ public class AdoptionService {
|
||||
String adoptionStatus = normalizeAdoptionStatus(request.getAdoptionStatus());
|
||||
Long currentPetId = adoption.getPet() != null ? adoption.getPet().getPetId() : null;
|
||||
validatePetAvailability(pet, adoption.getAdoptionId(), currentPetId);
|
||||
if (ADOPTION_STATUS_COMPLETED.equalsIgnoreCase(adoptionStatus) && request.getAdoptionDate() != null && request.getAdoptionDate().isAfter(LocalDate.now())) {
|
||||
throw new IllegalArgumentException("Cannot mark a future-dated adoption as Completed");
|
||||
}
|
||||
|
||||
adoption.setPet(pet);
|
||||
adoption.setCustomer(customer);
|
||||
@@ -122,7 +128,7 @@ public class AdoptionService {
|
||||
adoption.setAdoptionStatus(adoptionStatus);
|
||||
|
||||
adoption = adoptionRepository.save(adoption);
|
||||
syncPetStatus(pet, adoptionStatus, adoption.getAdoptionId());
|
||||
syncPetStatus(pet, adoptionStatus, adoption.getAdoptionId(), customer);
|
||||
return mapToResponse(adoption);
|
||||
}
|
||||
|
||||
@@ -216,13 +222,16 @@ public class AdoptionService {
|
||||
}
|
||||
}
|
||||
|
||||
private void syncPetStatus(Pet pet, String adoptionStatus, Long adoptionId) {
|
||||
private void syncPetStatus(Pet pet, String adoptionStatus, Long adoptionId, User customer) {
|
||||
boolean completedElsewhere = adoptionId != null
|
||||
&& adoptionRepository.existsByPet_IdAndAdoptionStatusIgnoreCaseAndAdoptionIdNot(pet.getPetId(), ADOPTION_STATUS_COMPLETED, adoptionId);
|
||||
if (ADOPTION_STATUS_COMPLETED.equalsIgnoreCase(adoptionStatus) || completedElsewhere) {
|
||||
pet.setPetStatus(PET_STATUS_ADOPTED);
|
||||
pet.setOwner(customer);
|
||||
pet.setStore(null);
|
||||
} else {
|
||||
pet.setPetStatus(PET_STATUS_AVAILABLE);
|
||||
pet.setOwner(null);
|
||||
}
|
||||
petRepository.save(pet);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user