From 1c24fb6b325b2a74b373ef9ed89afee5cd576e12 Mon Sep 17 00:00:00 2001 From: Amardeep Chimber Date: Wed, 20 Nov 2024 13:05:04 +0000 Subject: [PATCH] MAN-110 - update logic and test --- .../CreateAppointmentIntegrationTests.kt | 3 +- .../model/appointment/CreateAppointment.kt | 5 ++++ .../service/SentenceAppointmentService.kt | 30 +++++++++++-------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt index acd094b1e3..1dd1b44def 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CreateAppointmentIntegrationTests.kt @@ -179,11 +179,12 @@ class CreateAppointmentIntegrationTests { .andReturn().response.contentAsJson() - val response1 = mockMvc.perform( + val conflict = mockMvc.perform( post("/appointment/${person.crn}") .withToken() .withJson(appointment)) .andExpect(MockMvcResultMatchers.status().isConflict) +// .andExpect(jsonPath("$.message", equalTo("Appointment conflicts with an existing future appointment"))) val appointments = appointmentRepository.findAllById(response.appointments.map { it.id }) appointmentRepository.deleteAll(appointments) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/appointment/CreateAppointment.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/appointment/CreateAppointment.kt index 28d500cfc4..e0fdba1857 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/appointment/CreateAppointment.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/appointment/CreateAppointment.kt @@ -43,3 +43,8 @@ data class User( val username: String, val team: String ) + +data class OverlappingAppointment( + val start: String, + val end: String, +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentService.kt index 6c3c21a66b..f0ddeb553d 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentService.kt @@ -6,6 +6,7 @@ import uk.gov.justice.digital.hmpps.api.model.appointment.CreateAppointment import uk.gov.justice.digital.hmpps.api.model.appointment.CreatedAppointment import uk.gov.justice.digital.hmpps.audit.service.AuditableService import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService +import uk.gov.justice.digital.hmpps.datetime.DeliusDateTimeFormatter import uk.gov.justice.digital.hmpps.datetime.EuropeLondon import uk.gov.justice.digital.hmpps.exception.ConflictException import uk.gov.justice.digital.hmpps.exception.InvalidRequestException @@ -36,7 +37,9 @@ class SentenceAppointmentService( return audit(BusinessInteractionCode.ADD_CONTACT) { audit -> val om = offenderManagerRepository.getByCrn(crn) audit["offenderId"] = om.person.id - checkForConflicts(om.person.id, createAppointment) + + checkForConflicts(createAppointment) + val userAndLocation = staffUserRepository.getUserAndLocation(createAppointment.user.username, createAppointment.user.team) val createAppointments: ArrayList = arrayListOf() @@ -71,6 +74,20 @@ class SentenceAppointmentService( } } + createAppointments.forEach { + if (it.start.isAfter(ZonedDateTime.now()) && appointmentRepository.appointmentClashes( + om.person.id, + it.start.toLocalDate(), + it.start, + it.end + ) + ) { + throw ConflictException("Appointment conflicts with an existing future appointment ${createAppointment.start.toLocalDateTime().format( + DeliusDateTimeFormatter).dropLast(3)} and ${createAppointment.end.toLocalDateTime().format( + DeliusDateTimeFormatter).dropLast(3)}") + } + } + val appointments = createAppointments.map { it.withManager(om, userAndLocation) } val savedAppointments = appointmentRepository.saveAll(appointments) val createdAppointments = savedAppointments.map { CreatedAppointment(it.id) } @@ -81,7 +98,6 @@ class SentenceAppointmentService( } private fun checkForConflicts( - personId: Long, createAppointment: CreateAppointment ) { if (createAppointment.requirementId != null && createAppointment.licenceConditionId != null) { @@ -110,16 +126,6 @@ class SentenceAppointmentService( throw NotFoundException("LicenceCondition", "licenceConditionId", createAppointment.licenceConditionId) } - if (createAppointment.start.isAfter(ZonedDateTime.now()) && appointmentRepository.appointmentClashes( - personId, - createAppointment.start.toLocalDate(), - createAppointment.start, - createAppointment.end - ) - ) { - throw ConflictException("Appointment conflicts with an existing future appointment") - } - val licenceOrRequirement = listOfNotNull(createAppointment.licenceConditionId, createAppointment.requirementId) if (licenceOrRequirement.size > 1) {