Skip to content

Commit

Permalink
Merge branch 'main' into CAS-480-include-wheelchair-accessible-in-search
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-elabdulla authored Sep 26, 2024
2 parents 319a0cf + f58563e commit 11d61bc
Show file tree
Hide file tree
Showing 30 changed files with 430 additions and 193 deletions.
1 change: 0 additions & 1 deletion helm_deploy/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ generic-service:

FEATURE-FLAGS_CAS1-APPEAL-MANAGER-CAN-ASSESS-APPLICATIONS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-ADJUDICATIONS-UP-TO-12-MONTHS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-CONVICTION-DOCUMENTS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-SPECIFIC-PRISON-NOTE-TYPES: true
FEATURE-FLAGS_CAS1-WOMENS-ESTATE-ENABLED: true
FEATURE-FLAGS_USE-AP-AND-DELIUS-TO-UPDATE-USERS: true
Expand Down
1 change: 0 additions & 1 deletion helm_deploy/values-preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ generic-service:

FEATURE-FLAGS_CAS1-APPEAL-MANAGER-CAN-ASSESS-APPLICATIONS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-ADJUDICATIONS-UP-TO-12-MONTHS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-CONVICTION-DOCUMENTS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-SPECIFIC-PRISON-NOTE-TYPES: true
FEATURE-FLAGS_CAS1-WOMENS-ESTATE-ENABLED: false
FEATURE-FLAGS_USE-AP-AND-DELIUS-TO-UPDATE-USERS: true
Expand Down
1 change: 0 additions & 1 deletion helm_deploy/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ generic-service:

FEATURE-FLAGS_CAS1-APPEAL-MANAGER-CAN-ASSESS-APPLICATIONS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-ADJUDICATIONS-UP-TO-12-MONTHS: false
FEATURE-FLAGS_CAS1-ONLY-LIST-CONVICTION-DOCUMENTS: false
FEATURE-FLAGS_CAS1-ONLY-LIST-SPECIFIC-PRISON-NOTE-TYPES: false
FEATURE-FLAGS_CAS1-WOMENS-ESTATE-ENABLED: false
FEATURE-FLAGS_USE-AP-AND-DELIUS-TO-UPDATE-USERS: true
Expand Down
1 change: 0 additions & 1 deletion helm_deploy/values-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ generic-service:

FEATURE-FLAGS_CAS1-APPEAL-MANAGER-CAN-ASSESS-APPLICATIONS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-ADJUDICATIONS-UP-TO-12-MONTHS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-CONVICTION-DOCUMENTS: true
FEATURE-FLAGS_CAS1-ONLY-LIST-SPECIFIC-PRISON-NOTE-TYPES: true
FEATURE-FLAGS_CAS1-WOMENS-ESTATE-ENABLED: true
FEATURE-FLAGS_USE-AP-AND-DELIUS-TO-UPDATE-USERS: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.AssessmentServic
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.HttpAuthService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.RequestForPlacementService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.SpringConfigFeatureFlagService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1WithdrawableService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.WithdrawableEntitiesWithNotes
Expand Down Expand Up @@ -83,7 +82,6 @@ class ApplicationsController(
private val appealTransformer: AppealTransformer,
private val requestForPlacementService: RequestForPlacementService,
private val withdrawableTransformer: WithdrawableTransformer,
private val featureFlagService: SpringConfigFeatureFlagService,
) : ApplicationsApiDelegate {

override fun applicationsGet(xServiceName: ServiceName?): ResponseEntity<List<ApplicationSummary>> {
Expand Down Expand Up @@ -453,14 +451,8 @@ class ApplicationsController(
}

val transformedDocuments = when (application) {
is ApprovedPremisesApplicationEntity -> documentTransformer.transformToApi(
groupedDocuments = documents,
onlyConvictionDocuments = featureFlagService.getBooleanFlag("cas1-only-list-conviction-documents"),
)
is TemporaryAccommodationApplicationEntity -> documentTransformer.transformToApi(
groupedDocuments = documents,
convictionId = application.convictionId,
)
is ApprovedPremisesApplicationEntity -> documentTransformer.transformToApiUnfiltered(documents)
is TemporaryAccommodationApplicationEntity -> documentTransformer.transformToApiFiltered(documents, application.convictionId)
else -> throw RuntimeException("Unsupported Application type: ${application::class.qualifiedName}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.controller.cas1
import org.springframework.http.ResponseEntity
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.Cas1PremisesBasicSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesGender
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserPermission
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserAccessService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService
Expand All @@ -29,4 +32,20 @@ class Cas1PremisesController(
),
)
}

override fun getPremisesSummaries(gender: Cas1ApprovedPremisesGender?): ResponseEntity<List<Cas1PremisesBasicSummary>> {
return ResponseEntity
.ok()
.body(
cas1PremisesService.getPremises(
gender = when (gender) {
Cas1ApprovedPremisesGender.man -> ApprovedPremisesGender.MAN
Cas1ApprovedPremisesGender.woman -> ApprovedPremisesGender.WOMAN
null -> null
},
).map {
cas1PremisesTransformer.toPremiseBasicSummary(it)
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ interface PremisesRepository : JpaRepository<PremisesEntity, UUID> {
)
fun findAllApprovedPremisesSummary(probationRegionId: UUID?, apAreaId: UUID?): List<ApprovedPremisesSummary>

@Query("SELECT p as premises FROM ApprovedPremisesEntity p WHERE p.id = :id")
fun findApprovedPremisesByIdOrNull(id: UUID): ApprovedPremisesEntity?

@Query("SELECT p as premises, $BED_COUNT_QUERY as bedCount FROM PremisesEntity p WHERE TYPE(p) = :type")
fun <T : PremisesEntity> findAllByType(type: Class<T>): List<PremisesWithBedCount>

Expand Down Expand Up @@ -175,6 +172,12 @@ where
fun getBookingSummariesForPremisesId(premisesId: UUID): List<BookingSummary>
}

@Repository
interface ApprovedPremisesRepository : JpaRepository<ApprovedPremisesEntity, UUID> {
@Query("SELECT p as premises FROM ApprovedPremisesEntity p WHERE :gender IS NULL OR p.gender = :gender")
fun findForSummaries(gender: ApprovedPremisesGender?): List<ApprovedPremisesEntity>
}

@Entity
@Table(name = "premises")
@DiscriminatorColumn(name = "service")
Expand Down Expand Up @@ -214,6 +217,7 @@ abstract class PremisesEntity(
var status: PropertyStatus,
)

@SuppressWarnings("LongParameterList")
@Entity
@DiscriminatorValue("approved-premises")
@Table(name = "approved_premises")
Expand All @@ -239,6 +243,8 @@ class ApprovedPremisesEntity(
characteristics: MutableList<CharacteristicEntity>,
status: PropertyStatus,
var point: Point?, // TODO: Make not-null once Premises have had point added in all environments
@Enumerated(value = EnumType.STRING)
val gender: ApprovedPremisesGender,
) : PremisesEntity(
id,
name,
Expand All @@ -259,6 +265,11 @@ class ApprovedPremisesEntity(
status,
)

enum class ApprovedPremisesGender {
MAN,
WOMAN,
}

@Entity
@DiscriminatorValue("temporary-accommodation")
@Table(name = "temporary_accommodation_premises")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.locationtech.jts.geom.PrecisionModel
import org.slf4j.LoggerFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PropertyStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesGender
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.LocalAuthorityAreaEntity
Expand Down Expand Up @@ -58,6 +59,7 @@ class ApprovedPremisesSeedJob(
"qCode",
"latitude",
"longitude",
"gender",
),
) {
private val log = LoggerFactory.getLogger(this::class.java)
Expand Down Expand Up @@ -97,6 +99,7 @@ class ApprovedPremisesSeedJob(
qCode = columns["qCode"]!!,
latitude = columns["latitude"]!!.toDoubleOrNull(),
longitude = columns["longitude"]!!.toDoubleOrNull(),
gender = ApprovedPremisesGender.valueOf(columns["gender"]!!),
)

override fun processRow(row: ApprovedPremisesSeedCsvRow) {
Expand All @@ -111,7 +114,7 @@ class ApprovedPremisesSeedJob(
val characteristics = characteristicsFromRow(row)

if (existingPremises != null) {
updateExistingApprovedPremises(row, existingPremises as ApprovedPremisesEntity, probationRegion, localAuthorityArea, characteristics)
updateExistingApprovedPremises(row, existingPremises, probationRegion, localAuthorityArea, characteristics)
} else {
createNewApprovedPremises(row, probationRegion, localAuthorityArea, characteristics)
}
Expand Down Expand Up @@ -175,6 +178,7 @@ class ApprovedPremisesSeedJob(
longitude = row.longitude,
latitude = row.latitude,
point = if (row.longitude != null && row.latitude != null) geometryFactory.createPoint(Coordinate(row.latitude, row.longitude)) else null,
gender = row.gender,
),
)

Expand Down Expand Up @@ -270,4 +274,5 @@ data class ApprovedPremisesSeedCsvRow(
val qCode: String,
val latitude: Double?,
val longitude: Double?,
val gender: ApprovedPremisesGender,
)
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ class PremisesService(
premisesRepository.findAllByProbationRegionAndType(probationRegionId, it)
} ?: listOf()

fun getApprovedPremises(premisesId: UUID) = premisesRepository.findApprovedPremisesByIdOrNull(premisesId)

fun getPremises(premisesId: UUID): PremisesEntity? = premisesRepository.findByIdOrNull(premisesId)

fun getPremisesSummary(premisesId: UUID): List<BookingSummary> = premisesRepository.getBookingSummariesForPremisesId(premisesId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.PremisesRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesGender
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.PremisesService
import java.util.UUID

@Service
class Cas1PremisesService(
val premisesRepository: PremisesRepository,
val premisesRepository: ApprovedPremisesRepository,
val premisesService: PremisesService,
val cas1OutOfServiceBedService: Cas1OutOfServiceBedService,
) {
fun getPremisesSummary(premisesId: UUID): CasResult<Cas1PremisesSummaryInfo> {
val premise = premisesRepository.findByIdOrNull(premisesId)
if (premise !is ApprovedPremisesEntity) return CasResult.NotFound("premises", premisesId.toString())
?: return CasResult.NotFound("premises", premisesId.toString())

val bedCount = premisesService.getBedCount(premise)
val outOfServiceBedsCount = cas1OutOfServiceBedService.getActiveOutOfServiceBedsCountForPremisesId(premisesId)
Expand All @@ -31,6 +32,10 @@ class Cas1PremisesService(
)
}

fun getPremises(gender: ApprovedPremisesGender?) = premisesRepository.findForSummaries(gender)

fun findPremiseById(id: UUID) = premisesRepository.findByIdOrNull(id)

data class Cas1PremisesSummaryInfo(
val entity: ApprovedPremisesEntity,
val bedCount: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PaginationMetadata
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.validatedCasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.PlacementRequestService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.PremisesService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.PageCriteria
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.getMetadata
import java.time.LocalDate
Expand All @@ -32,7 +31,7 @@ import java.util.UUID

@Service
class Cas1SpaceBookingService(
private val premisesService: PremisesService,
private val cas1PremisesService: Cas1PremisesService,
private val placementRequestService: PlacementRequestService,
private val cas1SpaceBookingRepository: Cas1SpaceBookingRepository,
private val cas1SpaceSearchRepository: Cas1SpaceSearchRepository,
Expand All @@ -51,7 +50,7 @@ class Cas1SpaceBookingService(
departureDate: LocalDate,
createdBy: UserEntity,
): CasResult<Cas1SpaceBookingEntity> = validatedCasResult {
val premises = premisesService.getApprovedPremises(premisesId)
val premises = cas1PremisesService.findPremiseById(premisesId)
if (premises == null) {
"$.premisesId" hasValidationError "doesNotExist"
}
Expand Down Expand Up @@ -133,7 +132,7 @@ class Cas1SpaceBookingService(
bookingId: UUID,
cas1NewArrival: Cas1NewArrival,
): CasResult<Cas1SpaceBookingEntity> = validatedCasResult {
val premises = premisesService.getApprovedPremises(premisesId)
val premises = cas1PremisesService.findPremiseById(premisesId)
if (premises == null) {
"$.premisesId" hasValidationError "doesNotExist"
}
Expand Down Expand Up @@ -180,7 +179,7 @@ class Cas1SpaceBookingService(
bookingId: UUID,
cas1NewDeparture: Cas1NewDeparture,
): CasResult<Cas1SpaceBookingEntity> = validatedCasResult {
val premises = premisesService.getApprovedPremises(premisesId)
val premises = cas1PremisesService.findPremiseById(premisesId)
if (premises == null) {
"$.premisesId" hasValidationError "doesNotExist"
}
Expand Down Expand Up @@ -239,7 +238,7 @@ class Cas1SpaceBookingService(
filterCriteria: SpaceBookingFilterCriteria,
pageCriteria: PageCriteria<Cas1SpaceBookingSummarySortField>,
): CasResult<Pair<List<Cas1SpaceBookingSearchResult>, PaginationMetadata?>> {
if (premisesService.getApprovedPremises(premisesId) == null) return CasResult.NotFound("premises", premisesId.toString())
if (cas1PremisesService.findPremiseById(premisesId) == null) return CasResult.NotFound("premises", premisesId.toString())

val page = cas1SpaceBookingRepository.search(
filterCriteria.residency?.name,
Expand All @@ -265,7 +264,7 @@ class Cas1SpaceBookingService(
}

fun getBooking(premisesId: UUID, bookingId: UUID): CasResult<Cas1SpaceBookingEntity> {
if (premisesService.getApprovedPremises(premisesId) !is ApprovedPremisesEntity) return CasResult.NotFound("premises", premisesId.toString())
if (cas1PremisesService.findPremiseById(premisesId) !is ApprovedPremisesEntity) return CasResult.NotFound("premises", premisesId.toString())

val booking = cas1SpaceBookingRepository.findByIdOrNull(bookingId) ?: return CasResult.NotFound("booking", bookingId.toString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,27 @@ class DocumentTransformer {

private val log = LoggerFactory.getLogger(this::class.java)

fun transformToApiFiltered(groupedDocuments: GroupedDocuments, convictionId: Long?) =
transformToApi(groupedDocuments) { it.convictionId == convictionId.toString() }

fun transformToApiUnfiltered(groupedDocuments: GroupedDocuments) =
transformToApi(groupedDocuments) { true }

fun transformToApi(
groupedDocuments: GroupedDocuments,
onlyConvictionDocuments: Boolean = false,
convictionId: Long? = null,
convictionDocFilter: (ConvictionDocuments) -> Boolean,
): List<Document> {
val convictionDocFilter: (ConvictionDocuments) -> Boolean = { document -> convictionId?.let { document.convictionId == convictionId.toString() } ?: true }
val offenderDocuments = documentsWithIdsAndNames(groupedDocuments.documents).map {
Document(
id = it.id!!,
level = DocumentLevel.offender,
fileName = it.documentName!!,
createdAt = it.createdAt.toInstant(ZoneOffset.UTC),
typeCode = it.type.code,
typeDescription = it.type.description,
description = it.extendedDescription,
)
}

val filteredConvictionDocuments = groupedDocuments
.convictions
Expand All @@ -38,23 +53,7 @@ class DocumentTransformer {
)
}

when (onlyConvictionDocuments) {
true -> return convictionDocuments
false -> {
val offenderDocuments = documentsWithIdsAndNames(groupedDocuments.documents).map {
Document(
id = it.id!!,
level = DocumentLevel.offender,
fileName = it.documentName!!,
createdAt = it.createdAt.toInstant(ZoneOffset.UTC),
typeCode = it.type.code,
typeDescription = it.type.description,
description = it.extendedDescription,
)
}
return offenderDocuments + convictionDocuments
}
}
return offenderDocuments + convictionDocuments
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1

import org.springframework.stereotype.Service
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.PremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApAreaTransformer

Expand All @@ -22,4 +25,13 @@ class Cas1PremisesTransformer(
apArea = apAreaTransformer.transformJpaToApi(entity.probationRegion.apArea!!),
)
}

fun toPremiseBasicSummary(entity: PremisesEntity): Cas1PremisesBasicSummary {
val apArea = entity.probationRegion.apArea!!
return Cas1PremisesBasicSummary(
id = entity.id,
name = entity.name,
apArea = NamedId(apArea.id, apArea.name),
)
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ reports:
feature-flags:
cas1-appeal-manager-can-assess-applications: false
cas1-only-list-adjudications-up-to-12-months: false
cas1-only-list-conviction-documents: false
cas1-only-list-specific-prison-note-types: false
cas1-womens-estate-enabled: false
use-ap-and-delius-to-update-users: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE approved_premises ADD gender TEXT NOT NULL DEFAULT 'MALE';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE approved_premises ALTER COLUMN gender SET DEFAULT 'MAN'::text;
UPDATE approved_premises SET gender = 'MAN';
Loading

0 comments on commit 11d61bc

Please sign in to comment.