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 1dd1b44def..90c3700d0b 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 @@ -23,12 +23,15 @@ import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.STAF import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.STAFF_USER_1 import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.TEAM import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator +import uk.gov.justice.digital.hmpps.datetime.DeliusDateFormatter +import uk.gov.justice.digital.hmpps.datetime.EuropeLondon import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AppointmentRepository import uk.gov.justice.digital.hmpps.test.CustomMatchers.isCloseTo import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken import java.time.LocalDate +import java.time.LocalTime import java.time.ZonedDateTime import java.util.* @@ -161,8 +164,8 @@ class CreateAppointmentIntegrationTests { val appointment = CreateAppointment( Companion.user, CreateAppointment.Type.HomeVisitToCaseNS, - ZonedDateTime.now().plusHours(1), - ZonedDateTime.now().plusHours(2), + ZonedDateTime.of(LocalDate.now().plusDays(1), LocalTime.NOON, EuropeLondon), + ZonedDateTime.of(LocalDate.now().plusDays(1), LocalTime.NOON.plusHours(1), EuropeLondon), numberOfAppointments = 3, eventId = PersonGenerator.EVENT_1.id, uuid = UUID.randomUUID() @@ -179,12 +182,19 @@ class CreateAppointmentIntegrationTests { .andReturn().response.contentAsJson() - val conflict = mockMvc.perform( + val dateNowPlusOneDay = LocalDate.now().plusDays(1).format(DeliusDateFormatter) + val dateNowPlusTwoDays = LocalDate.now().plusDays(2).format(DeliusDateFormatter) + val dateNowPlusThreeDays = LocalDate.now().plusDays(3).format(DeliusDateFormatter) + + val errorMsg = """ + Appointment(s) conflicts with an existing future appointment [{"start":"$dateNowPlusOneDay 12:00","end":"$dateNowPlusOneDay 13:00"},{"start":"$dateNowPlusTwoDays 12:00","end":"$dateNowPlusTwoDays 13:00"},{"start":"$dateNowPlusThreeDays 12:00","end":"$dateNowPlusThreeDays 13:00"}] + """.trimIndent() + mockMvc.perform( post("/appointment/${person.crn}") .withToken() .withJson(appointment)) .andExpect(MockMvcResultMatchers.status().isConflict) -// .andExpect(jsonPath("$.message", equalTo("Appointment conflicts with an existing future appointment"))) + .andExpect(jsonPath("$.message", equalTo(errorMsg))) 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/service/SentenceAppointmentService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentService.kt index f0ddeb553d..8b07bd9b59 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 @@ -1,9 +1,11 @@ package uk.gov.justice.digital.hmpps.service +import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.api.model.appointment.AppointmentDetail 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.api.model.appointment.OverlappingAppointment 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 @@ -28,7 +30,8 @@ class SentenceAppointmentService( private val eventSentenceRepository: EventSentenceRepository, private val requirementRepository: RequirementRepository, private val licenceConditionRepository: LicenceConditionRepository, - private val staffUserRepository: StaffUserRepository + private val staffUserRepository: StaffUserRepository, + private val objectMapper: ObjectMapper ) : AuditableService(auditedInteractionService) { fun createAppointment( crn: String, @@ -74,7 +77,7 @@ class SentenceAppointmentService( } } - createAppointments.forEach { + val overlappingAppointments = createAppointments.mapNotNull { if (it.start.isAfter(ZonedDateTime.now()) && appointmentRepository.appointmentClashes( om.person.id, it.start.toLocalDate(), @@ -82,10 +85,15 @@ class SentenceAppointmentService( 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)}") - } + OverlappingAppointment( + it.start.toLocalDateTime().format(DeliusDateTimeFormatter).dropLast(3), + it.end.toLocalDateTime().format(DeliusDateTimeFormatter).dropLast(3) + ) + } else null + } + + if (overlappingAppointments.isNotEmpty()) { + throw ConflictException("Appointment(s) conflicts with an existing future appointment ${objectMapper.writeValueAsString(overlappingAppointments)}") } val appointments = createAppointments.map { it.withManager(om, userAndLocation) } diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentServiceTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentServiceTest.kt index 7a1fd50db3..0665a5fef5 100644 --- a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentServiceTest.kt +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/SentenceAppointmentServiceTest.kt @@ -1,5 +1,6 @@ package uk.gov.justice.digital.hmpps.service +import com.fasterxml.jackson.databind.ObjectMapper import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Test @@ -50,6 +51,9 @@ class SentenceAppointmentServiceTest { @Mock lateinit var staffUserRepository: StaffUserRepository + @Mock + lateinit var objectMapper: ObjectMapper + @InjectMocks lateinit var service: SentenceAppointmentService