generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PI-2784 Add teams endpoint for workforce integration
- Loading branch information
1 parent
4544eb0
commit cd42e33
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/TeamsIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...cations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/TeamsResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
) |
17 changes: 17 additions & 0 deletions
17
...tions-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/TeamResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
20 changes: 20 additions & 0 deletions
20
...allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/TeamService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) } | ||
} | ||
) | ||
} |