Skip to content

Commit

Permalink
Merge pull request #2779 from ministryofjustice/CAS-1343-rename-to-vo…
Browse files Browse the repository at this point in the history
…id-bedspace

CAS-1343 Rename to void bedspace
  • Loading branch information
muhammad-elabdulla authored Jan 9, 2025
2 parents 523bd4b + 48a0f98 commit 363a262
Show file tree
Hide file tree
Showing 33 changed files with 261 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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.Cas3VoidBedspacesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceEntity
import java.time.LocalDate
import java.util.UUID

Expand Down Expand Up @@ -247,7 +247,7 @@ abstract class PremisesEntity(
@OneToMany(mappedBy = "premises")
val bookings: MutableList<BookingEntity>,
@OneToMany(mappedBy = "premises")
val voidBedspaces: MutableList<Cas3VoidBedspacesEntity>,
val voidBedspaces: MutableList<Cas3VoidBedspaceEntity>,
@OneToMany(mappedBy = "premises")
val rooms: MutableList<RoomEntity>,
@ManyToMany
Expand Down Expand Up @@ -280,7 +280,7 @@ class ApprovedPremisesEntity(
probationRegion: ProbationRegionEntity,
localAuthorityArea: LocalAuthorityAreaEntity,
bookings: MutableList<BookingEntity>,
lostBeds: MutableList<Cas3VoidBedspacesEntity>,
lostBeds: MutableList<Cas3VoidBedspaceEntity>,
var apCode: String,
var qCode: String,
rooms: MutableList<RoomEntity>,
Expand Down Expand Up @@ -364,7 +364,7 @@ class TemporaryAccommodationPremisesEntity(
probationRegion: ProbationRegionEntity,
localAuthorityArea: LocalAuthorityAreaEntity?,
bookings: MutableList<BookingEntity>,
lostBeds: MutableList<Cas3VoidBedspacesEntity>,
lostBeds: MutableList<Cas3VoidBedspaceEntity>,
rooms: MutableList<RoomEntity>,
characteristics: MutableList<CharacteristicEntity>,
status: PropertyStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class Cas3VoidBedspaceCancellationEntity(
val notes: String?,
@OneToOne
@JoinColumn(name = "cas3_void_bedspace_id")
val voidBedspace: Cas3VoidBedspacesEntity,
val voidBedspace: Cas3VoidBedspaceEntity,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import java.util.Objects
import java.util.UUID

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

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

@Query("SELECT lb FROM Cas3VoidBedspacesEntity lb WHERE lb.bed.id IN :voidBedspaceIds")
fun findByBedIds(voidBedspaceIds: List<UUID>): List<Cas3VoidBedspacesEntity>
@Query("SELECT lb FROM Cas3VoidBedspaceEntity lb WHERE lb.bed.id IN :voidBedspaceIds")
fun findByBedIds(voidBedspaceIds: List<UUID>): List<Cas3VoidBedspaceEntity>

@Query(
"""
SELECT lb
FROM Cas3VoidBedspacesEntity lb
FROM Cas3VoidBedspaceEntity lb
LEFT JOIN lb.cancellation c
WHERE lb.bed.id = :bedId AND
lb.startDate <= :endDate AND
Expand All @@ -38,19 +38,19 @@ interface Cas3VoidBedspacesRepository : JpaRepository<Cas3VoidBedspacesEntity, U
c is NULL
""",
)
fun findByBedIdAndOverlappingDate(bedId: UUID, startDate: LocalDate, endDate: LocalDate, thisEntityId: UUID?): List<Cas3VoidBedspacesEntity>
fun findByBedspaceIdAndOverlappingDate(bedId: UUID, startDate: LocalDate, endDate: LocalDate, thisEntityId: UUID?): List<Cas3VoidBedspaceEntity>

@Query("SELECT lb FROM Cas3VoidBedspacesEntity 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<Cas3VoidBedspacesEntity>
@Query("SELECT lb FROM Cas3VoidBedspaceEntity lb LEFT JOIN lb.cancellation c WHERE lb.startDate <= :endDate AND lb.endDate >= :startDate AND lb.bed = :bed AND c is NULL")
fun findAllByOverlappingDateForBedspace(startDate: LocalDate, endDate: LocalDate, bed: BedEntity): List<Cas3VoidBedspaceEntity>

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

@SuppressWarnings("LongParameterList")
@Entity
@Table(name = "cas3_void_bedspaces")
class Cas3VoidBedspacesEntity(
class Cas3VoidBedspaceEntity(
@Id
val id: UUID,
var startDate: LocalDate,
Expand All @@ -71,7 +71,7 @@ class Cas3VoidBedspacesEntity(
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Cas3VoidBedspacesEntity) return false
if (other !is Cas3VoidBedspaceEntity) return false

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

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

override fun toString() = "Cas3VoidBedspacesEntity:$id"
override fun toString() = "Cas3VoidBedspaceEntity:$id"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BedUsageReportGenerator(

override val convert: BedEntity.(properties: BedUsageReportProperties) -> List<BedUsageReportRow> = { properties ->
val bookings = bookingRepository.findAllByOverlappingDateForBed(properties.startDate, properties.endDate, this)
val voids = cas3VoidBedspacesRepository.findAllByOverlappingDateForBed(properties.startDate, properties.endDate, this)
val voids = cas3VoidBedspacesRepository.findAllByOverlappingDateForBedspace(properties.startDate, properties.endDate, this)

val premises = this.room.premises
val temporaryAccommodationPremisesEntity = premises as? TemporaryAccommodationPremisesEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class VoidBedspacesReportGenerator(
val startOfMonth = LocalDate.of(properties.year, properties.month, 1)
val endOfMonth = LocalDate.of(properties.year, properties.month, startOfMonth.month.length(startOfMonth.isLeapYear))

val voidBedspaces = cas3VoidBedspacesRepository.findAllByOverlappingDateForBed(startOfMonth, endOfMonth, this)
val voidBedspaces = cas3VoidBedspacesRepository.findAllByOverlappingDateForBedspace(startOfMonth, endOfMonth, this)

voidBedspaces.map {
val bed = it.bed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class BookingService(
endDate: LocalDate,
thisEntityId: UUID?,
bedId: UUID,
) = cas3VoidBedspacesRepository.findByBedIdAndOverlappingDate(
) = cas3VoidBedspacesRepository.findByBedspaceIdAndOverlappingDate(
bedId,
startDate,
endDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Cas3BookingService(
return@validated it.id hasConflictError "A Booking already exists for dates from ${it.arrivalDate} to ${it.lastUnavailableDate} which overlaps with the desired dates"
}

getLostBedspaceWithConflictingDates(arrivalDate, expectedLastUnavailableDate, null, bedId)?.let {
getVoidBedspaceWithConflictingDates(arrivalDate, expectedLastUnavailableDate, null, bedId)?.let {
return@validated it.id hasConflictError "A Lost Bed already exists for dates from ${it.startDate} to ${it.endDate} which overlaps with the desired dates"
}

Expand Down Expand Up @@ -248,7 +248,7 @@ class Cas3BookingService(
return@validated it.id hasConflictError "A Booking already exists for dates from ${it.arrivalDate} to ${it.lastUnavailableDate} which overlaps with the desired dates"
}

getLostBedspaceWithConflictingDates(booking.arrivalDate, expectedLastUnavailableDate, null, booking.bed!!.id)?.let {
getVoidBedspaceWithConflictingDates(booking.arrivalDate, expectedLastUnavailableDate, null, booking.bed!!.id)?.let {
return@validated it.id hasConflictError "A Lost Bed already exists for dates from ${it.startDate} to ${it.endDate} which overlaps with the desired dates"
}

Expand Down Expand Up @@ -426,7 +426,7 @@ class Cas3BookingService(
return@validated it.id hasConflictError "A Booking already exists for dates from ${it.arrivalDate} to ${it.lastUnavailableDate} which overlaps with the desired dates"
}

getLostBedspaceWithConflictingDates(booking.arrivalDate, expectedLastUnavailableDate, null, bedId)?.let {
getVoidBedspaceWithConflictingDates(booking.arrivalDate, expectedLastUnavailableDate, null, bedId)?.let {
return@validated it.id hasConflictError "A Lost Bed already exists for dates from ${it.startDate} to ${it.endDate} which overlaps with the desired dates"
}

Expand All @@ -452,7 +452,7 @@ class Cas3BookingService(
}

@SuppressWarnings("ThrowsCount")
fun getBooking(id: UUID): AuthorisableActionResult<uk.gov.justice.digital.hmpps.approvedpremisesapi.service.BookingService.BookingAndPersons> {
fun getBooking(id: UUID): AuthorisableActionResult<BookingAndPersons> {
val booking = bookingRepository.findByIdOrNull(id)
?: return AuthorisableActionResult.NotFound("Booking", id.toString())

Expand All @@ -469,7 +469,7 @@ class Cas3BookingService(
)

return AuthorisableActionResult.Success(
uk.gov.justice.digital.hmpps.approvedpremisesapi.service.BookingService.BookingAndPersons(
BookingAndPersons(
booking,
personInfo,
),
Expand Down Expand Up @@ -540,12 +540,12 @@ class Cas3BookingService(
return candidateBookings.firstOrNull { it.lastUnavailableDate >= arrivalDate }
}

private fun getLostBedspaceWithConflictingDates(
private fun getVoidBedspaceWithConflictingDates(
startDate: LocalDate,
endDate: LocalDate,
thisEntityId: UUID?,
bedId: UUID,
) = cas3VoidBedspacesRepository.findByBedIdAndOverlappingDate(
) = cas3VoidBedspacesRepository.findByBedspaceIdAndOverlappingDate(
bedId,
startDate,
endDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.TemporaryAcco
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.TemporaryAccommodationPremisesSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceCancellationEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceCancellationRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceReasonRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.Availability
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.ValidationErrors
Expand Down Expand Up @@ -317,7 +317,7 @@ class Cas3PremisesService(
referenceNumber: String?,
notes: String?,
bedId: UUID,
): ValidatableActionResult<Cas3VoidBedspacesEntity> =
): ValidatableActionResult<Cas3VoidBedspaceEntity> =
validated {
if (endDate.isBefore(startDate)) {
"$.endDate" hasValidationError "beforeStartDate"
Expand All @@ -338,7 +338,7 @@ class Cas3PremisesService(
}

val voidBedspacesEntity = cas3VoidBedspacesRepository.save(
Cas3VoidBedspacesEntity(
Cas3VoidBedspaceEntity(
id = UUID.randomUUID(),
premises = premises,
startDate = startDate,
Expand All @@ -361,7 +361,7 @@ class Cas3PremisesService(
reasonId: UUID,
referenceNumber: String?,
notes: String?,
): AuthorisableActionResult<ValidatableActionResult<Cas3VoidBedspacesEntity>> {
): AuthorisableActionResult<ValidatableActionResult<Cas3VoidBedspaceEntity>> {
val voidBedspace = cas3VoidBedspacesRepository.findByIdOrNull(voidBedspaceId)
?: return AuthorisableActionResult.NotFound()

Expand Down Expand Up @@ -396,7 +396,7 @@ class Cas3PremisesService(
}

fun cancelVoidBedspace(
voidBedspace: Cas3VoidBedspacesEntity,
voidBedspace: Cas3VoidBedspaceEntity,
notes: String?,
) = validated<Cas3VoidBedspaceCancellationEntity> {
if (voidBedspace.cancellation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas3
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.LostBed
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.LostBedStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceEntity

@Component
class Cas3VoidBedspacesTransformer(
private val cas3VoidBedspaceReasonTransformer: Cas3VoidBedspaceReasonTransformer,
private val cas3VoidBedspaceCancellationTransformer: Cas3VoidBedspaceCancellationTransformer,
) {
fun transformJpaToApi(jpa: Cas3VoidBedspacesEntity) = LostBed(
fun transformJpaToApi(jpa: Cas3VoidBedspaceEntity) = LostBed(
id = jpa.id,
startDate = jpa.startDate,
endDate = jpa.endDate,
Expand All @@ -24,7 +24,7 @@ class Cas3VoidBedspacesTransformer(
roomName = jpa.bed.room.name,
)

private fun determineStatus(jpa: Cas3VoidBedspacesEntity) = when {
private fun determineStatus(jpa: Cas3VoidBedspaceEntity) = when {
jpa.cancellation != null -> LostBedStatus.cancelled
else -> LostBedStatus.active
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.cas3
import io.github.bluegroundltd.kfactory.Factory
import io.github.bluegroundltd.kfactory.Yielded
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceCancellationEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.randomDateTimeBefore
import java.time.OffsetDateTime
import java.util.UUID
Expand All @@ -12,7 +12,7 @@ class Cas3VoidBedspaceCancellationEntityFactory : Factory<Cas3VoidBedspaceCancel
private var id: Yielded<UUID> = { UUID.randomUUID() }
private var createdAt: Yielded<OffsetDateTime> = { OffsetDateTime.now().randomDateTimeBefore(14) }
private var notes: Yielded<String>? = null
private var voidBedspace: Yielded<Cas3VoidBedspacesEntity>? = null
private var voidBedspace: Yielded<Cas3VoidBedspaceEntity>? = null

fun withId(id: UUID) = apply {
this.id = { id }
Expand All @@ -26,11 +26,11 @@ class Cas3VoidBedspaceCancellationEntityFactory : Factory<Cas3VoidBedspaceCancel
this.notes = { notes }
}

fun withYieldedVoidBedspace(voidBedspace: Yielded<Cas3VoidBedspacesEntity>) = apply {
fun withYieldedVoidBedspace(voidBedspace: Yielded<Cas3VoidBedspaceEntity>) = apply {
this.voidBedspace = voidBedspace
}

fun withVoidBedspace(voidBedspace: Cas3VoidBedspacesEntity) = apply {
fun withVoidBedspace(voidBedspace: Cas3VoidBedspaceEntity) = apply {
this.voidBedspace = { voidBedspace }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import io.github.bluegroundltd.kfactory.Yielded
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.BedEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.PremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceCancellationEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspaceReasonEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.randomDateAfter
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.randomDateBefore
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.randomStringMultiCaseWithNumbers
import java.time.LocalDate
import java.util.UUID

class Cas3VoidBedspacesEntityFactory : Factory<Cas3VoidBedspacesEntity> {
class Cas3VoidBedspaceEntityFactory : Factory<Cas3VoidBedspaceEntity> {
private var id: Yielded<UUID> = { UUID.randomUUID() }
private var startDate: Yielded<LocalDate> = { LocalDate.now().randomDateBefore(6) }
private var endDate: Yielded<LocalDate> = { LocalDate.now().randomDateAfter(6) }
Expand Down Expand Up @@ -73,7 +73,7 @@ class Cas3VoidBedspacesEntityFactory : Factory<Cas3VoidBedspacesEntity> {
}

@SuppressWarnings("TooGenericExceptionThrown")
override fun produce() = Cas3VoidBedspacesEntity(
override fun produce() = Cas3VoidBedspaceEntity(
id = this.id(),
startDate = this.startDate(),
endDate = this.endDate(),
Expand Down
Loading

0 comments on commit 363a262

Please sign in to comment.