Skip to content

Commit

Permalink
PI-1554 added get staff by username endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Oct 10, 2023
1 parent 1aedd80 commit e1d5e41
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.probationarea.ProbationA
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.Dataset
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceDataRepository
import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffRepository
import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffUser
import uk.gov.justice.digital.hmpps.integrations.delius.team.TeamRepository
import uk.gov.justice.digital.hmpps.user.AuditUserRepository
import java.time.LocalDate
Expand Down Expand Up @@ -81,7 +82,8 @@ class DataLoader(
private val registrationRepository: RegistrationRepository,
private val referralRepository: ReferralRepository,
private val probationCaseDataLoader: ProbationCaseDataLoader,
private val lduRepository: LduRepository
private val lduRepository: LduRepository,
private val staffUserRepository: StaffUserRepository
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
Expand Down Expand Up @@ -147,6 +149,9 @@ class DataLoader(
)
)

staffRepository.save(StaffGenerator.DEFAULT_STAFF)
staffUserRepository.save(StaffGenerator.DEFAULT_STAFF_USER)

val personManagerStaff = StaffGenerator.generate(code = "N54A001")
staffRepository.save(personManagerStaff)
val person = PersonGenerator.DEFAULT
Expand Down Expand Up @@ -193,3 +198,5 @@ interface DatasetRepository : JpaRepository<Dataset, Long>
interface ProbationAreaRepository : JpaRepository<ProbationArea, Long>
interface AddressRepository : JpaRepository<Address, Long>
interface RegisterTypeRepository : JpaRepository<RegisterType, Long>

interface StaffUserRepository : JpaRepository<StaffUser, Long>
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package uk.gov.justice.digital.hmpps.data.generator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator.STAFF_GRADE
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.ApprovedPremises
import uk.gov.justice.digital.hmpps.integrations.delius.staff.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.staff.StaffUser
import uk.gov.justice.digital.hmpps.integrations.delius.team.Team
import uk.gov.justice.digital.hmpps.set
import java.util.concurrent.atomic.AtomicLong

object StaffGenerator {
private val staffCodeGenerator = AtomicLong(1)
val DEFAULT_STAFF = generate()

val DEFAULT_STAFF_USER = generateStaffUser("john-smith", DEFAULT_STAFF)

fun generate(
name: String = "TEST",
Expand All @@ -24,4 +29,10 @@ object StaffGenerator {
teams = teams,
approvedPremises = approvedPremises
)

fun generateStaffUser(
username: String,
staff: Staff? = null,
id: Long = IdGenerator.getAndIncrement()
) = StaffUser(staff, username, id).apply { staff?.set("user", this) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.data.generator.ApprovedPremisesGenerator
import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator
import uk.gov.justice.digital.hmpps.security.withOAuth2Token

@AutoConfigureMockMvc
Expand Down Expand Up @@ -60,4 +61,15 @@ class StaffControllerIntegrationTest {
.andExpect(jsonPath("$.content[*].name.surname", equalTo(listOf("Key-worker"))))
.andExpect(jsonPath("$.content[*].keyWorker", equalTo(listOf(true))))
}

@Test
fun `Get staff by username`() {
val username = StaffGenerator.DEFAULT_STAFF.user!!.username
mockMvc.perform(get("/staff/$username").withOAuth2Token(wireMockServer))
.andExpect(status().isOk)
.andExpect(jsonPath("$.username", equalTo(username)))
.andExpect(jsonPath("$.name.surname", equalTo(StaffGenerator.DEFAULT_STAFF.surname)))
.andExpect(jsonPath("$.name.forename", equalTo(StaffGenerator.DEFAULT_STAFF.forename)))
.andExpect(jsonPath("$.code", equalTo(StaffGenerator.DEFAULT_STAFF.code)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ class StaffController(
@RequestParam(required = false, defaultValue = "false") keyWorker: Boolean,
@PageableDefault(value = 100) pageable: Pageable = Pageable.ofSize(100)
) = staffService.getStaffInApprovedPremises(code, keyWorker, pageable)

@PreAuthorize("hasRole('ROLE_APPROVED_PREMISES_STAFF')")
@Operation(
summary = "Get the staff name by username",
description = """Returns the Staff name associated with the given username.
"""
)
@GetMapping(value = ["/staff/{userName}"])
fun getStaffByUsername(
@PathVariable userName: String
) = staffService.getStaffByUsername(userName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import jakarta.persistence.JoinColumn
import jakarta.persistence.JoinTable
import jakarta.persistence.ManyToMany
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import org.hibernate.annotations.Immutable
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.entity.ApprovedPremises
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.ReferenceData
Expand Down Expand Up @@ -35,6 +37,9 @@ class Staff(
@Column
val surname: String,

@OneToOne(mappedBy = "staff")
val user: StaffUser? = null,

@ManyToMany
@JoinTable(
name = "staff_team",
Expand All @@ -51,3 +56,20 @@ class Staff(
)
val approvedPremises: List<ApprovedPremises>
)

@Entity
@Immutable
@Table(name = "user_")
class StaffUser(

@OneToOne
@JoinColumn(name = "staff_id")
val staff: Staff? = null,

@Column(name = "distinguished_name")
val username: String,

@Id
@Column(name = "user_id")
val id: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.integrations.delius.staff

import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand Down Expand Up @@ -33,6 +34,9 @@ interface StaffRepository : JpaRepository<Staff, Long> {
): Page<Staff>

fun findByCode(code: String): Staff?

@EntityGraph(attributePaths = ["user"])
fun findByUserUsername(username: String): Staff?
}

fun StaffRepository.getByCode(code: String): Staff =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ data class StaffGrade(
val code: String,
val description: String
)

data class StaffDetail(
val username: String,
val name: PersonName,
val code: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.Approve
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.model.PersonName
import uk.gov.justice.digital.hmpps.model.StaffDetail
import uk.gov.justice.digital.hmpps.model.StaffGrade
import uk.gov.justice.digital.hmpps.model.StaffResponse

Expand All @@ -18,7 +19,11 @@ class StaffService(
private val staffRepository: StaffRepository
) {
@Transactional
fun getStaffInApprovedPremises(approvedPremisesCode: String, keyWorkersOnly: Boolean, pageable: Pageable): Page<StaffResponse> {
fun getStaffInApprovedPremises(
approvedPremisesCode: String,
keyWorkersOnly: Boolean,
pageable: Pageable
): Page<StaffResponse> {
if (!approvedPremisesRepository.existsByCodeCode(approvedPremisesCode)) {
throw NotFoundException("Approved Premises", "code", approvedPremisesCode)
}
Expand All @@ -34,10 +39,23 @@ class StaffService(
}
}

fun getStaffByUsername(username: String) =
staffRepository.findByUserUsername(username)?.toStaffDetail() ?: throw NotFoundException(
"Staff",
"username",
username
)

fun Staff.toResponse(approvedPremisesCode: String) = StaffResponse(
code = code,
name = PersonName(forename, surname, middleName),
grade = grade?.let { grade -> StaffGrade(grade.code, grade.description) },
keyWorker = approvedPremises.map { ap -> ap.code.code }.contains(approvedPremisesCode)
)

fun Staff.toStaffDetail() = StaffDetail(
user!!.username,
PersonName(forename, surname, middleName),
code
)
}

0 comments on commit e1d5e41

Please sign in to comment.