From 3c3e9a434ef475b2c24bcd1a5b9f1efd0f8d2c80 Mon Sep 17 00:00:00 2001 From: Amardeep Chimber Date: Fri, 15 Nov 2024 16:32:45 +0000 Subject: [PATCH] MAN-156 - update api and test --- .../AppointmentOutcomeIntegrationTest.kt | 59 ++++++++++++++----- .../delius/sentence/entity/Appointment.kt | 3 + .../service/AppointmentOutcomeService.kt | 6 +- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AppointmentOutcomeIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AppointmentOutcomeIntegrationTest.kt index c3581bcf23..47fedf817c 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AppointmentOutcomeIntegrationTest.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AppointmentOutcomeIntegrationTest.kt @@ -1,6 +1,9 @@ package uk.gov.justice.digital.hmpps +import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc @@ -14,10 +17,15 @@ 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.Outcome import uk.gov.justice.digital.hmpps.api.model.appointment.User +import uk.gov.justice.digital.hmpps.data.generator.AppointmentGenerator.ATTENDED_COMPLIED +import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.DEFAULT_PROVIDER +import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.DEFAULT_LOCATION +import uk.gov.justice.digital.hmpps.data.generator.OffenderManagerGenerator.STAFF_1 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.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 @@ -58,20 +66,7 @@ class AppointmentOutcomeIntegrationTest { @Test fun `when an appointment outcome does not exist returns a 404 response`() { - val response = mockMvc.perform( - post("/appointment/${PersonGenerator.PERSON_1.crn}") - .withToken() - .withJson( - CreateAppointment( - User(STAFF_USER_1.username, TEAM.description), - CreateAppointment.Type.PlannedOfficeVisitNS, - ZonedDateTime.now().plusDays(1), - ZonedDateTime.now().plusDays(2), - eventId = PersonGenerator.EVENT_1.id, - uuid = UUID.randomUUID() - ) - ) - ).andReturn().response.contentAsJson() + val response = createAppointment() mockMvc .perform(MockMvcRequestBuilders.patch("/appointment") @@ -83,4 +78,40 @@ class AppointmentOutcomeIntegrationTest { appointmentRepository.deleteById(response.appointments[0].id) } + + @Test + fun `outcome updated`() { + val response = createAppointment() + val request = Outcome(response.appointments[0].id, "ATTC", false, "my notes") + + mockMvc + .perform(MockMvcRequestBuilders.patch("/appointment") + .withToken() + .withJson(request) + ) + .andExpect(MockMvcResultMatchers.status().isOk) + + val appointment = appointmentRepository.findById(response.appointments[0].id).get() + assertEquals("Y", appointment.attended) + assertEquals(request.notes, appointment.notes) + assertFalse(appointment.sensitive!!) + + + appointmentRepository.delete(appointment) + } + + private fun createAppointment() = mockMvc.perform( + post("/appointment/${PersonGenerator.PERSON_1.crn}") + .withToken() + .withJson( + CreateAppointment( + User(STAFF_USER_1.username, TEAM.description), + CreateAppointment.Type.PlannedOfficeVisitNS, + ZonedDateTime.now().plusDays(1), + ZonedDateTime.now().plusDays(2), + eventId = PersonGenerator.EVENT_1.id, + uuid = UUID.randomUUID() + ) + ) + ).andReturn().response.contentAsJson() } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Appointment.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Appointment.kt index c9f763db86..1731f4fac7 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Appointment.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/Appointment.kt @@ -80,6 +80,9 @@ class Appointment( @Column(name = "office_location_id") val officeLocationId: Long? = null, + @Column(name = "attended", columnDefinition = "char(1)") + var attended: String? = null, + @ManyToOne @JoinColumn(name = "contact_outcome_type_id") val outcome: ContactOutcome? = null, diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AppointmentOutcomeService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AppointmentOutcomeService.kt index 1a7f2e57b6..823fc930ab 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AppointmentOutcomeService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AppointmentOutcomeService.kt @@ -16,12 +16,14 @@ class AppointmentOutcomeService( fun recordOutcome(outcome: Outcome) { val appointment = appointmentRepository.findById(outcome.id).orElseThrow { throw NotFoundException("Appointment", "id", outcome.id) } - contactTypeOutcomeRepository.getByTypeIdAndOutcomeCode(appointment.type.id, outcome.code) + val contactTypeOutcome = contactTypeOutcomeRepository.getByTypeIdAndOutcomeCode(appointment.type.id, outcome.code) appointment.apply { - sensitive = appointment.sensitive + attended = if (contactTypeOutcome.type.attendanceContact) "Y" else "N" notes = outcome.notes + sensitive = outcome.sensitive } + appointmentRepository.save(appointment) } } \ No newline at end of file