Skip to content

Commit

Permalink
MAN-110 - update logic and test
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Nov 20, 2024
1 parent 1c24fb6 commit 3b0e74e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -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()
Expand All @@ -179,12 +182,19 @@ class CreateAppointmentIntegrationTests {
.andReturn().response.contentAsJson<AppointmentDetail>()


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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -74,18 +77,23 @@ class SentenceAppointmentService(
}
}

createAppointments.forEach {
val overlappingAppointments = createAppointments.mapNotNull {
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)}")
}
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) }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,6 +51,9 @@ class SentenceAppointmentServiceTest {
@Mock
lateinit var staffUserRepository: StaffUserRepository

@Mock
lateinit var objectMapper: ObjectMapper

@InjectMocks
lateinit var service: SentenceAppointmentService

Expand Down

0 comments on commit 3b0e74e

Please sign in to comment.