Adjust booking create and link guest vehicles to booking
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
This commit is contained in:
@@ -5,6 +5,7 @@ import com.android.trisolarisserver.controller.dto.GuestResponse
|
||||
import com.android.trisolarisserver.controller.dto.GuestVehicleRequest
|
||||
import com.android.trisolarisserver.models.booking.Guest
|
||||
import com.android.trisolarisserver.models.booking.GuestVehicle
|
||||
import com.android.trisolarisserver.db.repo.BookingRepo
|
||||
import com.android.trisolarisserver.db.repo.GuestRepo
|
||||
import com.android.trisolarisserver.db.repo.GuestRatingRepo
|
||||
import com.android.trisolarisserver.repo.GuestVehicleRepo
|
||||
@@ -22,6 +23,7 @@ class Guests(
|
||||
private val propertyAccess: PropertyAccess,
|
||||
private val propertyRepo: PropertyRepo,
|
||||
private val guestRepo: GuestRepo,
|
||||
private val bookingRepo: BookingRepo,
|
||||
private val guestVehicleRepo: GuestVehicleRepo,
|
||||
private val guestRatingRepo: GuestRatingRepo
|
||||
) {
|
||||
@@ -64,16 +66,32 @@ class Guests(
|
||||
requireMember(propertyAccess, propertyId, principal)
|
||||
|
||||
val (property, guest) = requirePropertyGuest(propertyRepo, guestRepo, propertyId, guestId)
|
||||
if (guestVehicleRepo.existsByPropertyIdAndVehicleNumberIgnoreCase(property.id!!, request.vehicleNumber)) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Vehicle number already exists")
|
||||
val booking = bookingRepo.findById(request.bookingId).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found")
|
||||
}
|
||||
if (booking.property.id != property.id) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Booking not in property")
|
||||
}
|
||||
if (booking.primaryGuest != null && booking.primaryGuest?.id != guest.id) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Booking linked to different guest")
|
||||
}
|
||||
|
||||
val vehicle = GuestVehicle(
|
||||
property = property,
|
||||
guest = guest,
|
||||
vehicleNumber = request.vehicleNumber.trim()
|
||||
)
|
||||
guestVehicleRepo.save(vehicle)
|
||||
val existing = guestVehicleRepo.findByPropertyIdAndVehicleNumberIgnoreCase(property.id!!, request.vehicleNumber)
|
||||
if (existing != null) {
|
||||
if (existing.guest.id != guest.id) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Vehicle number already exists")
|
||||
}
|
||||
existing.booking = booking
|
||||
guestVehicleRepo.save(existing)
|
||||
} else {
|
||||
val vehicle = GuestVehicle(
|
||||
property = property,
|
||||
guest = guest,
|
||||
booking = booking,
|
||||
vehicleNumber = request.vehicleNumber.trim()
|
||||
)
|
||||
guestVehicleRepo.save(vehicle)
|
||||
}
|
||||
return setOf(guest).toResponse(guestVehicleRepo, guestRatingRepo).first()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user