Skip to content

Commit

Permalink
PI-265: Added pagination to team caseload endpoint (#3696)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 authored Apr 29, 2024
1 parent c140739 commit c52f1c4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package uk.gov.justice.digital.hmpps.api.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.data.domain.PageRequest
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*
import uk.gov.justice.digital.hmpps.service.UserService

@RestController
Expand All @@ -24,8 +22,13 @@ class UserController(private val userService: UserService) {
fun getUserTeams(@PathVariable username: String) = userService.getUserTeams(username)

@GetMapping("/team/{teamCode}")

@Operation(summary = "Gets the caseload for the team")
fun getTeamCaseload(@PathVariable teamCode: String) = userService.getTeamCaseload(teamCode)
fun getTeamCaseload(
@PathVariable teamCode: String,
@RequestParam(required = false, defaultValue = "0") page: Int,
@RequestParam(required = false, defaultValue = "10") size: Int
) = userService.getTeamCaseload(teamCode, PageRequest.of(page, size))

@GetMapping("/team/{teamCode}/staff")
@Operation(summary = "Gets the staff within the team")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package uk.gov.justice.digital.hmpps.api.model.user

data class TeamCaseload(
val totalPages: Int,
val totalElements: Int,
val provider: String?,
val team: Team,
val caseload: List<TeamCase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uk.gov.justice.digital.hmpps.integrations.delius.user.entity
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
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 @@ -238,7 +240,7 @@ interface CaseloadRepository : JpaRepository<Caseload, Long> {
where c.team.code = :teamCode
"""
)
fun findByTeamCode(teamCode: String): List<Caseload>
fun findByTeamCode(teamCode: String, pageable: Pageable): Page<Caseload>
}

@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.digital.hmpps.service

import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.api.model.Name
Expand Down Expand Up @@ -28,12 +29,15 @@ class UserService(
}

@Transactional
fun getTeamCaseload(teamCode: String): TeamCaseload {
fun getTeamCaseload(teamCode: String, pageable: Pageable): TeamCaseload {
val team = teamRepository.getTeam(teamCode)
val caseload = caseloadRepository.findByTeamCode(team.code)
val caseload = caseloadRepository.findByTeamCode(team.code, pageable)
caseload.content
return TeamCaseload(
totalElements = caseload.totalElements.toInt(),
totalPages = caseload.totalPages,
provider = team.provider.description,
caseload = caseload.map { it.toTeamCase() },
caseload = caseload.content.map { it.toTeamCase() },
team = Team(description = team.description, team.code)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.whenever
import org.springframework.data.domain.PageRequest
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.api.model.user.*
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.USER
Expand Down Expand Up @@ -54,12 +55,14 @@ internal class UserControllerTest {
fun `calls get team caseload function `() {
val teamCode = "teamCode"
val expectedResponse = TeamCaseload(
totalPages = 1,
totalElements = 1,
provider = USER.staff?.provider?.description,
caseload = listOf(CASELOAD_PERSON_1.toTeamCase()),
team = Team(description = "desc", code = "code")
)
whenever(userService.getTeamCaseload(teamCode)).thenReturn(expectedResponse)
val res = controller.getTeamCaseload(teamCode)
whenever(userService.getTeamCaseload(teamCode, PageRequest.of(0, 10))).thenReturn(expectedResponse)
val res = controller.getTeamCaseload(teamCode, 0, 10)
assertThat(res, equalTo(expectedResponse))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.whenever
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.DEFAULT_PROVIDER
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.DEFAULT_STAFF
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.DEFAULT_TEAM
Expand Down Expand Up @@ -87,8 +89,14 @@ internal class UserServiceTest {
fun `calls get team caseload function`() {
val teamCode = DEFAULT_TEAM.code
whenever(teamRepository.findByTeamCode(teamCode)).thenReturn(DEFAULT_TEAM)
whenever(caseloadRepository.findByTeamCode(teamCode)).thenReturn(listOf(CASELOAD_PERSON_1))
val res = service.getTeamCaseload(teamCode)
whenever(caseloadRepository.findByTeamCode(teamCode, Pageable.ofSize(1))).thenReturn(
PageImpl(
listOf(
CASELOAD_PERSON_1
)
)
)
val res = service.getTeamCaseload(teamCode, Pageable.ofSize(1))
assertThat(res.provider, equalTo(DEFAULT_PROVIDER.description))
assertThat(res.caseload[0].staff.code, equalTo(CASELOAD_PERSON_1.staff.code))
assertThat(res.team.code, equalTo(DEFAULT_TEAM.code))
Expand Down

0 comments on commit c52f1c4

Please sign in to comment.