Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/terraform/templates/SERVICE_NAME-…
Browse files Browse the repository at this point in the history
…queue--github--ministryofjustice/cloud-platform-terraform-sqs--5.0.0-5.1.0
  • Loading branch information
marcus-bcl authored Nov 8, 2024
2 parents 8286b93 + 7dada9c commit e92029b
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class DataLoader(
staffRepository.save(StaffGenerator.CRU_WOMENS_ESTATE)
staffUserRepository.save(StaffGenerator.CRU_WOMENS_ESTATE_USER)

staffRepository.save(StaffGenerator.STAFF_WITHOUT_USERNAME)

val personManagerStaff = StaffGenerator.generate(code = "N54A001")
staffRepository.save(personManagerStaff)
val person = PersonGenerator.DEFAULT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object StaffGenerator {
val CRU_WOMENS_ESTATE = generate(
name = "CRU Womens Estate"
)
val STAFF_WITHOUT_USERNAME = generate()

val DEFAULT_STAFF_USER = generateStaffUser("john-smith", DEFAULT_STAFF)
val JIM_SNOW_USER = generateStaffUser("JIMSNOWLDAP", JIM_SNOW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,26 @@ class StaffControllerIntegrationTest {
assertThat(res.probationArea.description, equalTo(StaffGenerator.DEFAULT_STAFF.probationArea.description))
assertThat(res.active, equalTo(true))
}

@Test
fun `get staff by code`() {
val staffCode = StaffGenerator.DEFAULT_STAFF.code
val res = mockMvc.perform(get("/staff?code=${staffCode}").withToken())
.andExpect(status().isOk).andReturn().response.contentAsJson<StaffDetail>()
assertThat(res.username, equalTo(StaffGenerator.DEFAULT_STAFF.user!!.username))
assertThat(res.name.surname, equalTo(StaffGenerator.DEFAULT_STAFF.surname))
assertThat(res.name.forename, equalTo(StaffGenerator.DEFAULT_STAFF.forename))
assertThat(res.code, equalTo(StaffGenerator.DEFAULT_STAFF.code))
}

@Test
fun `get staff without username by code`() {
val staffCode = StaffGenerator.STAFF_WITHOUT_USERNAME.code
val res = mockMvc.perform(get("/staff?code=${staffCode}").withToken())
.andExpect(status().isOk).andReturn().response.contentAsJson<StaffDetail>()
assertThat(res.username, equalTo(null))
assertThat(res.name.surname, equalTo(StaffGenerator.STAFF_WITHOUT_USERNAME.surname))
assertThat(res.name.forename, equalTo(StaffGenerator.STAFF_WITHOUT_USERNAME.forename))
assertThat(res.code, equalTo(StaffGenerator.STAFF_WITHOUT_USERNAME.code))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.StaffService

@RestController
@PreAuthorize("hasRole('PROBATION_API__APPROVED_PREMISES__CASE_DETAIL')")
class StaffController(
private val staffService: StaffService
) {
@PreAuthorize("hasRole('PROBATION_API__APPROVED_PREMISES__CASE_DETAIL')")
@Operation(
summary = "List all members of staff that are keyworkers in the Approved Premises",
description = """An Approved Premises is defined in Delius as part of reference data.
Expand All @@ -32,14 +32,15 @@ class StaffController(
@PageableDefault(value = 100) pageable: Pageable = Pageable.ofSize(100)
) = staffService.getStaffInApprovedPremises(code, keyWorker, pageable)

@PreAuthorize("hasRole('PROBATION_API__APPROVED_PREMISES__CASE_DETAIL')")
@Operation(
summary = "Get the staff name by username",
description = """Returns the Staff name associated with the given username.
"""
)
@Operation(summary = "Get the staff details by username")
@GetMapping(value = ["/staff/{userName}"])
fun getStaffByUsername(
@PathVariable userName: String
) = staffService.getStaffByUsername(userName)

@Operation(summary = "Get the staff details by code")
@GetMapping(value = ["/staff"], params = ["code"])
fun getStaffByCode(
@RequestParam code: String
) = staffService.getStaffByCode(code)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package uk.gov.justice.digital.hmpps.integrations.delius.person.offence.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.JpaRepository
Expand All @@ -15,11 +9,13 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referra
import java.time.LocalDate

interface CaseOffence {
val id: Long
val code: String
val description: String
val date: LocalDate?
val main: Boolean
val eventNumber: String
val eventId: Long
}

@Immutable
Expand Down Expand Up @@ -91,11 +87,25 @@ class Offence(
interface MainOffenceRepository : JpaRepository<MainOffence, Long> {
@Query(
"""
select mo.offence.code as code, mo.offence.description as description, mo.date as date, true as main, mo.event.number as eventNumber
select
mo.offence.id as id,
mo.offence.code as code,
mo.offence.description as description,
mo.date as date,
true as main,
mo.event.number as eventNumber,
mo.event.id as eventId
from MainOffence mo
where mo.event.personId = :personId and mo.event.active = true
union all
select ao.offence.code, ao.offence.description, ao.date, false, ao.event.number
select
ao.offence.id,
ao.offence.code,
ao.offence.description,
ao.date,
false,
ao.event.number,
ao.event.id
from AdditionalOffence ao
where ao.event.personId = :personId and ao.event.active = true
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class StaffDetail(
val staffIdentifier: Long,
val teams: List<Team> = emptyList(),
val probationArea: ProbationArea,
val username: String,
val username: String?,
val name: PersonName,
val code: String,
val active: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.Approve
import uk.gov.justice.digital.hmpps.integrations.delius.staff.LdapUser
import uk.gov.justice.digital.hmpps.integrations.delius.staff.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffRepository
import uk.gov.justice.digital.hmpps.integrations.delius.staff.getByCode
import uk.gov.justice.digital.hmpps.ldap.findByUsername
import uk.gov.justice.digital.hmpps.model.*

Expand Down Expand Up @@ -42,11 +43,12 @@ class StaffService(

fun getStaffByUsername(username: String) =
staffRepository.findByUsername(username)?.toStaffDetail(ldapTemplate.findByUsername<LdapUser>(username))
?: throw NotFoundException(
"Staff",
"username",
username
)
?: throw NotFoundException("Staff", "username", username)

fun getStaffByCode(code: String) =
staffRepository.getByCode(code).let {
it.toStaffDetail(it.user?.username?.let { username -> ldapTemplate.findByUsername<LdapUser>(username) })
}

fun Staff.toResponse(approvedPremisesCode: String) = StaffResponse(
code = code,
Expand All @@ -68,7 +70,7 @@ class StaffService(
endDate = it.endDate
)
},
username = user!!.username,
username = user?.username,
name = PersonName(forename, surname, middleName),
code = code,
probationArea = ProbationArea(
Expand Down
2 changes: 1 addition & 1 deletion projects/feature-flags/container/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/flipt-io/flipt:v1.51.0
FROM ghcr.io/flipt-io/flipt:v1.51.1

# Run any pending migrations on startup
CMD ["sh", "-c", "./flipt migrate && ./flipt"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package uk.gov.justice.digital.hmpps

import org.junit.jupiter.api.Assertions.assertEquals
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.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import uk.gov.justice.digital.hmpps.api.model.sentence.History
import uk.gov.justice.digital.hmpps.api.model.sentence.ProbationHistory
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator
import uk.gov.justice.digital.hmpps.service.toSummary
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
import java.time.LocalDate
import uk.gov.justice.digital.hmpps.api.model.sentence.SentenceSummary

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ProbationHistoryIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Test
fun `no probation history`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonDetailsGenerator.PERSONAL_DETAILS.crn}/probation-history")
.withToken()
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<History>()

val expected = History(
PersonDetailsGenerator.PERSONAL_DETAILS.toSummary(),
listOf(),
ProbationHistory(0, null, 0, 0)
)

assertEquals(expected, response)
}

@Test
fun `get probation history`() {
val response = mockMvc
.perform(
MockMvcRequestBuilders.get("/sentence/${PersonGenerator.OVERVIEW.crn}/probation-history").withToken()
)
.andExpect(MockMvcResultMatchers.status().isOk)
.andReturn().response.contentAsJson<History>()

val expected = History(
PersonGenerator.OVERVIEW.toSummary(),
listOf(
SentenceSummary(1234567, "Pre-Sentence"),
SentenceSummary(7654321, "Default Sentence Type")
),
ProbationHistory(2, LocalDate.now().minusDays(7), 2, 2)
)

assertEquals(expected, response)
}
}
Loading

0 comments on commit e92029b

Please sign in to comment.