From e5182d1631f4bfb55e53e8c7fa25f91af066ffd4 Mon Sep 17 00:00:00 2001 From: pmcphee77 <150798161+pmcphee77@users.noreply.github.com> Date: Mon, 29 Jan 2024 17:54:53 +0000 Subject: [PATCH] PI-1834: Add team addresses to responsible com endpoint (#3129) * PI-1834: Add team addresses to responsible com endpoint --- .../justice/digital/hmpps/data/DataLoader.kt | 17 ++++------ .../data/generator/OfficeLocationGenerator.kt | 12 +++++++ .../hmpps/data/generator/PersonGenerator.kt | 10 +++++- .../justice/digital/hmpps/IntegrationTest.kt | 30 +++++++++++++++--- .../digital/hmpps/api/model/Manager.kt | 1 + .../delius/manager/entity/PersonManager.kt | 17 ++++++---- .../delius/provider/entity/Provider.kt | 12 +------ .../digital/hmpps/service/ManagerService.kt | 12 +++---- .../hmpps/service/OfficeAddressService.kt | 31 ++++++++++--------- .../digital/hmpps/service/StaffService.kt | 2 -- .../digital/hmpps/service/StaffServiceTest.kt | 4 --- 11 files changed, 87 insertions(+), 61 deletions(-) diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 7f0c34e733..cfe2ef4bc0 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -7,15 +7,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.context.ApplicationListener import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional -import uk.gov.justice.digital.hmpps.data.generator.AddressGenerator -import uk.gov.justice.digital.hmpps.data.generator.CaseloadGenerator -import uk.gov.justice.digital.hmpps.data.generator.OfficeLocationGenerator -import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator -import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator -import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator -import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator -import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator -import uk.gov.justice.digital.hmpps.data.generator.UserGenerator +import uk.gov.justice.digital.hmpps.data.generator.* import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CvlMapping import uk.gov.justice.digital.hmpps.user.AuditUserRepository @@ -54,6 +46,8 @@ class DataLoader( entityManager.persist(StaffGenerator.DEFAULT_STAFF_USER) entityManager.flush() + createOfficeLocationsAndDistricts() + entityManager.persist(PersonGenerator.DEFAULT_PERSON) entityManager.persist(PersonGenerator.DEFAULT_CM) @@ -70,7 +64,7 @@ class DataLoader( ) createForAddingLicenceConditions() - createOfficeLocationsAndDistricts() + createCaseloadData() } @@ -97,7 +91,8 @@ class DataLoader( OfficeLocationGenerator.DISTRICT_MKY, OfficeLocationGenerator.DISTRICT_OXF, OfficeLocationGenerator.LOCATION_BRK_1, - OfficeLocationGenerator.LOCATION_BRK_2 + OfficeLocationGenerator.LOCATION_BRK_2, + OfficeLocationGenerator.LOCATION_ENDED ) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OfficeLocationGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OfficeLocationGenerator.kt index 001de9b342..642ceec1ac 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OfficeLocationGenerator.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/OfficeLocationGenerator.kt @@ -33,6 +33,18 @@ object OfficeLocationGenerator { postcode = "RG1 3EH", ldu = DISTRICT_BRK ) + val LOCATION_ENDED = generateLocation( + code = "TVP_RCC", + description = "Reading Office", + buildingNumber = "23", + buildingName = "The old hall", + streetName = "Another Place", + district = "District 2", + town = "Reading", + postcode = "RG1 3EH", + endDate = LocalDate.now().minusDays(1), + ldu = DISTRICT_BRK + ) fun generateDistrict( code: String, diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt index d0b337aa66..542a68fe32 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt @@ -1,7 +1,10 @@ package uk.gov.justice.digital.hmpps.data.generator +import uk.gov.justice.digital.hmpps.data.generator.OfficeLocationGenerator.LOCATION_BRK_2 +import uk.gov.justice.digital.hmpps.data.generator.OfficeLocationGenerator.LOCATION_ENDED import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManager import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.OfficeLocation import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team @@ -27,8 +30,13 @@ object PersonGenerator { provider: Provider = ProviderGenerator.DEFAULT_PROVIDER, team: Team = ProviderGenerator.DEFAULT_TEAM, staff: Staff = StaffGenerator.DEFAULT, + teamAddresses: List = listOf( + OfficeLocationGenerator.LOCATION_BRK_1, + LOCATION_BRK_2, + LOCATION_ENDED + ), softDeleted: Boolean = false, active: Boolean = true, id: Long = IdGenerator.getAndIncrement() - ) = PersonManager(person, provider, team, staff, softDeleted, active, id) + ) = PersonManager(person, provider, team, teamAddresses, staff, softDeleted, active, id) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index 8041b32fdd..eefc975719 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -12,13 +12,11 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import uk.gov.justice.digital.hmpps.api.model.* +import uk.gov.justice.digital.hmpps.data.generator.OfficeLocationGenerator import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator -import uk.gov.justice.digital.hmpps.service.asManager -import uk.gov.justice.digital.hmpps.service.asPDUHead -import uk.gov.justice.digital.hmpps.service.asStaff -import uk.gov.justice.digital.hmpps.service.asStaffName +import uk.gov.justice.digital.hmpps.service.* import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withJson import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken @@ -39,14 +37,36 @@ internal class IntegrationTest { .andExpect(status().isOk) .andReturn().response.contentAsJson() + val expectedAddresses: List = listOf( + OfficeLocationGenerator.LOCATION_BRK_1.asAddress(), + OfficeLocationGenerator.LOCATION_BRK_2.asAddress() + ) + assertThat( manager, equalTo( - PersonGenerator.DEFAULT_CM.asManager().copy(username = "john-smith", email = "john.smith@moj.gov.uk") + PersonGenerator.DEFAULT_CM.asManager().copy( + username = "john-smith", email = "john.smith@moj.gov.uk", + team = ProviderGenerator.DEFAULT_TEAM.asTeam(teamAddresses = expectedAddresses) + ) ) ) } + @Test + fun `returns only active team office locations`() { + val crn = PersonGenerator.DEFAULT_PERSON.crn + + val manager = mockMvc + .perform(get("/probation-case/$crn/responsible-community-manager").withToken()) + .andExpect(status().isOk) + .andReturn().response.contentAsJson() + assertThat( + manager.team.addresses?.size, + equalTo(2) + ) + } + @Test fun `returns 404 if no crn or community officer`() { mockMvc.perform( diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt index 971cc34a3e..8fd0e4cfc9 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Manager.kt @@ -18,6 +18,7 @@ data class Team( val code: String, val description: String, val telephone: String?, val emailAddress: String?, + val addresses: List?, val district: District, val borough: Borough, val startDate: LocalDate, diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt index ed26bcc364..afe1262cc2 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/manager/entity/PersonManager.kt @@ -1,17 +1,13 @@ package uk.gov.justice.digital.hmpps.integrations.delius.manager.entity -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.Id -import jakarta.persistence.JoinColumn -import jakarta.persistence.ManyToOne -import jakarta.persistence.Table +import jakarta.persistence.* import org.hibernate.annotations.Immutable import org.hibernate.annotations.SQLRestriction import org.springframework.data.jpa.repository.EntityGraph import org.springframework.data.jpa.repository.JpaRepository import uk.gov.justice.digital.hmpps.exception.NotFoundException import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.OfficeLocation import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team @@ -34,6 +30,15 @@ class PersonManager( @JoinColumn(name = "team_id") val team: Team, + @ManyToMany + @JoinTable( + name = "team_office_location", + joinColumns = [JoinColumn(name = "team_id")], + inverseJoinColumns = [JoinColumn(name = "office_location_id")] + ) + @SQLRestriction("end_date is null or end_date > current_date") + val teamOfficeLocations: List, + @ManyToOne @JoinColumn(name = "allocation_staff_id") val staff: Staff, diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt index 567fc19e45..b5e753161b 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/provider/entity/Provider.kt @@ -1,14 +1,6 @@ package uk.gov.justice.digital.hmpps.integrations.delius.provider.entity -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.Id -import jakarta.persistence.JoinColumn -import jakarta.persistence.JoinTable -import jakarta.persistence.ManyToMany -import jakarta.persistence.ManyToOne -import jakarta.persistence.OneToOne -import jakarta.persistence.Table +import jakarta.persistence.* import org.hibernate.annotations.Immutable import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable @@ -57,8 +49,6 @@ class Team( val id: Long ) -interface TeamRepository : JpaRepository - @Immutable @Entity @Table(name = "district") diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt index 870cd9eab9..35304773ab 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ManagerService.kt @@ -5,14 +5,11 @@ import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.api.model.ManagedOffender 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.OfficeAddress import uk.gov.justice.digital.hmpps.exception.NotFoundException import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManager import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManagerRepository -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Borough -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.District -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Provider -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Team +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.* import uk.gov.justice.digital.hmpps.ldap.findEmailByUsername @Service @@ -33,7 +30,7 @@ fun PersonManager.asManager() = Manager( staff.code, staff.name(), provider.asProvider(), - team.asTeam(), + team.asTeam(teamOfficeLocations.map(OfficeLocation::asAddress)), staff.user?.username, staff.user?.email, staff.isUnallocated() @@ -41,11 +38,12 @@ fun PersonManager.asManager() = Manager( fun Staff.name() = Name(forename, middleName, surname) fun Provider.asProvider() = uk.gov.justice.digital.hmpps.api.model.Provider(code, description) -fun Team.asTeam() = uk.gov.justice.digital.hmpps.api.model.Team( +fun Team.asTeam(teamAddresses: List? = null) = uk.gov.justice.digital.hmpps.api.model.Team( code, description, telephone, emailAddress, + teamAddresses, district.asDistrict(), district.borough.asBorough(), startDate, diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficeAddressService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficeAddressService.kt index f8c4c89646..b003af1e04 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficeAddressService.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficeAddressService.kt @@ -4,25 +4,28 @@ import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.api.model.OfficeAddress +import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.OfficeLocation import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.OfficeLocationRepository @Service class OfficeAddressService(private val officeLocationRepository: OfficeLocationRepository) { fun findAddresses(ldu: String, officeName: String, pageable: Pageable): Page = officeLocationRepository.findByLduAndOfficeName(ldu, officeName, pageable).map { - OfficeAddress( - it.description, - it.buildingName, - it.buildingNumber, - it.streetName, - it.district, - it.townCity, - it.county, - it.postcode, - it.ldu.description, - it.telephoneNumber, - it.startDate, - it.endDate - ) + it.asAddress() } } + +fun OfficeLocation.asAddress() = OfficeAddress( + description, + buildingName, + buildingNumber, + streetName, + district, + townCity, + county, + postcode, + ldu.description, + telephoneNumber, + startDate, + endDate +) diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt index 6fe0d6f205..221bd4c262 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/StaffService.kt @@ -11,7 +11,6 @@ import uk.gov.justice.digital.hmpps.integrations.delius.caseload.entity.Caseload import uk.gov.justice.digital.hmpps.integrations.delius.caseload.entity.CaseloadRepository import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.BoroughRepository import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.StaffRepository -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.TeamRepository import uk.gov.justice.digital.hmpps.ldap.findEmailByUsername @Service @@ -20,7 +19,6 @@ class StaffService( private val staffRepository: StaffRepository, private val boroughRepository: BoroughRepository, private val caseloadRepository: CaseloadRepository, - private val teamRepository: TeamRepository ) { fun findStaff(username: String): Staff = staffRepository.findByUserUsername(username)?.let { staff -> diff --git a/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/StaffServiceTest.kt b/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/StaffServiceTest.kt index 4e45d287a2..43b3bdc885 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/StaffServiceTest.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/StaffServiceTest.kt @@ -13,7 +13,6 @@ import uk.gov.justice.digital.hmpps.data.generator.CaseloadGenerator import uk.gov.justice.digital.hmpps.integrations.delius.caseload.entity.CaseloadRepository import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.BoroughRepository import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.StaffRepository -import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.TeamRepository @ExtendWith(MockitoExtension::class) internal class StaffServiceTest { @@ -30,9 +29,6 @@ internal class StaffServiceTest { @Mock lateinit var caseloadRepository: CaseloadRepository - @Mock - lateinit var teamRepository: TeamRepository - @InjectMocks lateinit var service: StaffService