Skip to content

Commit

Permalink
PI-1758 Handle duplicate office location codes (#2958)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Jan 2, 2024
1 parent f6dd623 commit a4301a8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class DataLoader(

mainCatRepository.save(SentenceGenerator.MAIN_CAT_F)

providerRepository.save(ProviderGenerator.NON_CRS_PROVIDER)
providerRepository.save(ProviderGenerator.INACTIVE_PROVIDER)
val provider = providerRepository.saveAndFlush(ProviderGenerator.INTENDED_PROVIDER)
pduRepository.saveAll(
listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import java.time.LocalDate

object ProviderGenerator {
val INTENDED_PROVIDER = generateProvider(Provider.INTENDED_PROVIDER_CODE, "Test Provider")
val NON_CRS_PROVIDER = generateProvider("N01", "Non-CRS Provider")
val INACTIVE_PROVIDER = generateProvider("N02", "Inactive Provider", endDate = LocalDate.of(2020, 1, 1))
val PROBATION_BOROUGH = generateBorough("PDU01")
val PRISON_BOROUGH = generateBorough("PDU02")
val PROBATION_DISTRICT = generateDistrict("LDU01", borough = PROBATION_BOROUGH)
Expand All @@ -28,11 +30,17 @@ object ProviderGenerator {
val LOCATIONS = listOf(
generateLocation("TESTONE", buildingName = "Test One", streetName = "Mantle Place", postcode = "MP1 1PM"),
generateLocation("TESTTWO", buildingName = "Test Two", postcode = "MP2 2PM", telephoneNumber = "020 123 6789"),
generateLocation("NOTCRS", providerId = 999L)
generateLocation("NOTCRS", provider = NON_CRS_PROVIDER),
generateLocation("DEFAULT", provider = INACTIVE_PROVIDER), // duplicate code linked to inactive provider
generateLocation("ENDDATE", endDate = LocalDate.of(2020, 1, 1))
)

fun generateProvider(code: String, description: String, id: Long = IdGenerator.getAndIncrement()) =
Provider(code, description, id)
fun generateProvider(
code: String,
description: String,
endDate: LocalDate? = null,
id: Long = IdGenerator.getAndIncrement()
) = Provider(code, description, endDate, id)

fun generateBorough(
code: String,
Expand Down Expand Up @@ -83,7 +91,7 @@ object ProviderGenerator {
telephoneNumber: String? = null,
startDate: LocalDate = LocalDate.now().minusDays(7),
endDate: LocalDate? = null,
providerId: Long = INTENDED_PROVIDER.id,
provider: Provider = INTENDED_PROVIDER,
id: Long = IdGenerator.getAndIncrement()
) = Location(
code,
Expand All @@ -98,7 +106,7 @@ object ProviderGenerator {
telephoneNumber,
startDate,
endDate,
providerId,
provider,
id
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Provider(

val description: String,

val endDate: LocalDate?,

@Id
@Column(name = "probation_area_id")
val id: Long
Expand Down Expand Up @@ -73,8 +75,9 @@ class Location(
val startDate: LocalDate,
val endDate: LocalDate?,

@Column(name = "probation_area_id")
val providerId: Long,
@ManyToOne
@JoinColumn(name = "probation_area_id")
val provider: Provider,

@Id
@Column(name = "office_location_id")
Expand Down Expand Up @@ -130,17 +133,28 @@ interface TeamRepository : JpaRepository<Team, Long> {
fun TeamRepository.getByCode(code: String) = findByCode(code) ?: throw NotFoundException("Team", "code", code)

interface LocationRepository : JpaRepository<Location, Long> {
fun findByCodeAndEndDateIsNull(code: String): Location?
@Query(
"""
select l from Location l
join fetch l.provider p
where l.code = :code
and (p.endDate is null or p.endDate > current_date)
and (l.endDate is null or l.endDate > current_date)
"""
)
fun findActiveLocationByCode(code: String): Location?

@Query(
"""
select l from Location l
where l.providerId = :providerId
and l.endDate is null
join fetch l.provider p
where p.id = :providerId
and (p.endDate is null or p.endDate > current_date)
and (l.endDate is null or l.endDate > current_date)
"""
)
fun findAllLocationsForProvider(providerId: Long): List<Location>
}

fun LocationRepository.getByCode(code: String) =
findByCodeAndEndDateIsNull(code) ?: throw NotFoundException("Location", "code", code)
findActiveLocationByCode(code) ?: throw NotFoundException("Location", "code", code)

0 comments on commit a4301a8

Please sign in to comment.