Skip to content
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
50 changes: 24 additions & 26 deletions src/main/java/com/josdem/vetlog/helper/VaccinationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ public class VaccinationHelper {
private static final String TRICAT_BOOST_VACCINE = "TRICAT_BOOST";
private static final String FELV_VACCINE = "FeLV";

private static final Map<String, String> NEXT_VACCINE = Map.of(
PUPPY_VACCINE, C4CV_VACCINE,
C4CV_VACCINE, C6CV_VACCINE,
TRICAT_VACCINE, TRICAT_BOOST_VACCINE);

private static final Map<String, java.time.Period> NEXT_VACCINE_OFFSET = Map.of(
PUPPY_VACCINE, java.time.Period.ofDays(15),
C4CV_VACCINE, java.time.Period.ofDays(15),
TRICAT_VACCINE, java.time.Period.ofDays(21));
private static final Map<String, Map<String, java.time.Period>> NEXT_VACCINE_AND_OFFSET = Map.of(
PUPPY_VACCINE, Map.of(C4CV_VACCINE, java.time.Period.ofDays(15)),
C4CV_VACCINE, Map.of(C6CV_VACCINE, java.time.Period.ofDays(15)),
TRICAT_VACCINE, Map.of(
TRICAT_BOOST_VACCINE, java.time.Period.ofDays(21),
FELV_VACCINE, java.time.Period.ofDays(21)
)
);

private static final Map<String, java.time.Period> NEXT_RABIES_VACCINE_OFFSET = Map.of(
TRICAT_VACCINE, java.time.Period.ofDays(21),
C6CV_VACCINE, java.time.Period.ofDays(15),
TRICAT_BOOST_VACCINE, java.time.Period.ofDays(21),
RABIES_VACCINE, java.time.Period.ofYears(1));
RABIES_VACCINE, java.time.Period.ofYears(1)
);

private final VaccinationRepository vaccinationRepository;

Expand All @@ -72,24 +72,23 @@ public void validateRabiesVaccine(List<Vaccination> previousVaccines, List<Vacci
&& previousVaccine.getStatus() == VaccinationStatus.PENDING)
&& isSpecificCriteriaSatisfiedForApplyingNextVaccine(appliedName, RABIES_VACCINE, pet)) {
saveNewVaccine(RABIES_VACCINE, LocalDate.now().plus(NEXT_RABIES_VACCINE_OFFSET.get(appliedName)), pet);
if (petNeedsLeukemiaVaccine(pet)) {
saveNewVaccine(FELV_VACCINE, LocalDate.now().plusDays(21), pet);
}
}
}
}

public void validateNextVaccines(List<Vaccination> previousVaccines, List<Vaccination> newVaccines, Pet pet) {
for (Vaccination newVaccine : newVaccines) {
String appliedName = newVaccine.getName();
if (NEXT_VACCINE.containsKey(appliedName)
if (NEXT_VACCINE_AND_OFFSET.containsKey(appliedName)
&& newVaccine.getStatus() == VaccinationStatus.APPLIED
&& previousVaccines.stream()
.anyMatch(previousVaccine -> appliedName.equalsIgnoreCase(previousVaccine.getName())
&& previousVaccine.getStatus() == VaccinationStatus.PENDING)) {
String nextName = NEXT_VACCINE.get(appliedName);
if (!isSpecificCriteriaSatisfiedForApplyingNextVaccine(appliedName, nextName, pet)) continue;
saveNewVaccine(nextName, LocalDate.now().plus(NEXT_VACCINE_OFFSET.get(appliedName)), pet);
Map<String, java.time.Period> nextNamesAndOffsets = NEXT_VACCINE_AND_OFFSET.get(appliedName);
for (Map.Entry<String, java.time.Period> nextNameAndOffset : nextNamesAndOffsets.entrySet()) {
if (!isSpecificCriteriaSatisfiedForApplyingNextVaccine(appliedName, nextNameAndOffset.getKey(), pet)) continue;
saveNewVaccine(nextNameAndOffset.getKey(), LocalDate.now().plus(nextNameAndOffset.getValue()), pet);
}
}
}
}
Expand All @@ -104,24 +103,23 @@ private boolean isSpecificCriteriaSatisfiedForApplyingNextVaccine(String applied
.map(birthDate -> ChronoUnit.WEEKS.between(birthDate, LocalDate.now()))
.map(weeks -> weeks >= 9 && weeks <= 16)
.orElse(false);
}
if (TRICAT_VACCINE.equalsIgnoreCase(appliedName) && RABIES_VACCINE.equalsIgnoreCase(nextName)) {
} else if (TRICAT_VACCINE.equalsIgnoreCase(appliedName) && RABIES_VACCINE.equalsIgnoreCase(nextName)) {
return Optional.ofNullable(pet)
.map(Pet::getBirthDate)
.map(dob -> ChronoUnit.DAYS.between(dob, LocalDate.now()))
.map(days -> days > (16 * 7))
.orElse(false);
} else if (TRICAT_VACCINE.equalsIgnoreCase(appliedName) && FELV_VACCINE.equalsIgnoreCase(nextName)) {
return Optional.ofNullable(pet)
.map(Pet::getBreed)
.map(Breed::getType)
.filter(PetType.CAT::equals)
.flatMap(type -> Optional.ofNullable(pet.getGoingOutOften()))
.orElse(false);
Comment on lines +112 to +118

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my understanding there was no age restriction specified of in #847 or implemented in #847, but could you confirm @josdem? I will update accordingly.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, however; do not worry, I asked copilot to address this for us, it did it here: #876

}
return true;
}

private boolean petNeedsLeukemiaVaccine(Pet pet) {
return Optional.ofNullable(pet)
.filter(p -> p.getBreed() != null && PetType.CAT.equals(p.getBreed().getType()))
.map(Pet::getGoingOutOften)
.orElse(false);
}

private void saveNewVaccine(String name, LocalDate date, Pet pet) {
Vaccination future = new Vaccination(null, name, date, VaccinationStatus.NEW, pet);
vaccinationRepository.save(future);
Expand Down
41 changes: 33 additions & 8 deletions src/test/java/com/josdem/vetlog/helper/VaccinationHelperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class VaccinationHelperTest {
}

@Test
fun `should not create TRICAT_BOOST 21 days later when TRICAT applied and pet is cat not aged 9 to 16 weeks`(testInfo: TestInfo) {
fun `should not create TRICAT_BOOST when TRICAT applied and pet is cat not aged 9 to 16 weeks`(testInfo: TestInfo) {
log.info(testInfo.displayName)
val pet = Pet()
val breed = Breed()
Expand All @@ -209,11 +209,16 @@ class VaccinationHelperTest {

vaccinationHelper.validateNextVaccines(listOf(previousVaccines), listOf(newVaccines), pet)

verify(vaccinationRepository, times(0)).save(any())
verify(vaccinationRepository, times(0)).save(
argThat { vaccination ->
vaccination.name == "TRICAT_BOOST" &&
vaccination.status == VaccinationStatus.NEW
},
)
}

@Test
fun `should not create TRICAT_BOOST 45 days later when TRICAT applied and pet is not cat`(testInfo: TestInfo) {
fun `should not create TRICAT_BOOST or FeLV when TRICAT applied and pet is not cat`(testInfo: TestInfo) {
log.info(testInfo.displayName)
val pet = Pet()
val breed = Breed()
Expand Down Expand Up @@ -293,7 +298,7 @@ class VaccinationHelperTest {
}

@Test
fun `should update tricat_boost vaccination status to APPLIED`(testInfo: TestInfo) {
fun `should update TRICAT_BOOST vaccination status to APPLIED`(testInfo: TestInfo) {
log.info(testInfo.displayName)
val previousVaccines = Vaccination(3L, "TRICAT_BOOST", LocalDate.now(), VaccinationStatus.PENDING, pet)
val newVaccines = Vaccination(3L, "TRICAT_BOOST", LocalDate.now(), VaccinationStatus.APPLIED, pet)
Expand All @@ -311,11 +316,9 @@ class VaccinationHelperTest {
}

@Test
fun `should create new FELV vaccine`(testInfo: TestInfo) {
fun `should create new FeLV vaccine when TRICAT is applied`(testInfo: TestInfo) {
log.info(testInfo.displayName)
val pet = Pet()
pet.id = 1L
pet.birthDate = LocalDate.now().minusWeeks(20)
pet.goingOutOften = true
pet.breed = Breed().apply { type = PetType.CAT }
val previousVaccines = Vaccination(1L, "TRICAT", LocalDate.now(), VaccinationStatus.PENDING, pet)
Expand All @@ -324,7 +327,7 @@ class VaccinationHelperTest {
val expectedDate = LocalDate.now().plusDays(21)
whenever(vaccinationRepository.findAllByPetId(1L)).thenReturn(listOf(previousVaccines))

vaccinationHelper.validateRabiesVaccine(listOf(previousVaccines), listOf(newVaccines), pet)
vaccinationHelper.validateNextVaccines(listOf(previousVaccines), listOf(newVaccines), pet)

verify(vaccinationRepository).save(
argThat { vaccination ->
Expand All @@ -335,4 +338,26 @@ class VaccinationHelperTest {
},
)
}

@Test
fun `should not create FeLV when TRICAT applied and pet is cat and not going out often`(testInfo: TestInfo) {
log.info(testInfo.displayName)
val pet = Pet()
val breed = Breed()
breed.type = PetType.CAT
pet.breed = breed
pet.goingOutOften = false
val previousVaccines = Vaccination(1L, "TRICAT", LocalDate.now(), VaccinationStatus.PENDING, pet)
val newVaccines = Vaccination(1L, "TRICAT", LocalDate.now(), VaccinationStatus.APPLIED, pet)
whenever(vaccinationRepository.findAllByPetId(1L)).thenReturn(listOf(previousVaccines))

vaccinationHelper.validateNextVaccines(listOf(previousVaccines), listOf(newVaccines), pet)

verify(vaccinationRepository, times(0)).save(
argThat { vaccination ->
vaccination.name == "FeLV" &&
vaccination.status == VaccinationStatus.NEW
},
)
}
}