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 eda60f0 commit 1c24fb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,12 @@ class CreateAppointmentIntegrationTests {
.andReturn().response.contentAsJson<AppointmentDetail>()


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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ data class User(
val username: String,
val team: String
)

data class OverlappingAppointment(
val start: String,
val end: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<CreateAppointment> = arrayListOf()
Expand Down Expand Up @@ -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) }
Expand All @@ -81,7 +98,6 @@ class SentenceAppointmentService(
}

private fun checkForConflicts(
personId: Long,
createAppointment: CreateAppointment
) {
if (createAppointment.requirementId != null && createAppointment.licenceConditionId != null) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1c24fb6

Please sign in to comment.