From 6a18b23147e7297ffe0df7ef45f73f0e2ab9ec0e Mon Sep 17 00:00:00 2001 From: pmcphee77 <150798161+pmcphee77@users.noreply.github.com> Date: Tue, 23 Apr 2024 16:19:03 +0100 Subject: [PATCH] PI-2065: Updated caseload api (#3669) * PI-2065: Updated caseload api --- .../hmpps/data/generator/ContactGenerator.kt | 4 +- .../digital/hmpps/UserIntegrationTest.kt | 85 +++++++++++++----- .../hmpps/api/controller/UserController.kt | 20 ++++- .../digital/hmpps/api/model/user/Staff.kt | 8 ++ .../digital/hmpps/api/model/user/StaffCase.kt | 1 - .../hmpps/api/model/user/StaffCaseload.kt | 9 ++ .../digital/hmpps/api/model/user/Team.kt | 2 +- .../digital/hmpps/api/model/user/TeamStaff.kt | 6 ++ .../api/model/user/{User.kt => UserTeam.kt} | 3 +- .../integrations/delius/user/entity/User.kt | 90 +++++++++++++++++-- .../digital/hmpps/service/UserService.kt | 59 ++++++++---- .../api/controller/UserControllerTest.kt | 54 +++++++++-- .../digital/hmpps/service/UserServiceTest.kt | 73 ++++++++++++++- 13 files changed, 351 insertions(+), 63 deletions(-) create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Staff.kt create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCaseload.kt create mode 100644 projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/TeamStaff.kt rename projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/{User.kt => UserTeam.kt} (69%) diff --git a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactGenerator.kt b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactGenerator.kt index 5c359547a3..8bf5b72db3 100644 --- a/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactGenerator.kt +++ b/projects/manage-supervision-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactGenerator.kt @@ -31,7 +31,7 @@ object ContactGenerator { val DEFAULT_STAFF = generateStaff("N01BDT1", "John", "Smith", emptyList()) val STAFF_1 = generateStaff("N01BDT2", "Jim", "Brown", emptyList()) - val DEFAULT_TEAM = generateTeam(code = "TEAM1", description = "Main Team", staff = listOf(DEFAULT_STAFF, STAFF_1)) + val DEFAULT_TEAM = generateTeam(code = "TEAM11", description = "Main Team", staff = listOf(DEFAULT_STAFF, STAFF_1)) val USER = User( id = IdGenerator.getAndIncrement(), @@ -251,7 +251,7 @@ fun generateTeam( description: String = "Description of $code", id: Long = IdGenerator.getAndIncrement(), staff: List, -) = Team(id = id, code = code, description = description, staff = staff) +) = Team(id = id, code = code, description = description, staff = staff, provider = DEFAULT_PROVIDER) fun generateStaff( code: String, diff --git a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt index 1c833ffa6c..80351d8975 100644 --- a/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt +++ b/projects/manage-supervision-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/UserIntegrationTest.kt @@ -10,14 +10,19 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDO import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status -import uk.gov.justice.digital.hmpps.api.model.user.User +import uk.gov.justice.digital.hmpps.api.model.Name +import uk.gov.justice.digital.hmpps.api.model.user.StaffCaseload +import uk.gov.justice.digital.hmpps.api.model.user.TeamStaff +import uk.gov.justice.digital.hmpps.api.model.user.UserTeam +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 +import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.STAFF_1 import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.USER import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.USER_1 import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW import uk.gov.justice.digital.hmpps.data.generator.personalDetails.PersonDetailsGenerator.PERSONAL_DETAILS import uk.gov.justice.digital.hmpps.service.name - import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken @@ -32,17 +37,13 @@ internal class UserIntegrationTest { val person = USER val res = mockMvc - .perform(get("/caseload/${person.username}").withToken()) + .perform(get("/caseload/user/${person.username}").withToken()) .andExpect(status().isOk) - .andReturn().response.contentAsJson() + .andReturn().response.contentAsJson() - assertThat(res.cases[0].crn, equalTo(OVERVIEW.crn)) - assertThat(res.cases[0].caseName, equalTo(OVERVIEW.name())) - assertThat(res.teams[0].description, equalTo(DEFAULT_TEAM.description)) - assertThat(res.teams[0].cases[0].crn, equalTo(OVERVIEW.crn)) - assertThat(res.teams[0].cases[0].caseName, equalTo(OVERVIEW.name())) - assertThat(res.teams[0].cases[1].crn, equalTo(PERSONAL_DETAILS.crn)) - assertThat(res.teams[0].cases[1].caseName, equalTo(PERSONAL_DETAILS.name())) + assertThat(res.provider, equalTo(DEFAULT_PROVIDER.description)) + assertThat(res.caseload[0].crn, equalTo(OVERVIEW.crn)) + assertThat(res.caseload[0].caseName, equalTo(OVERVIEW.name())) } @Test @@ -50,30 +51,70 @@ internal class UserIntegrationTest { val person = USER_1 val res = mockMvc - .perform(get("/caseload/${person.username}").withToken()) + .perform(get("/caseload/user/${person.username}").withToken()) .andExpect(status().isOk) - .andReturn().response.contentAsJson() + .andReturn().response.contentAsJson() - assertThat(res.cases[0].crn, equalTo(PERSONAL_DETAILS.crn)) - assertThat(res.cases[0].caseName, equalTo(PERSONAL_DETAILS.name())) - assertThat(res.teams[0].description, equalTo(DEFAULT_TEAM.description)) - assertThat(res.teams[0].cases[1].crn, equalTo(PERSONAL_DETAILS.crn)) - assertThat(res.teams[0].cases[1].caseName, equalTo(PERSONAL_DETAILS.name())) - assertThat(res.teams[0].cases[0].crn, equalTo(OVERVIEW.crn)) - assertThat(res.teams[0].cases[0].caseName, equalTo(OVERVIEW.name())) + assertThat(res.provider, equalTo(DEFAULT_PROVIDER.description)) + assertThat(res.caseload[0].crn, equalTo(PERSONAL_DETAILS.crn)) + assertThat(res.caseload[0].caseName, equalTo(PERSONAL_DETAILS.name())) } @Test fun `not found status returned`() { mockMvc - .perform(get("/caseload/invalidusername").withToken()) + .perform(get("/caseload/user/invalidusername").withToken()) .andExpect(status().isNotFound) } @Test fun `unauthorized status returned`() { mockMvc - .perform(get("/caseload/invalidusername")) + .perform(get("/caseload/user/invalidusername")) .andExpect(status().isUnauthorized) } + + @Test + fun `teams for a user`() { + + val person = USER_1 + val res = mockMvc + .perform(get("/caseload/user/${person.username}/teams").withToken()) + .andExpect(status().isOk) + .andReturn().response.contentAsJson() + + assertThat(res.provider, equalTo(DEFAULT_PROVIDER.description)) + assertThat(res.teams[0].description, equalTo(DEFAULT_TEAM.description)) + assertThat(res.teams[0].code.trim(), equalTo(DEFAULT_TEAM.code.trim())) + } + + @Test + fun `staff for a team`() { + + val code = DEFAULT_TEAM.code.trim() + val res = mockMvc + .perform(get("/caseload/team/${code}/staff").withToken()) + .andExpect(status().isOk) + .andReturn().response.contentAsJson() + + assertThat(res.provider, equalTo(DEFAULT_PROVIDER.description)) + assertThat(res.staff[0].code.trim(), equalTo(DEFAULT_STAFF.code.trim())) + assertThat(res.staff[0].name, equalTo(Name(forename = DEFAULT_STAFF.forename, surname = DEFAULT_STAFF.surname))) + assertThat(res.staff[1].code.trim(), equalTo(STAFF_1.code.trim())) + assertThat(res.staff[1].name, equalTo(Name(forename = STAFF_1.forename, surname = STAFF_1.surname))) + } + + @Test + fun `case load for a staffcode`() { + + val person = USER_1 + val res = mockMvc + .perform(get("/caseload/staff/${person.staff?.code?.trim()}").withToken()) + .andExpect(status().isOk) + .andReturn().response.contentAsJson() + + assertThat(res.provider, equalTo(DEFAULT_PROVIDER.description)) + assertThat(res.caseload[0].crn, equalTo(PERSONAL_DETAILS.crn)) + assertThat(res.caseload[0].caseName, equalTo(PERSONAL_DETAILS.name())) + } } diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt index 4c9315e62d..bb45d35399 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserController.kt @@ -11,11 +11,23 @@ import uk.gov.justice.digital.hmpps.service.UserService @RestController @Tag(name = "Caseload Info") -@RequestMapping("/caseload/{username}") +@RequestMapping("/caseload") @PreAuthorize("hasRole('PROBATION_API__MANAGE_A_SUPERVISION__CASE_DETAIL')") class UserController(private val userService: UserService) { - @GetMapping - @Operation(summary = "Gets caseloads for a person and staff within their teams") - fun getCaseload(@PathVariable username: String) = userService.getUserDetails(username) + @GetMapping("/user/{username}") + @Operation(summary = "Gets caseloads for the user") + fun getUserCaseload(@PathVariable username: String) = userService.getUserCaseload(username) + + @GetMapping("/user/{username}/teams") + @Operation(summary = "Gets the users teams") + fun getUserTeams(@PathVariable username: String) = userService.getUserTeams(username) + + @GetMapping("/team/{teamCode}/staff") + @Operation(summary = "Gets the staff within the team") + fun getTeamStaff(@PathVariable teamCode: String) = userService.getTeamStaff(teamCode) + + @GetMapping("/staff/{staffCode}") + @Operation(summary = "Gets the caseload for a staff code ") + fun getStaffCaseload(@PathVariable staffCode: String) = userService.getStaffCaseload(staffCode) } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Staff.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Staff.kt new file mode 100644 index 0000000000..1a31d9910c --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Staff.kt @@ -0,0 +1,8 @@ +package uk.gov.justice.digital.hmpps.api.model.user + +import uk.gov.justice.digital.hmpps.api.model.Name + +data class Staff( + val name: Name, + val code: String +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt index fd0f633e15..a5faa77ecd 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCase.kt @@ -3,7 +3,6 @@ package uk.gov.justice.digital.hmpps.api.model.user import uk.gov.justice.digital.hmpps.api.model.Name data class StaffCase( - val staff: Name, val caseName: Name, val crn: String ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCaseload.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCaseload.kt new file mode 100644 index 0000000000..600738044f --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/StaffCaseload.kt @@ -0,0 +1,9 @@ +package uk.gov.justice.digital.hmpps.api.model.user + +import uk.gov.justice.digital.hmpps.api.model.Name + +data class StaffCaseload( + val provider: String?, + val staff: Name, + val caseload: List +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Team.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Team.kt index 6d097a5f98..9444861004 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Team.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/Team.kt @@ -2,5 +2,5 @@ package uk.gov.justice.digital.hmpps.api.model.user data class Team( val description: String, - val cases: List + val code: String ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/TeamStaff.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/TeamStaff.kt new file mode 100644 index 0000000000..f96f517c7e --- /dev/null +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/TeamStaff.kt @@ -0,0 +1,6 @@ +package uk.gov.justice.digital.hmpps.api.model.user + +data class TeamStaff( + val provider: String?, + val staff: List, +) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/User.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/UserTeam.kt similarity index 69% rename from projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/User.kt rename to projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/UserTeam.kt index a90ba4ca4d..837ddddd73 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/User.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/user/UserTeam.kt @@ -1,7 +1,6 @@ package uk.gov.justice.digital.hmpps.api.model.user -data class User( +data class UserTeam( val provider: String?, val teams: List, - val cases: List ) diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt index 4d0b1d3b25..664586638f 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/user/entity/User.kt @@ -4,6 +4,7 @@ import jakarta.persistence.* import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction 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.LocalDate @@ -29,6 +30,23 @@ class User( val surname: String, ) +interface UserRepository : JpaRepository { + + @Query( + """ + select u + from User u + join fetch u.staff s + join fetch s.provider p + where upper(u.username) = upper(:username) + """ + ) + fun findByUsername(username: String): User? +} + +fun UserRepository.getUser(username: String) = + findByUsername(username) ?: throw NotFoundException("User", "username", username) + @Immutable @Entity @Table(name = "staff") @@ -56,7 +74,7 @@ class Staff( joinColumns = [JoinColumn(name = "staff_id")], inverseJoinColumns = [JoinColumn(name = "team_id")] ) - val teams: List = emptyList(), + val teams: List, @Id @Column(name = "staff_id") @@ -67,6 +85,29 @@ class Staff( } } +interface StaffRepository : JpaRepository { + @Query( + """ + select s.teams + from Staff s + where s.code = :staffCode + """ + ) + fun findTeamsByStaffCode(staffCode: String): List + + @Query( + """ + select s + from Staff s + where s.code = :staffCode + """ + ) + fun findByStaffCode(staffCode: String): Staff? +} + +fun StaffRepository.getStaff(staffCode: String) = + findByStaffCode(staffCode) ?: throw NotFoundException("Staff", "staffCode", staffCode) + @Entity @Immutable @Table(name = "team") @@ -87,9 +128,36 @@ class Team( joinColumns = [JoinColumn(name = "team_id")], inverseJoinColumns = [JoinColumn(name = "staff_id")] ) - val staff: List + val staff: List, + + @JoinColumn(name = "probation_area_id") + @ManyToOne + val provider: Provider, ) +interface TeamRepository : JpaRepository { + @Query( + """ + select t.staff + from Team t + where t.code = :teamCode + """ + ) + fun findStaffByTeamCode(teamCode: String): List + + @Query( + """ + select t.provider.description + from Team t + where t.code = :teamCode + """ + ) + fun findProviderByTeamCode(teamCode: String): String? +} + +fun TeamRepository.getProvider(teamCode: String) = + findProviderByTeamCode(teamCode) ?: throw NotFoundException("Team", "teamCode", teamCode) + @Immutable @Entity @Table(name = "probation_area") @@ -132,6 +200,18 @@ data class Caseload( val roleCode: String ) +interface CaseloadRepository : JpaRepository { + @Query( + """ + select c from Caseload c + join fetch c.person p + join fetch c.team t + where c.staff.code = :staffCode + """ + ) + fun findByStaffCode(staffCode: String): List +} + @Entity @Immutable @Table(name = "offender") @@ -156,9 +236,5 @@ class CaseloadPerson( val surname: String, ) -interface UserRepository : JpaRepository { - fun findUserByUsername(username: String): User? -} -fun UserRepository.getUser(username: String) = - findUserByUsername(username) ?: throw NotFoundException("User", "username", username) + diff --git a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt index 411ad067bf..4b472f3781 100644 --- a/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt +++ b/projects/manage-supervision-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/UserService.kt @@ -3,21 +3,55 @@ package uk.gov.justice.digital.hmpps.service import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import uk.gov.justice.digital.hmpps.api.model.Name -import uk.gov.justice.digital.hmpps.api.model.user.StaffCase +import uk.gov.justice.digital.hmpps.api.model.user.* +import uk.gov.justice.digital.hmpps.api.model.user.Staff import uk.gov.justice.digital.hmpps.api.model.user.Team -import uk.gov.justice.digital.hmpps.api.model.user.User -import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.Caseload -import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.UserRepository -import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.getUser +import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.* @Service class UserService( - private val userRepository: UserRepository + private val userRepository: UserRepository, + private val caseloadRepository: CaseloadRepository, + private val staffRepository: StaffRepository, + private val teamRepository: TeamRepository ) { @Transactional - fun getUserDetails(username: String): User { - return userRepository.getUser(username).toUser() + fun getUserCaseload(username: String): StaffCaseload { + val user = userRepository.getUser(username) + val caseload = caseloadRepository.findByStaffCode(user.staff!!.code) + return StaffCaseload( + provider = user.staff.provider.description, + caseload = caseload.map { it.toStaffCase() }, + staff = Name(forename = user.staff.forename, surname = user.staff.surname) + ) + } + + @Transactional + fun getStaffCaseload(staffCode: String): StaffCaseload { + val staff = staffRepository.getStaff(staffCode) + val caseload = caseloadRepository.findByStaffCode(staff.code) + return StaffCaseload( + provider = staff.provider.description, + caseload = caseload.map { it.toStaffCase() }, + staff = Name(forename = staff.forename, surname = staff.surname) + ) + } + + @Transactional + fun getUserTeams(username: String): UserTeam { + val user = userRepository.getUser(username) + val teams = staffRepository.findTeamsByStaffCode(user.staff!!.code) + .map { Team(description = it.description, code = it.code) } + return UserTeam(provider = user.staff.provider.description, teams = teams) + } + + @Transactional + fun getTeamStaff(teamCode: String): TeamStaff { + val provider = teamRepository.getProvider(teamCode) + val staff = teamRepository.findStaffByTeamCode(teamCode) + .map { Staff(name = Name(forename = it.forename, surname = it.surname), code = it.code) } + return TeamStaff(provider = provider, staff = staff) } } @@ -28,14 +62,5 @@ fun Caseload.toStaffCase() = StaffCase( surname = person.surname ), crn = person.crn, - staff = Name(forename = staff.forename, surname = staff.surname) ) -fun uk.gov.justice.digital.hmpps.integrations.delius.user.entity.Team.toTeam() = Team( - description = description, - cases = staff.flatMap { cl -> cl.caseLoad.map { it.toStaffCase() } }) - -fun uk.gov.justice.digital.hmpps.integrations.delius.user.entity.User.toUser() = User( - cases = staff?.caseLoad?.map { it.toStaffCase() } ?: emptyList(), - provider = staff?.provider?.description, - teams = staff?.teams?.map { it.toTeam() } ?: emptyList()) \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt index bea52f35e9..f0b29c673e 100644 --- a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/api/controller/UserControllerTest.kt @@ -8,9 +8,12 @@ import org.mockito.InjectMocks import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension import org.mockito.kotlin.whenever +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 +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.CASELOAD_PERSON_1 import uk.gov.justice.digital.hmpps.service.UserService -import uk.gov.justice.digital.hmpps.service.toUser +import uk.gov.justice.digital.hmpps.service.toStaffCase @ExtendWith(MockitoExtension::class) internal class UserControllerTest { @@ -22,11 +25,52 @@ internal class UserControllerTest { lateinit var controller: UserController @Test - fun `calls get get activity function `() { + fun `calls get user case load function `() { val username = "username" - val expectedResponse = USER.toUser() - whenever(userService.getUserDetails(username)).thenReturn(expectedResponse) - val res = controller.getCaseload(username) + val expectedResponse = StaffCaseload( + provider = USER.staff?.provider?.description, + caseload = listOf(CASELOAD_PERSON_1.toStaffCase()), + staff = Name(forename = USER.staff!!.forename, surname = USER.staff!!.surname) + ) + whenever(userService.getUserCaseload(username)).thenReturn(expectedResponse) + val res = controller.getUserCaseload(username) + assertThat(res, equalTo(expectedResponse)) + } + + @Test + fun `calls get user teams function `() { + val username = "username" + val expectedResponse = UserTeam( + provider = USER.staff?.provider?.description, + teams = listOf(Team(description = "desc", code = "code")) + ) + whenever(userService.getUserTeams(username)).thenReturn(expectedResponse) + val res = controller.getUserTeams(username) + assertThat(res, equalTo(expectedResponse)) + } + + @Test + fun `calls get staff in teams function `() { + val teamCode = "teamCode" + val expectedResponse = TeamStaff( + provider = USER.staff?.provider?.description, + staff = listOf(Staff(code = "code", name = Name(forename = "forename", surname = "surname"))) + ) + whenever(userService.getTeamStaff(teamCode)).thenReturn(expectedResponse) + val res = controller.getTeamStaff(teamCode) + assertThat(res, equalTo(expectedResponse)) + } + + @Test + fun `calls get caseload for staff code `() { + val staffCode = "staffCode" + val expectedResponse = StaffCaseload( + provider = USER.staff?.provider?.description, + caseload = listOf(CASELOAD_PERSON_1.toStaffCase()), + staff = Name(forename = USER.staff!!.forename, surname = USER.staff!!.surname) + ) + whenever(userService.getStaffCaseload(staffCode)).thenReturn(expectedResponse) + val res = controller.getStaffCaseload(staffCode) assertThat(res, equalTo(expectedResponse)) } } \ No newline at end of file diff --git a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/UserServiceTest.kt b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/UserServiceTest.kt index 9af427826f..87e3d76cd2 100644 --- a/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/UserServiceTest.kt +++ b/projects/manage-supervision-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/UserServiceTest.kt @@ -9,7 +9,15 @@ import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension import org.mockito.kotlin.whenever 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 +import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.STAFF_1 import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.USER +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.CASELOAD_PERSON_1 +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW +import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.CaseloadRepository +import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.StaffRepository +import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.TeamRepository import uk.gov.justice.digital.hmpps.integrations.delius.user.entity.UserRepository @ExtendWith(MockitoExtension::class) @@ -18,16 +26,77 @@ internal class UserServiceTest { @Mock lateinit var userRepository: UserRepository + @Mock + lateinit var caseloadRepository: CaseloadRepository + + @Mock + lateinit var staffRepository: StaffRepository + + @Mock + lateinit var teamRepository: TeamRepository + @InjectMocks lateinit var service: UserService @Test fun `calls get person activity function`() { val username = "username" - whenever(userRepository.findUserByUsername(username)).thenReturn(USER) - val res = service.getUserDetails(username) + whenever(userRepository.findByUsername(username)).thenReturn(USER) + whenever(caseloadRepository.findByStaffCode(USER.staff!!.code)).thenReturn(listOf(CASELOAD_PERSON_1)) + val res = service.getUserCaseload(username) assertThat( res.provider, equalTo(DEFAULT_PROVIDER.description) ) } + + @Test + fun `calls get user teams function`() { + val username = "username" + whenever(userRepository.findByUsername(username)).thenReturn(USER) + whenever(staffRepository.findTeamsByStaffCode(USER.staff!!.code)).thenReturn(listOf(DEFAULT_TEAM)) + val res = service.getUserTeams(username) + assertThat( + res.provider, equalTo(DEFAULT_PROVIDER.description) + ) + assertThat( + res.teams[0].description, equalTo(DEFAULT_TEAM.description) + ) + assertThat( + res.teams[0].code, equalTo(DEFAULT_TEAM.code) + ) + } + + @Test + fun `calls get team staff function`() { + val teamCode = DEFAULT_TEAM.code + whenever(teamRepository.findProviderByTeamCode(teamCode)).thenReturn(DEFAULT_PROVIDER.description) + whenever(teamRepository.findStaffByTeamCode(teamCode)).thenReturn(listOf(DEFAULT_STAFF, STAFF_1)) + val res = service.getTeamStaff(teamCode) + assertThat( + res.provider, equalTo(DEFAULT_PROVIDER.description) + ) + assertThat( + res.staff[0].code.trim(), equalTo(DEFAULT_STAFF.code.trim()) + ) + assertThat( + res.staff[1].code, equalTo(STAFF_1.code) + ) + } + + @Test + fun `calls get caseload for staff function`() { + val staffCode = DEFAULT_STAFF.code + whenever(staffRepository.findByStaffCode(staffCode)).thenReturn(DEFAULT_STAFF) + whenever(caseloadRepository.findByStaffCode(staffCode)).thenReturn(listOf(CASELOAD_PERSON_1)) + val res = service.getStaffCaseload(staffCode) + assertThat( + res.provider, equalTo(DEFAULT_PROVIDER.description) + ) + assertThat( + res.caseload[0].crn, equalTo(OVERVIEW.crn) + ) + assertThat( + res.caseload[0].caseName.forename, equalTo(OVERVIEW.forename) + ) + } } \ No newline at end of file