Skip to content

Commit

Permalink
PI-2101: Added licence conditions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 committed Aug 7, 2024
1 parent 7804300 commit fb2f2a7
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ object SentenceGenerator {
) = LicenceCondition(
disposal,
LocalDate.now(),
LocalDate.now(),
"commencement notes",
LocalDate.now(),
"termination notes",
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS),
ReferenceDataGenerator.LIC_COND_MAIN_CAT,
ReferenceDataGenerator.LIC_COND_SUB_CAT,
"Licence Condition notes",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"licenceConditions": [
{
"licenceConditionNotes": "Licence Condition notes",
"startDate": "2024-08-07",
"commencementDate": "2024-08-07",
"commencementNotes": "commencement notes",
"terminationDate": "2024-08-07",
"terminationNotes": "termination notes",
"createdDateTime": "2024-08-07T11:36:08.308673",
"active": true,
"licenceConditionTypeMainCat": {
"code": "LicMain",
"description": "lic cond main"
},
"licenceConditionTypeSubCat": {
"code": "LicSub",
"description": "Lic Sub cat"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
"mappings": [
{
"request": {
"method": "GET",
"urlPathTemplate": "/secure/offenders/crn/{crn}/convictions/{convictionId}/licenceConditions"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName": "get_licence_conditions_C123456.json"
}
},
{
"request": {
"method": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPat
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.api.model.Attendances
import uk.gov.justice.digital.hmpps.api.model.KeyValue
import uk.gov.justice.digital.hmpps.api.model.LicenceConditions
import uk.gov.justice.digital.hmpps.api.model.conviction.*
import uk.gov.justice.digital.hmpps.data.generator.AdditionalSentenceGenerator.SENTENCE_DISQ
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.ATTENDANCE_CONTACT_1
Expand Down Expand Up @@ -297,6 +298,20 @@ internal class ConvictionByCrnAndEventIdIntegrationTest {
.andReturn().response.contentAsJson<List<CourtReportMinimal>>()

assertThat(response.size, equalTo(1))
assertThat(response[0].reportManagers?.size, equalTo(1))
assertThat(response[0].reportManagers.size, equalTo(1))
}

@Test
fun `call convictions by id and licence conditions`() {
val crn = PersonGenerator.CURRENTLY_MANAGED.crn
val event = SentenceGenerator.CURRENTLY_MANAGED

val response = mockMvc
.perform(get("/probation-case/$crn/convictions/${event.id}/licenceConditions").withToken())
.andExpect(status().isOk)
.andReturn().response.contentAsJson<LicenceConditions>()

assertThat(response.licenceConditions.size, equalTo(1))
assertThat(response.licenceConditions[0].licenceConditionNotes, equalTo("Licence Condition notes"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ internal class ProxyIntegrationTest {
"CONVICTION_BY_ID_COURT_REPORTS": {
"convictionId": "?",
"activeOnly": true
},
"CONVICTION_BY_ID_LICENCE_CONDITIONS": {
"convictionId": "?",
"activeOnly": true
}
}
}
Expand All @@ -261,7 +265,7 @@ internal class ProxyIntegrationTest {
.withToken()
).andExpect(status().is2xxSuccessful).andReturn().response.contentAsJson<CompareAllReport>()

assertThat(res.totalNumberOfRequests, equalTo(12))
assertThat(res.totalNumberOfRequests, equalTo(13))
assertThat(res.totalNumberOfCrns, equalTo(2))
assertThat(res.currentPageNumber, equalTo(1))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.digital.hmpps.api.model

import java.time.LocalDate
import java.time.LocalDateTime

data class LicenceConditions(
val licenceConditions: List<LicenceCondition> = emptyList()
)

data class LicenceCondition(
val licenceConditionNotes: String? = null,
val startDate: LocalDate,
val commencementDate: LocalDate? = null,
val commencementNotes: String? = null,
val terminationDate: LocalDate? = null,
val terminationNotes: String? = null,
val createdDateTime: LocalDateTime,
val active: Boolean,
val licenceConditionTypeMainCat: KeyValue? = null,
val licenceConditionTypeSubCat: KeyValue? = null

)
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,3 @@ data class Requirement(
val length: Long?
)

data class LicenceCondition(
val description: String?,
val subTypeDescription: String?,
val startDate: LocalDate?,
val notes: String?,
val active: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ class CommunityApiController(
return proxy(request)
}

@GetMapping("/offenders/crn/{crn}/convictions/{convictionId}/licenceConditions")
fun convictionByIdCLicenceConditions(
request: HttpServletRequest,
@PathVariable crn: String,
@PathVariable convictionId: Long,
): Any {

sendComparisonReport(
mapOf(
"crn" to crn,
"convictionId" to convictionId
), Uri.CONVICTION_BY_ID_LICENCE_CONDITIONS, request
)

if (featureFlags.enabled("ccd-conviction-by-id-licence-conditions")) {
return convictionResource.getConvictionLicenceConditions(crn, convictionId)
}
return proxy(request)
}

@GetMapping("/**")
fun proxy(request: HttpServletRequest): ResponseEntity<String> {
val headers = request.headerNames.asSequence().associateWith { request.getHeader(it) }.toMutableMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,11 @@ enum class Uri(
"getConvictionCourtReports",
listOf("crn", "convictionId"),
),
CONVICTION_BY_ID_LICENCE_CONDITIONS(
"/secure/offenders/crn/{crn}/convictions/{convictionId}/licenceConditions",
"convictionResource",
"getConvictionLicenceConditions",
listOf("crn", "convictionId"),
),
DUMMY("/dummy", "dummyResource", "getDummy", listOf("crn")),
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ class ConvictionResource(
@PathVariable crn: String,
@PathVariable convictionId: Long
) = courtReportService.getCourtReportsFor(crn, convictionId)

@GetMapping("/{convictionId}/licenceConditions")
fun getConvictionLicenceConditions(
@PathVariable crn: String,
@PathVariable convictionId: Long
) = requirementService.getLicenceConditionsForConvictionId(crn, convictionId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import uk.gov.justice.digital.hmpps.api.model.KeyValue
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.AdRequirementMainCategory
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.LicenceCondition
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.RequirementMainCategory
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Custody
import java.time.LocalDate
Expand Down Expand Up @@ -148,6 +149,9 @@ class Disposal(

@OneToOne(mappedBy = "disposal")
val custody: Custody?,

@OneToMany(mappedBy = "disposal")
val licenceConditions: List<LicenceCondition> = emptyList(),
)

@Entity(name = "conviction_custody")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package uk.gov.justice.digital.hmpps.integrations.delius.event.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.Lob
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.JpaRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.Disposal
import java.time.LocalDate
import java.time.LocalDateTime

@Immutable
@Table(name = "lic_condition")
@Entity
@SQLRestriction("soft_deleted = 0 and active_flag = 1")
class LicenceCondition(
@ManyToOne
@JoinColumn(name = "disposal_id")
Expand All @@ -26,9 +19,26 @@ class LicenceCondition(
@Column
val startDate: LocalDate,

@Column
val commencementDate: LocalDate?,

@Lob
@Column
val commencementNotes: String?,

@Column
val terminationDate: LocalDate?,

@Lob
@Column
val terminationNotes: String?,

@Column(name = "created_datetime")
val createdDateTime: LocalDateTime,

@ManyToOne
@JoinColumn(name = "lic_cond_type_main_cat_id")
val mainCategory: LicenceConditionMainCategory,
val mainCategory: LicenceConditionMainCategory?,

@ManyToOne
@JoinColumn(name = "lic_cond_type_sub_cat_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ fun CourtAppearance.toCourtAppearance() = CourtAppearanceBasic(
appearanceDate = appearanceDate,
courtCode = court.code,
courtName = court.courtName,
appearanceType = KeyValue(court.courtType.code, court.courtType.description),
appearanceType = KeyValue(appearanceType.code, appearanceType.description),
crn = person.crn
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package uk.gov.justice.digital.hmpps.integrations.delius.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.KeyValue
import uk.gov.justice.digital.hmpps.api.model.LicenceConditions
import uk.gov.justice.digital.hmpps.api.model.conviction.ConvictionRequirements
import uk.gov.justice.digital.hmpps.api.model.conviction.PssRequirement
import uk.gov.justice.digital.hmpps.api.model.conviction.PssRequirements
Expand All @@ -9,6 +11,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.event.conviction.entity.
import uk.gov.justice.digital.hmpps.integrations.delius.event.conviction.entity.ConvictionRequirementRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.conviction.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.event.conviction.entity.getByEventId
import uk.gov.justice.digital.hmpps.integrations.delius.event.entity.LicenceCondition
import uk.gov.justice.digital.hmpps.integrations.delius.event.sentence.entity.PssRequirementRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getPerson
Expand Down Expand Up @@ -36,6 +39,16 @@ class RequirementService(
)
}

fun getLicenceConditionsForConvictionId(crn: String, convictionId: Long): LicenceConditions {
val event = getEventForPersonByCrnAndEventId(crn, convictionId)
val licenceConditions = event.disposal?.let {
it.licenceConditions.map { lc ->
lc.toLicenceCondition()
}
}
return LicenceConditions(licenceConditions ?: emptyList())
}

fun getPssRequirementsByConvictionId(crn: String, convictionId: Long): PssRequirements {
val event = getEventForPersonByCrnAndEventId(crn, convictionId)

Expand Down Expand Up @@ -63,5 +76,15 @@ fun PssRequirementEntity.toPssRequirement(): PssRequirement =
active
)



fun LicenceCondition.toLicenceCondition() = uk.gov.justice.digital.hmpps.api.model.LicenceCondition(
active = active,
commencementDate = commencementDate,
commencementNotes = commencementNotes,
createdDateTime = createdDateTime,
licenceConditionNotes = notes,
licenceConditionTypeMainCat = mainCategory?.let { KeyValue(it.code, it.description) },
licenceConditionTypeSubCat = subCategory?.let { KeyValue(it.code, it.description) },
startDate = startDate,
terminationDate = terminationDate,
terminationNotes = terminationNotes
)

0 comments on commit fb2f2a7

Please sign in to comment.