Skip to content

Commit

Permalink
Merge pull request #2775 from ministryofjustice/chore/add-full-addres…
Browse files Browse the repository at this point in the history
…s-to-cas1-premise-results

Add Full Address to CAS1 Premises API Types
  • Loading branch information
davidatkinsuk authored Jan 9, 2025
2 parents 8461768 + 1df3bd8 commit 14f7408
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.cas1.PremisesCas1Delegate
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1ApprovedPremisesGender
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesBasicSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesDaySummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingCharacteristic
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingDaySummarySortField
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SortDirection
Expand Down Expand Up @@ -37,14 +37,14 @@ class Cas1PremisesController(
private val cas1OutOfServiceBedSummaryTransformer: Cas1OutOfServiceBedSummaryTransformer,
) : PremisesCas1Delegate {

override fun getPremisesById(premisesId: UUID): ResponseEntity<Cas1PremisesSummary> {
override fun getPremisesById(premisesId: UUID): ResponseEntity<Cas1Premises> {
userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW)

return ResponseEntity
.ok()
.body(
cas1PremisesTransformer.toPremiseSummary(
extractEntityFromCasResult(cas1PremisesService.getPremisesSummary(premisesId)),
cas1PremisesTransformer.toPremises(
extractEntityFromCasResult(cas1PremisesService.getPremisesInfo(premisesId)),
),
)
}
Expand Down Expand Up @@ -77,7 +77,7 @@ class Cas1PremisesController(
): ResponseEntity<Cas1PremiseCapacity> {
userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW)

val premiseSummaryInfo = cas1PremisesService.getPremisesSummary(premisesId)
val premiseSummaryInfo = cas1PremisesService.getPremisesInfo(premisesId)
val premiseCapacity = cas1PremisesService.getPremiseCapacity(
premisesId = premisesId,
startDate = startDate,
Expand All @@ -103,7 +103,7 @@ class Cas1PremisesController(
userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW)

val premiseSummaryInfo = extractEntityFromCasResult(
cas1PremisesService.getPremisesSummary(premisesId),
cas1PremisesService.getPremisesInfo(premisesId),
)

return ResponseEntity.ok().body(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ interface ApprovedPremisesRepository : JpaRepository<ApprovedPremisesEntity, UUI
apArea.id,
apArea.name,
CAST(COUNT(b) as int),
p.supportsSpaceBookings
p.supportsSpaceBookings,
p.fullAddress,
p.addressLine1,
p.addressLine2,
p.town,
p.postcode
)
FROM
ApprovedPremisesEntity p
Expand All @@ -199,7 +204,7 @@ interface ApprovedPremisesRepository : JpaRepository<ApprovedPremisesEntity, UUI
WHERE
(:gender IS NULL OR p.gender = :gender)
AND(cast(:apAreaId as text) IS NULL OR apArea.id = :apAreaId)
GROUP BY p.id, p.name, p.apCode, apArea.id, apArea.name
GROUP BY p.id, p.name, p.apCode, apArea.id, apArea.name, p.fullAddress, p.addressLine1, p.addressLine2, p.town, p.postcode
""",
)
fun findForSummaries(gender: ApprovedPremisesGender?, apAreaId: UUID?): List<ApprovedPremisesBasicSummary>
Expand Down Expand Up @@ -291,6 +296,8 @@ class ApprovedPremisesEntity(
* Full Address, excluding postcode. When defined this should be used instead of [addressLine1], [addressLine2] and [town]
*
* Whilst currently nullable, once all site surveys have been imported this can be made non-null
*
* It's recommended that [resolveFullAddress] is used in the meantime when address is required
*/
var fullAddress: String?,
) : PremisesEntity(
Expand All @@ -311,7 +318,27 @@ class ApprovedPremisesEntity(
rooms,
characteristics,
status,
)
) {

fun resolveFullAddress() = resolveFullAddress(
fullAddress = fullAddress,
addressLine1 = addressLine1,
addressLine2 = addressLine2,
town = town,
)

companion object {
fun resolveFullAddress(
fullAddress: String?,
addressLine1: String,
addressLine2: String?,
town: String?,
) = fullAddress
?: listOf(addressLine1, addressLine2, town)
.filter { !it.isNullOrBlank() }
.joinToString(separator = ", ")
}
}

enum class ApprovedPremisesGender {
MAN,
Expand Down Expand Up @@ -398,6 +425,11 @@ data class ApprovedPremisesBasicSummary(
val apAreaName: String,
val bedCount: Int,
val supportsSpaceBookings: Boolean,
val fullAddress: String?,
val addressLine1: String,
val addressLine2: String?,
val town: String?,
val postcode: String,
)

interface BookingSummary {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Cas1PremisesService(
companion object {
private const val OVERBOOKING_RANGE_DURATION_WEEKS = 12L
}
fun getPremisesSummary(premisesId: UUID): CasResult<Cas1PremisesSummaryInfo> {
fun getPremisesInfo(premisesId: UUID): CasResult<Cas1PremisesInfo> {
val premise = premisesRepository.findByIdOrNull(premisesId)
?: return CasResult.NotFound("premises", premisesId.toString())

Expand All @@ -33,7 +33,7 @@ class Cas1PremisesService(
val overbookingSummary = premise.takeIf { it.supportsSpaceBookings }?.let { buildOverBookingSummary(it) } ?: emptyList()

return CasResult.Success(
Cas1PremisesSummaryInfo(
Cas1PremisesInfo(
entity = premise,
bedCount = bedCount,
availableBeds = bedCount - outOfServiceBedsCount,
Expand Down Expand Up @@ -83,7 +83,7 @@ class Cas1PremisesService(
)
}

data class Cas1PremisesSummaryInfo(
data class Cas1PremisesInfo(
val entity: ApprovedPremisesEntity,
val bedCount: Int,
val availableBeds: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class Cas1PremiseCapacitySummaryTransformer(
) {

fun toCas1PremiseCapacitySummary(
premiseSummaryInfo: Cas1PremisesService.Cas1PremisesSummaryInfo,
premiseSummaryInfo: Cas1PremisesService.Cas1PremisesInfo,
premiseCapacity: PremiseCapacitySummary,
) = Cas1PremiseCapacity(
premise = cas1PremisesTransformer.toPremiseSummary(premiseSummaryInfo),
premise = cas1PremisesTransformer.toPremises(premiseSummaryInfo),
startDate = premiseCapacity.range.fromInclusive,
endDate = premiseCapacity.range.toInclusive,
capacity = premiseCapacity.byDay.map { it.toApiType() },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesBasicSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.NamedId
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesBasicSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApAreaTransformer

@Service
class Cas1PremisesTransformer(
val apAreaTransformer: ApAreaTransformer,
) {
fun toPremiseSummary(premisesSummaryInfo: Cas1PremisesService.Cas1PremisesSummaryInfo): Cas1PremisesSummary {
fun toPremises(premisesSummaryInfo: Cas1PremisesService.Cas1PremisesInfo): Cas1Premises {
val entity = premisesSummaryInfo.entity
return Cas1PremisesSummary(
return Cas1Premises(
id = entity.id,
name = entity.name,
apCode = entity.apCode,
fullAddress = entity.resolveFullAddress(),
postcode = entity.postcode,
bedCount = premisesSummaryInfo.bedCount,
availableBeds = premisesSummaryInfo.availableBeds,
Expand All @@ -37,6 +39,13 @@ class Cas1PremisesTransformer(
apArea = NamedId(entity.apAreaId, entity.apAreaName),
bedCount = entity.bedCount,
supportsSpaceBookings = entity.supportsSpaceBookings,
fullAddress = ApprovedPremisesEntity.resolveFullAddress(
fullAddress = entity.fullAddress,
addressLine1 = entity.addressLine1,
addressLine2 = entity.addressLine2,
town = entity.town,
),
postcode = entity.postcode,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceChara
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceSearchParameters
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceSearchResults
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.NamedId
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas1.CandidatePremises
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.asApiType
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceSearchResult as ApiSpaceSearchResult
Expand Down Expand Up @@ -52,10 +53,10 @@ class Cas1SpaceSearchResultsTransformer {
},
)

fun CandidatePremises.resolveFullAddress(): String {
return fullAddress
?: listOf(addressLine1, addressLine2, town)
.filter { !it.isNullOrBlank() }
.joinToString(separator = ", ")
}
fun CandidatePremises.resolveFullAddress() = ApprovedPremisesEntity.resolveFullAddress(
fullAddress = fullAddress,
addressLine1 = addressLine1,
addressLine2 = addressLine2,
town = town,
)
}
2 changes: 1 addition & 1 deletion src/main/resources/static/cas1-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ paths:
content:
'application/json':
schema:
$ref: 'cas1-schemas.yml#/components/schemas/Cas1PremisesSummary'
$ref: 'cas1-schemas.yml#/components/schemas/Cas1Premises'
401:
$ref: '_shared.yml#/components/responses/401Response'
403:
Expand Down
15 changes: 13 additions & 2 deletions src/main/resources/static/cas1-schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ components:
example: 22
supportsSpaceBookings:
type: boolean
fullAddress:
description: Full address, excluding postcode
type: string
postcode:
type: string
required:
- id
- name
- apArea
- bedCount
- supportsSpaceBookings
- fullAddress
- postcode
Cas1PremisesSearchResultSummary:
type: object
properties:
Expand Down Expand Up @@ -77,7 +84,7 @@ components:
- apArea
- characteristics
- fullAddress
Cas1PremisesSummary:
Cas1Premises:
type: object
properties:
id:
Expand All @@ -89,6 +96,9 @@ components:
apCode:
type: string
example: NEHOPE1
fullAddress:
description: Full address, excluding postcode
type: string
postcode:
type: string
example: LS1 3AD
Expand Down Expand Up @@ -119,6 +129,7 @@ components:
- id
- name
- apCode
- fullAddress
- postcode
- apArea
- bedCount
Expand Down Expand Up @@ -739,7 +750,7 @@ components:
type: object
properties:
premise:
$ref: '#/components/schemas/Cas1PremisesSummary'
$ref: '#/components/schemas/Cas1Premises'
startDate:
type: string
format: date
Expand Down
17 changes: 14 additions & 3 deletions src/main/resources/static/codegen/built-cas1-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ paths:
content:
'application/json':
schema:
$ref: '#/components/schemas/Cas1PremisesSummary'
$ref: '#/components/schemas/Cas1Premises'
401:
$ref: '#/components/responses/401Response'
403:
Expand Down Expand Up @@ -6036,12 +6036,19 @@ components:
example: 22
supportsSpaceBookings:
type: boolean
fullAddress:
description: Full address, excluding postcode
type: string
postcode:
type: string
required:
- id
- name
- apArea
- bedCount
- supportsSpaceBookings
- fullAddress
- postcode
Cas1PremisesSearchResultSummary:
type: object
properties:
Expand Down Expand Up @@ -6094,7 +6101,7 @@ components:
- apArea
- characteristics
- fullAddress
Cas1PremisesSummary:
Cas1Premises:
type: object
properties:
id:
Expand All @@ -6106,6 +6113,9 @@ components:
apCode:
type: string
example: NEHOPE1
fullAddress:
description: Full address, excluding postcode
type: string
postcode:
type: string
example: LS1 3AD
Expand Down Expand Up @@ -6136,6 +6146,7 @@ components:
- id
- name
- apCode
- fullAddress
- postcode
- apArea
- bedCount
Expand Down Expand Up @@ -6756,7 +6767,7 @@ components:
type: object
properties:
premise:
$ref: '#/components/schemas/Cas1PremisesSummary'
$ref: '#/components/schemas/Cas1Premises'
startDate:
type: string
format: date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ApprovedPremisesEntityFactory : Factory<ApprovedPremisesEntity> {
private var latitude: Yielded<Double> = { randomDouble(53.50, 54.99) }
private var longitude: Yielded<Double> = { randomDouble(-1.56, 1.10) }
private var addressLine1: Yielded<String> = { randomStringUpperCase(10) }
private var addressLine2: Yielded<String> = { randomStringUpperCase(10) }
private var town: Yielded<String> = { randomStringUpperCase(10) }
private var addressLine2: Yielded<String?> = { randomStringUpperCase(10) }
private var town: Yielded<String?> = { randomStringUpperCase(10) }
private var notes: Yielded<String> = { randomStringUpperCase(15) }
private var emailAddress: Yielded<String?> = { randomStringUpperCase(10) }
private var service: Yielded<String> = { "CAS1" }
Expand Down Expand Up @@ -70,11 +70,11 @@ class ApprovedPremisesEntityFactory : Factory<ApprovedPremisesEntity> {
this.addressLine1 = { addressLine1 }
}

fun withAddressLine2(addressLine2: String) = apply {
fun withAddressLine2(addressLine2: String?) = apply {
this.addressLine2 = { addressLine2 }
}

fun withTown(town: String) = apply {
fun withTown(town: String?) = apply {
this.town = { town }
}

Expand Down
Loading

0 comments on commit 14f7408

Please sign in to comment.