Skip to content

Commit

Permalink
PI-1834: Add team addresses to responsible com endpoint (#3129)
Browse files Browse the repository at this point in the history
* PI-1834: Add team addresses to responsible com endpoint
  • Loading branch information
pmcphee77 authored Jan 29, 2024
1 parent bb4c0d9 commit e5182d1
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -70,7 +64,7 @@ class DataLoader(
)

createForAddingLicenceConditions()
createOfficeLocationsAndDistricts()

createCaseloadData()
}

Expand All @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,8 +30,13 @@ object PersonGenerator {
provider: Provider = ProviderGenerator.DEFAULT_PROVIDER,
team: Team = ProviderGenerator.DEFAULT_TEAM,
staff: Staff = StaffGenerator.DEFAULT,
teamAddresses: List<OfficeLocation> = 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,14 +37,36 @@ internal class IntegrationTest {
.andExpect(status().isOk)
.andReturn().response.contentAsJson<Manager>()

val expectedAddresses: List<OfficeAddress> = listOf(
OfficeLocationGenerator.LOCATION_BRK_1.asAddress(),
OfficeLocationGenerator.LOCATION_BRK_2.asAddress()
)

assertThat(
manager,
equalTo(
PersonGenerator.DEFAULT_CM.asManager().copy(username = "john-smith", email = "[email protected]")
PersonGenerator.DEFAULT_CM.asManager().copy(
username = "john-smith", email = "[email protected]",
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<Manager>()
assertThat(
manager.team.addresses?.size,
equalTo(2)
)
}

@Test
fun `returns 404 if no crn or community officer`() {
mockMvc.perform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ data class Team(
val code: String, val description: String,
val telephone: String?,
val emailAddress: String?,
val addresses: List<OfficeAddress>?,
val district: District,
val borough: Borough,
val startDate: LocalDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<OfficeLocation>,

@ManyToOne
@JoinColumn(name = "allocation_staff_id")
val staff: Staff,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -57,8 +49,6 @@ class Team(
val id: Long
)

interface TeamRepository : JpaRepository<Team, Long>

@Immutable
@Entity
@Table(name = "district")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,19 +30,20 @@ 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()
)

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<OfficeAddress>? = null) = uk.gov.justice.digital.hmpps.api.model.Team(
code,
description,
telephone,
emailAddress,
teamAddresses,
district.asDistrict(),
district.borough.asBorough(),
startDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OfficeAddress> =
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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,9 +29,6 @@ internal class StaffServiceTest {
@Mock
lateinit var caseloadRepository: CaseloadRepository

@Mock
lateinit var teamRepository: TeamRepository

@InjectMocks
lateinit var service: StaffService

Expand Down

0 comments on commit e5182d1

Please sign in to comment.