Skip to content

Commit

Permalink
PI-2784 Add teams endpoint for workforce integration
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Feb 5, 2025
1 parent 4544eb0 commit cd42e33
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.gov.justice.digital.hmpps

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.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
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.data.generator.TeamGenerator
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken

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

@Test
fun `successful response`() {
val team1 = TeamGenerator.DEFAULT.code
val team2 = TeamGenerator.ALLOCATION_TEAM.code
mockMvc.perform(get("/teams").withToken().param("teamCode", team1).param("teamCode", team2))
.andExpect(status().is2xxSuccessful)
.andExpect(jsonPath("$.teams.keys()").value(setOf(team1, team2)))
.andExpect(jsonPath("$.teams.$team1.size()").value(1))
.andExpect(jsonPath("$.teams.$team2.size()").value(2))
.andExpect(jsonPath("$.teams.$team2[0].name.forename").value("Joe"))
.andExpect(jsonPath("$.teams.$team2[0].name.surname").value("Bloggs"))
.andExpect(jsonPath("$.teams.$team2[0].email").value("example@example.com"))
}

@Test
fun `team codes can be empty`() {
mockMvc.perform(get("/teams").withToken())
.andExpect(status().is2xxSuccessful)
.andExpect(jsonPath("$.teams.keys()").isEmpty)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.gov.justice.digital.hmpps.api.model

data class TeamsResponse(
val teams: Map<String, List<StaffMember>>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.gov.justice.digital.hmpps.api.resource

import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.service.TeamService

@RestController
@RequestMapping("/teams")
@PreAuthorize("hasRole('PROBATION_API__WORKFORCE_ALLOCATIONS__CASE_DETAIL')")
class TeamResource(private val teamService: TeamService) {
@GetMapping
fun activeCases(@RequestParam("teamCode", defaultValue = "") teamCodes: List<String> = listOf()) =
teamService.getTeams(teamCodes)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package uk.gov.justice.digital.hmpps.service

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.TeamsResponse
import uk.gov.justice.digital.hmpps.api.model.toStaffMember
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffRepository

@Service
class TeamService(
private val staffRepository: StaffRepository,
private val ldapService: LdapService,
) {
fun getTeams(teamCodes: List<String>) = TeamsResponse(
teamCodes.associateWith { teamCode ->
val staff = staffRepository.findActiveStaffInTeam(teamCode)
val emails = ldapService.findEmailsForStaffIn(staff)
staff.map { it.toStaffMember(emails[it.user?.username]) }
}
)
}

0 comments on commit cd42e33

Please sign in to comment.