Remove guest create endpoint
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:
@@ -2,17 +2,14 @@ package com.android.trisolarisserver.controller
|
||||
|
||||
import com.android.trisolarisserver.component.PropertyAccess
|
||||
import com.android.trisolarisserver.component.GuestSignatureStorage
|
||||
import com.android.trisolarisserver.controller.dto.GuestCreateRequest
|
||||
import com.android.trisolarisserver.controller.dto.GuestResponse
|
||||
import com.android.trisolarisserver.controller.dto.GuestUpdateRequest
|
||||
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.GuestDocumentRepo
|
||||
import com.android.trisolarisserver.db.repo.GuestRepo
|
||||
import com.android.trisolarisserver.db.repo.GuestRatingRepo
|
||||
import com.android.trisolarisserver.repo.AppUserRepo
|
||||
import com.android.trisolarisserver.repo.GuestVehicleRepo
|
||||
import com.android.trisolarisserver.repo.PropertyRepo
|
||||
import com.android.trisolarisserver.security.MyPrincipal
|
||||
@@ -21,7 +18,6 @@ import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
@@ -37,71 +33,9 @@ class Guests(
|
||||
private val bookingRepo: BookingRepo,
|
||||
private val guestVehicleRepo: GuestVehicleRepo,
|
||||
private val guestRatingRepo: GuestRatingRepo,
|
||||
private val guestDocumentRepo: GuestDocumentRepo,
|
||||
private val signatureStorage: GuestSignatureStorage,
|
||||
private val appUserRepo: AppUserRepo
|
||||
private val signatureStorage: GuestSignatureStorage
|
||||
) {
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@Transactional
|
||||
fun createGuest(
|
||||
@PathVariable propertyId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||
@RequestBody request: GuestCreateRequest
|
||||
): GuestResponse {
|
||||
requireMember(propertyAccess, propertyId, principal)
|
||||
val property = requireProperty(propertyRepo, propertyId)
|
||||
|
||||
val phone = request.phoneE164?.trim()?.takeIf { it.isNotBlank() }
|
||||
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")
|
||||
}
|
||||
val currentGuest = booking.primaryGuest
|
||||
if (currentGuest != null && phone == null) {
|
||||
if (!isPlaceholderGuest(currentGuest)) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Booking already linked to guest")
|
||||
}
|
||||
val updated = applyGuestDetails(currentGuest, request)
|
||||
booking.updatedAt = OffsetDateTime.now()
|
||||
bookingRepo.save(booking)
|
||||
return setOf(updated).toResponse(propertyId, guestVehicleRepo, guestRatingRepo).first()
|
||||
}
|
||||
if (phone != null) {
|
||||
val existing = guestRepo.findByPropertyIdAndPhoneE164(propertyId, phone)
|
||||
if (existing != null) {
|
||||
val previous = booking.primaryGuest
|
||||
booking.primaryGuest = existing
|
||||
booking.updatedAt = OffsetDateTime.now()
|
||||
bookingRepo.save(booking)
|
||||
if (previous != null && previous.id != existing.id && isPlaceholderGuest(previous) && isSafeToDelete(previous)) {
|
||||
guestRepo.delete(previous)
|
||||
}
|
||||
return setOf(existing).toResponse(propertyId, guestVehicleRepo, guestRatingRepo).first()
|
||||
}
|
||||
}
|
||||
|
||||
val now = OffsetDateTime.now()
|
||||
val actor = appUserRepo.findById(principal!!.userId).orElse(null)
|
||||
val guest = Guest(
|
||||
property = property,
|
||||
phoneE164 = phone,
|
||||
name = request.name?.trim()?.ifBlank { null },
|
||||
nationality = request.nationality?.trim()?.ifBlank { null },
|
||||
addressText = request.addressText?.trim()?.ifBlank { null },
|
||||
createdBy = actor,
|
||||
updatedAt = now
|
||||
)
|
||||
val saved = guestRepo.save(guest)
|
||||
booking.primaryGuest = saved
|
||||
booking.updatedAt = now
|
||||
bookingRepo.save(booking)
|
||||
return setOf(saved).toResponse(propertyId, guestVehicleRepo, guestRatingRepo).first()
|
||||
}
|
||||
|
||||
@PutMapping("/{guestId}")
|
||||
fun updateGuest(
|
||||
@PathVariable propertyId: UUID,
|
||||
@@ -242,33 +176,6 @@ class Guests(
|
||||
.body(resource)
|
||||
}
|
||||
|
||||
private fun applyGuestDetails(guest: Guest, request: GuestCreateRequest): Guest {
|
||||
val name = request.name?.trim()?.ifBlank { null }
|
||||
val nationality = request.nationality?.trim()?.ifBlank { null }
|
||||
val address = request.addressText?.trim()?.ifBlank { null }
|
||||
if (name != null) guest.name = name
|
||||
if (nationality != null) guest.nationality = nationality
|
||||
if (address != null) guest.addressText = address
|
||||
guest.updatedAt = OffsetDateTime.now()
|
||||
return guestRepo.save(guest)
|
||||
}
|
||||
|
||||
private fun isPlaceholderGuest(guest: Guest): Boolean {
|
||||
return guest.phoneE164.isNullOrBlank() &&
|
||||
guest.name.isNullOrBlank() &&
|
||||
guest.nationality.isNullOrBlank() &&
|
||||
guest.addressText.isNullOrBlank() &&
|
||||
guest.signaturePath.isNullOrBlank()
|
||||
}
|
||||
|
||||
private fun isSafeToDelete(guest: Guest): Boolean {
|
||||
val id = guest.id ?: return false
|
||||
if (bookingRepo.countByPrimaryGuestId(id) > 0) return false
|
||||
if (guestVehicleRepo.existsByGuestId(id)) return false
|
||||
if (guestDocumentRepo.existsByGuestId(id)) return false
|
||||
if (guestRatingRepo.existsByGuestId(id)) return false
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private fun Set<Guest>.toResponse(
|
||||
|
||||
@@ -50,14 +50,6 @@ data class GuestResponse(
|
||||
val averageScore: Double?
|
||||
)
|
||||
|
||||
data class GuestCreateRequest(
|
||||
val bookingId: UUID,
|
||||
val phoneE164: String? = null,
|
||||
val name: String? = null,
|
||||
val nationality: String? = null,
|
||||
val addressText: String? = null
|
||||
)
|
||||
|
||||
data class GuestUpdateRequest(
|
||||
val phoneE164: String? = null,
|
||||
val name: String? = null,
|
||||
|
||||
Reference in New Issue
Block a user