Skip to content

Commit

Permalink
MAN-156 - update api and test
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Nov 15, 2024
1 parent c497fd3 commit 3c3e9a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<AppointmentDetail>()
val response = createAppointment()

mockMvc
.perform(MockMvcRequestBuilders.patch("/appointment")
Expand All @@ -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<AppointmentDetail>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 3c3e9a4

Please sign in to comment.