Skip to content

Commit

Permalink
PI-2622 Add ids to offences + add staff by code endpoint (#4403)
Browse files Browse the repository at this point in the history
* PI-2622 Add ids to offences + add staff by code endpoint

* Add test + make username nullable
  • Loading branch information
marcus-bcl authored Nov 7, 2024
1 parent 45ab369 commit d7731f3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 23 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

0 comments on commit d7731f3

Please sign in to comment.