Skip to content

Commit

Permalink
PI-1631 return allocation staff/team for crn (#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister authored Nov 7, 2023
1 parent faec6ed commit b16eee9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import org.springframework.test.web.servlet.MockMvc
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.api.model.Mappings
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator
import uk.gov.justice.digital.hmpps.data.generator.EventGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator
import uk.gov.justice.digital.hmpps.data.generator.TeamGenerator
import uk.gov.justice.digital.hmpps.security.withOAuth2Token

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

@Autowired lateinit var wireMockserver: WireMockServer
@Autowired
lateinit var wireMockserver: WireMockServer

@Test
fun `successful response`() {
Expand All @@ -45,4 +49,21 @@ class AllocationCompletedIntegrationTest {
.andExpect(jsonPath("$.staff.code").value(staff.code))
.andExpect(jsonPath("$.staff.email").doesNotExist())
}

@Test
fun `allocation manager successful response`() {
val person = PersonGenerator.DEFAULT
val team = TeamGenerator.DEFAULT
val staff = StaffGenerator.DEFAULT
mockMvc.perform(
get("/allocation-completed/manager").withOAuth2Token(wireMockserver)
.param("crn", person.crn)
)
.andExpect(status().is2xxSuccessful)
.andExpect(jsonPath("$.code").value(staff.code))
.andExpect(jsonPath("$.name.forename").value(staff.forename))
.andExpect(jsonPath("$.name.surname").value(staff.surname))
.andExpect(jsonPath("$.grade").value(staff.grade?.code?.let { Mappings.toAllocationsGradeCode[it] }))
.andExpect(jsonPath("$.teamCode").value(team.code))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.api.model
import uk.gov.justice.digital.hmpps.integrations.delius.person.Person
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonManager
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffRecord
import uk.gov.justice.digital.hmpps.integrations.delius.provider.Team
import java.time.LocalDate

data class Name(
Expand All @@ -25,6 +26,8 @@ data class Manager(val code: String, val name: Name, val teamCode: String, val g

data class InitialAppointment(val date: LocalDate)

data class Team(val code: String, val description: String)

data class ProbationStatus(
val status: ManagementStatus
) {
Expand Down Expand Up @@ -53,6 +56,7 @@ fun StaffRecord.grade() = grade?.code?.let { Mappings.toAllocationsGradeCode[it]
fun StaffRecord.toManager(teamCode: String) = Manager(code, name(), teamCode, grade())
fun StaffRecord.toStaffMember(email: String? = null) = StaffMember(code, name(), email, grade())
fun PersonManager.toManager() = staff.toManager(team.code)
fun Team.toTeam() = uk.gov.justice.digital.hmpps.api.model.Team(code, description)

object Mappings {
val toAllocationsGradeCode: Map<String, String> = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ class AllocationCompletedResource(private val service: AllocationCompletedServic
@RequestParam eventNumber: String,
@RequestParam staffCode: String
) = service.getDetails(crn, eventNumber, staffCode)

@PreAuthorize("hasRole('ROLE_ALLOCATION_CONTEXT')")
@Operation(
summary = "Team allocation code for Persons PO",
description = """
"""
)
@GetMapping("/manager")
fun getAllocatedManager(
@RequestParam crn: String
) = service.getAllocationManager(crn)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.integrations.delius.person

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import java.time.ZonedDateTime

interface PersonManagerRepository : JpaRepository<PersonManager, Long> {
Expand All @@ -16,3 +17,4 @@ interface PersonManagerRepository : JpaRepository<PersonManager, Long> {
)
fun findActiveManager(personId: Long, dateTime: ZonedDateTime = ZonedDateTime.now()): PersonManager?
}
fun PersonManagerRepository.getActiveManager(personId: Long) = findActiveManager(personId) ?: throw NotFoundException("PersonManager", "personId", personId)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.AllocationCompletedResponse
import uk.gov.justice.digital.hmpps.api.model.Event
import uk.gov.justice.digital.hmpps.api.model.InitialAppointment
import uk.gov.justice.digital.hmpps.api.model.Manager
import uk.gov.justice.digital.hmpps.api.model.name
import uk.gov.justice.digital.hmpps.api.model.toManager
import uk.gov.justice.digital.hmpps.api.model.toStaffMember
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.EventRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.getByPersonCrnAndNumber
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.getActiveManager
import uk.gov.justice.digital.hmpps.integrations.delius.person.getByCrnAndSoftDeletedFalse
import uk.gov.justice.digital.hmpps.integrations.delius.person.getCaseType
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffRepository
Expand All @@ -20,7 +24,8 @@ class AllocationCompletedService(
private val eventRepository: EventRepository,
private val staffRepository: StaffRepository,
private val ldapService: LdapService,
private val contactRepository: ContactRepository
private val contactRepository: ContactRepository,
private val personManagerRepository: PersonManagerRepository
) {
fun getDetails(
crn: String,
Expand All @@ -41,4 +46,12 @@ class AllocationCompletedService(
staff = staff?.toStaffMember(email)
)
}

fun getAllocationManager(
crn: String
): Manager {
val person = personRepository.getByCrnAndSoftDeletedFalse(crn)
val manager = personManagerRepository.getActiveManager(person.id)
return manager.toManager()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.EventRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffRepository
import java.time.LocalDate
Expand All @@ -34,6 +35,8 @@ class AllocationCompletedServiceTest {

@Mock lateinit var contactRepository: ContactRepository

@Mock lateinit var personManagerRepository: PersonManagerRepository

@InjectMocks lateinit var allocationCompletedService: AllocationCompletedService

@Test
Expand Down

0 comments on commit b16eee9

Please sign in to comment.