Skip to content

Commit

Permalink
Merge pull request #2768 from ministryofjustice/CAS-1343-move-lostbed…
Browse files Browse the repository at this point in the history
…-to-cas3-namespace

CAS-1343 Move LostBed to Cas3 namespace
  • Loading branch information
muhammad-elabdulla authored Jan 7, 2025
2 parents 63e2a97 + dc8d8bf commit f5cb737
Show file tree
Hide file tree
Showing 49 changed files with 547 additions and 533 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.ValidatableActio
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.BedService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.BookingService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.GetBookingForPremisesResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.LostBedService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.PremisesService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.RoomService
Expand All @@ -69,6 +68,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserAccessServic
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.cas3.Cas3BookingService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas3.Cas3LostBedService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas3.Cas3PremisesService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ArrivalTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.BedDetailTransformer
Expand All @@ -79,13 +79,13 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.Confirmation
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.DateChangeTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.DepartureTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ExtensionTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.LostBedCancellationTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.LostBedsTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.PremisesSummaryTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.PremisesTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.RoomTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.StaffMemberTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.TurnaroundTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas3.Cas3LostBedCancellationTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas3.Cas3LostBedsTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromAuthorisableActionResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromCasResult
import java.time.LocalDate
Expand All @@ -102,12 +102,12 @@ class PremisesController(
private val offenderService: OffenderService,
private val bookingService: BookingService,
private val cas3BookingService: Cas3BookingService,
private val lostBedsService: LostBedService,
private val lostBedsService: Cas3LostBedService,
private val bedService: BedService,
private val premisesTransformer: PremisesTransformer,
private val premisesSummaryTransformer: PremisesSummaryTransformer,
private val bookingTransformer: BookingTransformer,
private val lostBedsTransformer: LostBedsTransformer,
private val cas3LostBedsTransformer: Cas3LostBedsTransformer,
private val arrivalTransformer: ArrivalTransformer,
private val cancellationTransformer: CancellationTransformer,
private val confirmationTransformer: ConfirmationTransformer,
Expand All @@ -117,7 +117,7 @@ class PremisesController(
private val staffMemberService: StaffMemberService,
private val roomService: RoomService,
private val roomTransformer: RoomTransformer,
private val lostBedCancellationTransformer: LostBedCancellationTransformer,
private val cas3LostBedCancellationTransformer: Cas3LostBedCancellationTransformer,
private val turnaroundTransformer: TurnaroundTransformer,
private val bedSummaryTransformer: BedSummaryTransformer,
private val bedDetailTransformer: BedDetailTransformer,
Expand Down Expand Up @@ -709,7 +709,7 @@ class PremisesController(

val lostBeds = extractResultEntityOrThrow(result)

return ResponseEntity.ok(lostBedsTransformer.transformJpaToApi(lostBeds))
return ResponseEntity.ok(cas3LostBedsTransformer.transformJpaToApi(lostBeds))
}

override fun premisesPremisesIdLostBedsGet(premisesId: UUID): ResponseEntity<List<LostBed>> {
Expand All @@ -724,7 +724,7 @@ class PremisesController(
throw ForbiddenProblem()
}

return ResponseEntity.ok(lostBeds.map(lostBedsTransformer::transformJpaToApi))
return ResponseEntity.ok(lostBeds.map(cas3LostBedsTransformer::transformJpaToApi))
}

override fun premisesPremisesIdLostBedsLostBedIdGet(premisesId: UUID, lostBedId: UUID): ResponseEntity<LostBed> {
Expand All @@ -740,7 +740,7 @@ class PremisesController(
val lostBed = premises.lostBeds.firstOrNull { it.id == lostBedId }
?: throw NotFoundProblem(lostBedId, "LostBed")

return ResponseEntity.ok(lostBedsTransformer.transformJpaToApi(lostBed))
return ResponseEntity.ok(cas3LostBedsTransformer.transformJpaToApi(lostBed))
}

override fun premisesPremisesIdLostBedsLostBedIdPut(
Expand Down Expand Up @@ -787,7 +787,7 @@ class PremisesController(
is ValidatableActionResult.Success -> validationResult.entity
}

return ResponseEntity.ok(lostBedsTransformer.transformJpaToApi(updatedLostBed))
return ResponseEntity.ok(cas3LostBedsTransformer.transformJpaToApi(updatedLostBed))
}

override fun premisesPremisesIdLostBedsLostBedIdCancellationsPost(
Expand Down Expand Up @@ -820,7 +820,7 @@ class PremisesController(
is ValidatableActionResult.Success -> cancelVoidBedspaceResult.entity
}

return ResponseEntity.ok(lostBedCancellationTransformer.transformJpaToApi(cancellation))
return ResponseEntity.ok(cas3LostBedCancellationTransformer.transformJpaToApi(cancellation))
}

override fun premisesPremisesIdStaffGet(premisesId: UUID): ResponseEntity<List<StaffMember>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Characteristi
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.DepartureReasonRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.DestinationProviderRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.LocalAuthorityAreaRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.LostBedReasonRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.MoveOnCategoryRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.NonArrivalReasonRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ProbationDeliveryUnitRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ProbationRegionRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ReferralRejectionReasonRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3LostBedReasonRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.ForbiddenProblem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApAreaTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.CancellationReasonTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.CharacteristicTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.DepartureReasonTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.DestinationProviderTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.LocalAuthorityAreaTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.LostBedReasonTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.MoveOnCategoryTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.NonArrivalReasonTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ProbationDeliveryUnitTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ProbationRegionTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ReferralRejectionReasonTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas3.Cas3LostBedReasonTransformer
import java.util.UUID

@Service
Expand All @@ -49,7 +49,7 @@ class ReferenceDataController(
private val moveOnCategoryRepository: MoveOnCategoryRepository,
private val destinationProviderRepository: DestinationProviderRepository,
private val cancellationReasonRepository: CancellationReasonRepository,
private val lostBedReasonRepository: LostBedReasonRepository,
private val cas3LostBedReasonRepository: Cas3LostBedReasonRepository,
private val localAuthorityAreaRepository: LocalAuthorityAreaRepository,
private val characteristicRepository: CharacteristicRepository,
private val probationRegionRepository: ProbationRegionRepository,
Expand All @@ -60,7 +60,7 @@ class ReferenceDataController(
private val moveOnCategoryTransformer: MoveOnCategoryTransformer,
private val destinationProviderTransformer: DestinationProviderTransformer,
private val cancellationReasonTransformer: CancellationReasonTransformer,
private val lostBedReasonTransformer: LostBedReasonTransformer,
private val cas3LostBedReasonTransformer: Cas3LostBedReasonTransformer,
private val localAuthorityAreaTransformer: LocalAuthorityAreaTransformer,
private val characteristicTransformer: CharacteristicTransformer,
private val probationRegionTransformer: ProbationRegionTransformer,
Expand Down Expand Up @@ -144,12 +144,12 @@ class ReferenceDataController(
}

override fun referenceDataLostBedReasonsGet(xServiceName: ServiceName?): ResponseEntity<List<LostBedReason>> {
val lostBedReasons = when (xServiceName != null) {
true -> lostBedReasonRepository.findAllByServiceScope(xServiceName.value)
false -> lostBedReasonRepository.findAll()
val lostBedReasons = when (xServiceName == ServiceName.temporaryAccommodation) {
true -> cas3LostBedReasonRepository.findAllByServiceScope(xServiceName.value)
false -> throw ForbiddenProblem()
}

return ResponseEntity.ok(lostBedReasons.map(lostBedReasonTransformer::transformJpaToApi))
return ResponseEntity.ok(lostBedReasons.map(cas3LostBedReasonTransformer::transformJpaToApi))
}

override fun referenceDataProbationRegionsGet(): ResponseEntity<List<ProbationRegion>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.BookingStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PropertyStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3LostBedsEntity
import java.time.LocalDate
import java.util.UUID

Expand Down Expand Up @@ -241,7 +242,7 @@ abstract class PremisesEntity(
@OneToMany(mappedBy = "premises")
val bookings: MutableList<BookingEntity>,
@OneToMany(mappedBy = "premises")
val lostBeds: MutableList<LostBedsEntity>,
val lostBeds: MutableList<Cas3LostBedsEntity>,
@OneToMany(mappedBy = "premises")
val rooms: MutableList<RoomEntity>,
@ManyToMany
Expand Down Expand Up @@ -274,7 +275,7 @@ class ApprovedPremisesEntity(
probationRegion: ProbationRegionEntity,
localAuthorityArea: LocalAuthorityAreaEntity,
bookings: MutableList<BookingEntity>,
lostBeds: MutableList<LostBedsEntity>,
lostBeds: MutableList<Cas3LostBedsEntity>,
var apCode: String,
var qCode: String,
rooms: MutableList<RoomEntity>,
Expand Down Expand Up @@ -317,6 +318,7 @@ enum class ApprovedPremisesGender {
WOMAN,
}

@SuppressWarnings("LongParameterList")
@Entity
@DiscriminatorValue("temporary-accommodation")
@Table(name = "temporary_accommodation_premises")
Expand All @@ -335,7 +337,7 @@ class TemporaryAccommodationPremisesEntity(
probationRegion: ProbationRegionEntity,
localAuthorityArea: LocalAuthorityAreaEntity?,
bookings: MutableList<BookingEntity>,
lostBeds: MutableList<LostBedsEntity>,
lostBeds: MutableList<Cas3LostBedsEntity>,
rooms: MutableList<RoomEntity>,
characteristics: MutableList<CharacteristicEntity>,
status: PropertyStatus,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity
package uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3

import jakarta.persistence.Entity
import jakarta.persistence.Id
Expand All @@ -12,22 +12,22 @@ import java.util.Objects
import java.util.UUID

@Repository
interface LostBedCancellationRepository : JpaRepository<LostBedCancellationEntity, UUID>
interface Cas3LostBedCancellationRepository : JpaRepository<Cas3LostBedCancellationEntity, UUID>

@Entity
@Table(name = "lost_bed_cancellations")
data class LostBedCancellationEntity(
data class Cas3LostBedCancellationEntity(
@Id
val id: UUID,
val createdAt: OffsetDateTime,
val notes: String?,
@OneToOne
@JoinColumn(name = "lost_bed_id")
val lostBed: LostBedsEntity,
val lostBed: Cas3LostBedsEntity,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is LostBedCancellationEntity) return false
if (other !is Cas3LostBedCancellationEntity) return false

if (id != other.id) return false
if (notes != other.notes) return false
Expand All @@ -38,5 +38,5 @@ data class LostBedCancellationEntity(

override fun hashCode() = Objects.hash(notes, createdAt)

override fun toString() = "LostBedCancellationEntity:$id"
override fun toString() = "Cas3LostBedCancellationEntity:$id"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity
package uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3

import jakarta.persistence.Entity
import jakarta.persistence.Id
Expand All @@ -10,20 +10,20 @@ import org.springframework.stereotype.Repository
import java.util.UUID

@Repository
interface LostBedReasonRepository : JpaRepository<LostBedReasonEntity, UUID> {
@Query("SELECT l FROM LostBedReasonEntity l WHERE l.serviceScope = :serviceName OR l.serviceScope = '*'")
fun findAllByServiceScope(serviceName: String): List<LostBedReasonEntity>
interface Cas3LostBedReasonRepository : JpaRepository<Cas3LostBedReasonEntity, UUID> {
@Query("SELECT l FROM Cas3LostBedReasonEntity l WHERE l.serviceScope = :serviceName OR l.serviceScope = '*'")
fun findAllByServiceScope(serviceName: String): List<Cas3LostBedReasonEntity>
}

@Entity
@Table(name = "lost_bed_reasons")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
data class LostBedReasonEntity(
data class Cas3LostBedReasonEntity(
@Id
val id: UUID,
val name: String,
val isActive: Boolean,
val serviceScope: String,
) {
override fun toString() = "LostBedReasonEntity:$id"
override fun toString() = "Cas3LostBedReasonEntity:$id"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity
package uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3

import jakarta.persistence.Entity
import jakarta.persistence.Id
Expand All @@ -9,25 +9,27 @@ import jakarta.persistence.Table
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.BedEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.PremisesEntity
import java.time.LocalDate
import java.util.Objects
import java.util.UUID

@Repository
interface LostBedsRepository : JpaRepository<LostBedsEntity, UUID> {
@Query("SELECT lb FROM LostBedsEntity lb WHERE lb.premises.id = :premisesId AND lb.startDate <= :endDate AND lb.endDate >= :startDate")
fun findAllByPremisesIdAndOverlappingDate(premisesId: UUID, startDate: LocalDate, endDate: LocalDate): List<LostBedsEntity>
interface Cas3LostBedsRepository : JpaRepository<Cas3LostBedsEntity, UUID> {
@Query("SELECT lb FROM Cas3LostBedsEntity lb WHERE lb.premises.id = :premisesId AND lb.startDate <= :endDate AND lb.endDate >= :startDate")
fun findAllByPremisesIdAndOverlappingDate(premisesId: UUID, startDate: LocalDate, endDate: LocalDate): List<Cas3LostBedsEntity>

@Query("SELECT MAX(lb.endDate) FROM LostBedsEntity lb WHERE lb.premises.id = :premisesId")
@Query("SELECT MAX(lb.endDate) FROM Cas3LostBedsEntity lb WHERE lb.premises.id = :premisesId")
fun getHighestBookingDate(premisesId: UUID): LocalDate?

@Query("SELECT lb FROM LostBedsEntity lb WHERE lb.bed.id IN :lostBedIds")
fun findByBedIds(lostBedIds: List<UUID>): List<LostBedsEntity>
@Query("SELECT lb FROM Cas3LostBedsEntity lb WHERE lb.bed.id IN :lostBedIds")
fun findByBedIds(lostBedIds: List<UUID>): List<Cas3LostBedsEntity>

@Query(
"""
SELECT lb
FROM LostBedsEntity lb
FROM Cas3LostBedsEntity lb
LEFT JOIN lb.cancellation c
WHERE lb.bed.id = :bedId AND
lb.startDate <= :endDate AND
Expand All @@ -36,39 +38,40 @@ interface LostBedsRepository : JpaRepository<LostBedsEntity, UUID> {
c is NULL
""",
)
fun findByBedIdAndOverlappingDate(bedId: UUID, startDate: LocalDate, endDate: LocalDate, thisEntityId: UUID?): List<LostBedsEntity>
fun findByBedIdAndOverlappingDate(bedId: UUID, startDate: LocalDate, endDate: LocalDate, thisEntityId: UUID?): List<Cas3LostBedsEntity>

@Query("SELECT lb FROM LostBedsEntity lb LEFT JOIN lb.cancellation c WHERE lb.startDate <= :endDate AND lb.endDate >= :startDate AND lb.bed = :bed AND c is NULL")
fun findAllByOverlappingDateForBed(startDate: LocalDate, endDate: LocalDate, bed: BedEntity): List<LostBedsEntity>
@Query("SELECT lb FROM Cas3LostBedsEntity lb LEFT JOIN lb.cancellation c WHERE lb.startDate <= :endDate AND lb.endDate >= :startDate AND lb.bed = :bed AND c is NULL")
fun findAllByOverlappingDateForBed(startDate: LocalDate, endDate: LocalDate, bed: BedEntity): List<Cas3LostBedsEntity>

@Query("SELECT lb FROM LostBedsEntity lb LEFT JOIN lb.cancellation c WHERE lb.premises.id = :premisesId AND c is NULL")
fun findAllActiveForPremisesId(premisesId: UUID): List<LostBedsEntity>
@Query("SELECT lb FROM Cas3LostBedsEntity lb LEFT JOIN lb.cancellation c WHERE lb.premises.id = :premisesId AND c is NULL")
fun findAllActiveForPremisesId(premisesId: UUID): List<Cas3LostBedsEntity>
}

@SuppressWarnings("LongParameterList")
@Entity
@Table(name = "lost_beds")
class LostBedsEntity(
class Cas3LostBedsEntity(
@Id
val id: UUID,
var startDate: LocalDate,
var endDate: LocalDate,
@ManyToOne
@JoinColumn(name = "lost_bed_reason_id")
var reason: LostBedReasonEntity,
var reason: Cas3LostBedReasonEntity,
var referenceNumber: String?,
var notes: String?,
@ManyToOne
@JoinColumn(name = "premises_id")
var premises: PremisesEntity,
@OneToOne(mappedBy = "lostBed")
var cancellation: LostBedCancellationEntity?,
var cancellation: Cas3LostBedCancellationEntity?,
@ManyToOne
@JoinColumn(name = "bed_id")
var bed: BedEntity,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is LostBedsEntity) return false
if (other !is Cas3LostBedsEntity) return false

if (id != other.id) return false
if (startDate != other.startDate) return false
Expand All @@ -82,5 +85,5 @@ class LostBedsEntity(

override fun hashCode() = Objects.hash(id, startDate, endDate, reason, referenceNumber, notes)

override fun toString() = "LostBedsEntity:$id"
override fun toString() = "Cas3LostBedsEntity:$id"
}
Loading

0 comments on commit f5cb737

Please sign in to comment.