Skip to content

Commit

Permalink
PI-2035: Added activity and refactor (#3553)
Browse files Browse the repository at this point in the history
* PI-2035: Added activity and refactor
  • Loading branch information
pmcphee77 authored Mar 28, 2024
1 parent 9b6678f commit 09704be
Show file tree
Hide file tree
Showing 22 changed files with 432 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class DataLoader(
PersonGenerator.ACTIVE_ORDER,
PersonGenerator.INACTIVE_ORDER_1,
PersonGenerator.INACTIVE_ORDER_2,
ContactGenerator.BREACH_CONTACT_TYPE,
ContactGenerator.BREACH_ENFORCEMENT_ACTION,
ContactGenerator.APPT_CT_1,
ContactGenerator.OTHER_CT,
ContactGenerator.APPT_CT_2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.DEFAULT_BORO
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.DEFAULT_PROVIDER
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator.USER
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.*
import uk.gov.justice.digital.hmpps.integrations.delius.personalDetails.entity.ContactDocument
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
Expand All @@ -28,6 +28,9 @@ object ContactGenerator {
)
val DEFAULT_STAFF = generateStaff("N01BDT1", "John", "Smith")

val BREACH_CONTACT_TYPE = generateContactType("BRE02", false, "Breach Contact Type")
val BREACH_ENFORCEMENT_ACTION = generateEnforcementAction("BRE02", "Breach Enforcement Action", BREACH_CONTACT_TYPE)

val APPT_CT_1 = generateContactType("C089", true, "Alcohol Key Worker Session (NS)")
val OTHER_CT = generateContactType("XXXX", false, "Non attendance contact type")
val APPT_CT_2 = generateContactType("CODI", true, "Initial Appointment on Doorstep (NS)")
Expand All @@ -37,7 +40,8 @@ object ContactGenerator {
OVERVIEW,
APPT_CT_1,
ZonedDateTime.of(LocalDateTime.now().minusDays(1), ZoneId.of("Europe/London")),
attended = false
attended = false,
action = BREACH_ENFORCEMENT_ACTION
)
val PREVIOUS_APPT_CONTACT = generateContact(
OVERVIEW,
Expand All @@ -58,25 +62,52 @@ object ContactGenerator {
OVERVIEW,
APPT_CT_3,
ZonedDateTime.of(LocalDateTime.now().plusHours(3), ZoneId.of("Europe/London")),
linkedDocumentContactId = IdGenerator.getAndIncrement()
)

val CONTACT_DOCUMENT_1 = PersonDetailsGenerator.generateDocument(
val CONTACT_DOCUMENT_1 = generateContactDocument(
OVERVIEW.id,
"B001",
"contact.doc",
"DOCUMENT",
primaryKeyId = NEXT_APPT_CONTACT.linkedDocumentContactId

primaryKeyId = NEXT_APPT_CONTACT.id,
contact = NEXT_APPT_CONTACT
)
val CONTACT_DOCUMENT_2 = PersonDetailsGenerator.generateDocument(
val CONTACT_DOCUMENT_2 = generateContactDocument(
OVERVIEW.id,
"B002",
"contact2.doc",
"DOCUMENT",
primaryKeyId = NEXT_APPT_CONTACT.linkedDocumentContactId
primaryKeyId = NEXT_APPT_CONTACT.id,
contact = NEXT_APPT_CONTACT
)

fun generateContactDocument(
personId: Long,
alfrescoId: String,
name: String,
documentType: String,
primaryKeyId: Long? = null,
contact: Contact?
): ContactDocument {
val doc = ContactDocument(contact)
doc.id = IdGenerator.getAndIncrement()
doc.lastUpdated = ZonedDateTime.now().minusDays(1)
doc.alfrescoId = alfrescoId
doc.name = name
doc.personId = personId
doc.primaryKeyId = primaryKeyId
doc.type = documentType
return doc
}

fun generateEnforcementAction(code: String, description: String, contactType: ContactType) =
EnforcementAction(
id = IdGenerator.getAndIncrement(),
code = code,
description = description,
contactType = contactType
)

fun generateContact(
person: Person,
contactType: ContactType,
Expand All @@ -87,7 +118,7 @@ object ContactGenerator {
sensitive: Boolean? = null,
requirement: Requirement? = null,
notes: String? = null,
linkedDocumentContactId: Long? = null
action: EnforcementAction? = null
) = Contact(
id = IdGenerator.getAndIncrement(),
personId = person.id,
Expand All @@ -104,7 +135,7 @@ object ContactGenerator {
staff = DEFAULT_STAFF,
location = LOCATION_BRK_1,
notes = notes,
linkedDocumentContactId = linkedDocumentContactId
action = action
)

private fun generateContactType(code: String, attendance: Boolean, description: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,17 @@ object PersonDetailsGenerator {
name: String,
documentType: String,
primaryKeyId: Long? = null
) = PersonDocument(
id = IdGenerator.getAndIncrement(),
lastUpdated = ZonedDateTime.now().minusDays(1),
alfrescoId = alfrescoId,
name = name,
personId = personId,
primaryKeyId = primaryKeyId,
type = documentType
)
): PersonDocument {
val doc = PersonDocument()
doc.id = IdGenerator.getAndIncrement()
doc.lastUpdated = ZonedDateTime.now().minusDays(1)
doc.alfrescoId = alfrescoId
doc.name = name
doc.personId = personId
doc.primaryKeyId = primaryKeyId
doc.type = documentType
return doc
}

fun generateAlias(forename: String, secondName: String, surname: String, personId: Long) = Alias(
id = IdGenerator.getAndIncrement(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package uk.gov.justice.digital.hmpps

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.api.model.activity.PersonActivity
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW
import uk.gov.justice.digital.hmpps.service.toActivity
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = RANDOM_PORT)
internal class ActivityIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Test
fun `all person activity is returned`() {

val person = OVERVIEW
val res = mockMvc
.perform(get("/activity/${person.crn}").withToken())
.andExpect(status().isOk)
.andReturn().response.contentAsJson<PersonActivity>()

assertThat(res.personSummary.crn, equalTo(person.crn))
assertThat(res.activities.size, equalTo(7))
assertThat(res.activities[2].id, equalTo(ContactGenerator.FIRST_APPT_CONTACT.toActivity().id))
assertThat(res.activities[2].type, equalTo(ContactGenerator.FIRST_APPT_CONTACT.toActivity().type))
assertThat(
res.activities[2].location?.officeName,
equalTo(ContactGenerator.FIRST_APPT_CONTACT.toActivity().location?.officeName)
)
assertThat(res.activities[2].location?.postcode, equalTo("H34 7TH"))
assertThat(res.activities[2].isAppointment, equalTo(true))
assertThat(res.activities[1].documents.size, equalTo(2))
assertThat(res.activities[3].isAppointment, equalTo(false))
assertThat(res.activities[0].documents.size, equalTo(0))
assertThat(res.activities[0].action, equalTo("Breach Enforcement Action"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import uk.gov.justice.digital.hmpps.api.model.schedule.PersonAppointment
import uk.gov.justice.digital.hmpps.api.model.schedule.Schedule
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW
import uk.gov.justice.digital.hmpps.service.toAppointment
import uk.gov.justice.digital.hmpps.service.toActivity
import uk.gov.justice.digital.hmpps.service.toDocument
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
Expand All @@ -35,11 +35,11 @@ internal class ScheduleIntegrationTest {
.andReturn().response.contentAsJson<Schedule>()

assertThat(res.personSummary.crn, equalTo(person.crn))
assertThat(res.appointments[0].id, equalTo(ContactGenerator.FIRST_APPT_CONTACT.toAppointment().id))
assertThat(res.appointments[0].type, equalTo(ContactGenerator.FIRST_APPT_CONTACT.toAppointment().type))
assertThat(res.appointments[0].id, equalTo(ContactGenerator.FIRST_APPT_CONTACT.toActivity().id))
assertThat(res.appointments[0].type, equalTo(ContactGenerator.FIRST_APPT_CONTACT.toActivity().type))
assertThat(
res.appointments[0].location?.officeName,
equalTo(ContactGenerator.FIRST_APPT_CONTACT.toAppointment().location?.officeName)
equalTo(ContactGenerator.FIRST_APPT_CONTACT.toActivity().location?.officeName)
)
assertThat(res.appointments[0].location?.postcode, equalTo("H34 7TH"))
}
Expand All @@ -53,14 +53,14 @@ internal class ScheduleIntegrationTest {
.andExpect(status().isOk)
.andReturn().response.contentAsJson<Schedule>()
assertThat(res.personSummary.crn, equalTo(person.crn))
assertThat(res.appointments[0].id, equalTo(ContactGenerator.PREVIOUS_APPT_CONTACT_ABSENT.toAppointment().id))
assertThat(res.appointments[0].id, equalTo(ContactGenerator.PREVIOUS_APPT_CONTACT_ABSENT.toActivity().id))
assertThat(
res.appointments[0].type,
equalTo(ContactGenerator.PREVIOUS_APPT_CONTACT_ABSENT.toAppointment().type)
equalTo(ContactGenerator.PREVIOUS_APPT_CONTACT_ABSENT.toActivity().type)
)
assertThat(
res.appointments[0].location?.officeName,
equalTo(ContactGenerator.PREVIOUS_APPT_CONTACT_ABSENT.toAppointment().location?.officeName)
equalTo(ContactGenerator.PREVIOUS_APPT_CONTACT_ABSENT.toActivity().location?.officeName)
)
assertThat(res.appointments[0].location?.postcode, equalTo("H34 7TH"))
}
Expand Down Expand Up @@ -90,7 +90,7 @@ internal class ScheduleIntegrationTest {
.andReturn().response.contentAsJson<PersonAppointment>()
val expectedDocs =
listOf(ContactGenerator.CONTACT_DOCUMENT_1.toDocument(), ContactGenerator.CONTACT_DOCUMENT_2.toDocument())
val expectedAppointment = ContactGenerator.NEXT_APPT_CONTACT.toAppointment().copy(documents = expectedDocs)
val expectedAppointment = ContactGenerator.NEXT_APPT_CONTACT.toActivity().copy(documents = expectedDocs)
assertThat(res.personSummary.crn, equalTo(person.crn))
assertThat(res.appointment.id, equalTo(expectedAppointment.id))
assertThat(res.appointment.type, equalTo(expectedAppointment.type))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.digital.hmpps.api.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.ActivityService

@RestController
@Tag(name = "Activity")
@RequestMapping("/activity/{crn}")
@PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')")
class ActivityController(private val activityService: ActivityService) {

@GetMapping
@Operation(summary = "Gets all activity for a person’ ")
fun getPersonActivity(@PathVariable crn: String) = activityService.getPersonActivity(crn)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.ScheduleService

@RestController
@Tag(name = "Sentence")
@Tag(name = "Schedule")
@RequestMapping("/schedule/{crn}")
@PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')")
class ScheduleController(private val scheduleService: ScheduleService) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package uk.gov.justice.digital.hmpps.api.model.schedule
package uk.gov.justice.digital.hmpps.api.model.activity

import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.personalDetails.Document
import uk.gov.justice.digital.hmpps.api.model.schedule.OfficeAddress
import java.time.ZonedDateTime

data class Appointment(
data class Activity(

val id: Long,
val type: String,
Expand All @@ -20,6 +21,8 @@ data class Appointment(
val isNationalStandard: Boolean,
var location: OfficeAddress? = null,
val rescheduled: Boolean,
val rescheduledStaff: Boolean,
val rescheduledPop: Boolean,
val didTheyComply: Boolean?,
val absentWaitingEvidence: Boolean?,
val rearrangeOrCancelReason: String?,
Expand All @@ -30,6 +33,13 @@ data class Appointment(
val rarCategory: String?,
val acceptableAbsence: Boolean?,
val acceptableAbsenceReason: String?,
val isAppointment: Boolean = false,
val action: String?,
val isSystemContact: Boolean? = false,
val isEmailOrTextFromPop: Boolean? = false,
val isPhoneCallFromPop: Boolean? = false,
val isEmailOrTextToPop: Boolean? = false,
val isPhoneCallToPop: Boolean? = false,
val lastUpdated: ZonedDateTime,
val lastUpdatedBy: Name
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.justice.digital.hmpps.api.model.activity

import uk.gov.justice.digital.hmpps.api.model.PersonSummary

data class PersonActivity(
val personSummary: PersonSummary,
val activities: List<Activity>
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package uk.gov.justice.digital.hmpps.api.model.schedule

data class OfficeAddress(
val officeName: String,
val officeName: String?,
val buildingName: String?,
val buildingNumber: String?,
val streetName: String?,
Expand All @@ -14,7 +14,7 @@ data class OfficeAddress(
) {
companion object {
fun from(
officeName: String,
officeName: String?,
buildingName: String?,
buildingNumber: String?,
streetName: String?,
Expand All @@ -26,7 +26,7 @@ data class OfficeAddress(
telephoneNumber: String?
): OfficeAddress? =
if (
buildingName == null && buildingNumber == null && streetName == null &&
officeName == null && buildingName == null && buildingNumber == null && streetName == null &&
district == null && town == null && county == null && postcode == null
) {
null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package uk.gov.justice.digital.hmpps.api.model.schedule

import uk.gov.justice.digital.hmpps.api.model.PersonSummary
import uk.gov.justice.digital.hmpps.api.model.activity.Activity

data class PersonAppointment(
val personSummary: PersonSummary,
val appointment: Appointment
val appointment: Activity
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package uk.gov.justice.digital.hmpps.api.model.schedule

import uk.gov.justice.digital.hmpps.api.model.PersonSummary
import uk.gov.justice.digital.hmpps.api.model.activity.Activity

data class Schedule(
val personSummary: PersonSummary,
val appointments: List<Appointment>
val appointments: List<Activity>
)
Loading

0 comments on commit 09704be

Please sign in to comment.