Fix booking snapshot lazy loading
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s

This commit is contained in:
androidlover5842
2026-01-31 14:45:40 +05:30
parent d692deb402
commit 34fc7ca7d2
3 changed files with 13 additions and 2 deletions

View File

@@ -18,14 +18,14 @@ class BookingSnapshotBuilder(
private val guestVehicleRepo: GuestVehicleRepo private val guestVehicleRepo: GuestVehicleRepo
) { ) {
fun build(propertyId: UUID, bookingId: UUID): BookingDetailResponse { fun build(propertyId: UUID, bookingId: UUID): BookingDetailResponse {
val booking = bookingRepo.findById(bookingId).orElseThrow { val booking = bookingRepo.findDetailedById(bookingId).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found") ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found")
} }
if (booking.property.id != propertyId) { if (booking.property.id != propertyId) {
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found for property") throw ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found for property")
} }
val stays = roomStayRepo.findByBookingId(bookingId) val stays = roomStayRepo.findByBookingIdWithRoom(bookingId)
val activeRooms = stays.filter { it.toAt == null } val activeRooms = stays.filter { it.toAt == null }
val roomsToShow = activeRooms.ifEmpty { stays } val roomsToShow = activeRooms.ifEmpty { stays }
val roomNumbers = roomsToShow.map { it.room.roomNumber } val roomNumbers = roomsToShow.map { it.room.roomNumber }

View File

@@ -13,4 +13,7 @@ interface BookingRepo : JpaRepository<Booking, UUID> {
fun findByPropertyIdAndStatusInOrderByCreatedAtDesc(propertyId: UUID, status: Collection<com.android.trisolarisserver.models.booking.BookingStatus>): List<Booking> fun findByPropertyIdAndStatusInOrderByCreatedAtDesc(propertyId: UUID, status: Collection<com.android.trisolarisserver.models.booking.BookingStatus>): List<Booking>
@EntityGraph(attributePaths = ["primaryGuest"]) @EntityGraph(attributePaths = ["primaryGuest"])
fun findByPropertyIdOrderByCreatedAtDesc(propertyId: UUID): List<Booking> fun findByPropertyIdOrderByCreatedAtDesc(propertyId: UUID): List<Booking>
@EntityGraph(attributePaths = ["property", "primaryGuest", "createdBy"])
fun findDetailedById(id: UUID): java.util.Optional<Booking>
} }

View File

@@ -43,6 +43,14 @@ interface RoomStayRepo : JpaRepository<RoomStay, UUID> {
""") """)
fun findByBookingId(@Param("bookingId") bookingId: UUID): List<RoomStay> fun findByBookingId(@Param("bookingId") bookingId: UUID): List<RoomStay>
@Query("""
select rs
from RoomStay rs
join fetch rs.room r
where rs.booking.id = :bookingId
""")
fun findByBookingIdWithRoom(@Param("bookingId") bookingId: UUID): List<RoomStay>
@Query(""" @Query("""
select rs select rs
from RoomStay rs from RoomStay rs