Add admin cash payment delete endpoint
All checks were successful
build-and-deploy / build-deploy (push) Successful in 40s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 40s
This commit is contained in:
@@ -14,6 +14,7 @@ import com.android.trisolarisserver.security.MyPrincipal
|
|||||||
import org.springframework.http.HttpStatus
|
import org.springframework.http.HttpStatus
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping
|
||||||
import org.springframework.web.bind.annotation.PathVariable
|
import org.springframework.web.bind.annotation.PathVariable
|
||||||
import org.springframework.web.bind.annotation.PostMapping
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
import org.springframework.web.bind.annotation.RequestBody
|
import org.springframework.web.bind.annotation.RequestBody
|
||||||
@@ -89,6 +90,33 @@ class Payments(
|
|||||||
return paymentRepo.findByBookingIdOrderByReceivedAtDesc(bookingId).map { it.toResponse() }
|
return paymentRepo.findByBookingIdOrderByReceivedAtDesc(bookingId).map { it.toResponse() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{paymentId}")
|
||||||
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||||
|
fun delete(
|
||||||
|
@PathVariable propertyId: UUID,
|
||||||
|
@PathVariable bookingId: UUID,
|
||||||
|
@PathVariable paymentId: UUID,
|
||||||
|
@AuthenticationPrincipal principal: MyPrincipal?
|
||||||
|
) {
|
||||||
|
requireRole(propertyAccess, propertyId, principal, Role.ADMIN)
|
||||||
|
val booking = bookingRepo.findById(bookingId).orElseThrow {
|
||||||
|
ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found")
|
||||||
|
}
|
||||||
|
if (booking.property.id != propertyId) {
|
||||||
|
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Booking not found for property")
|
||||||
|
}
|
||||||
|
val payment = paymentRepo.findById(paymentId).orElseThrow {
|
||||||
|
ResponseStatusException(HttpStatus.NOT_FOUND, "Payment not found")
|
||||||
|
}
|
||||||
|
if (payment.booking.id != bookingId || payment.property.id != propertyId) {
|
||||||
|
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Payment not found for booking")
|
||||||
|
}
|
||||||
|
if (payment.method != PaymentMethod.CASH) {
|
||||||
|
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Only CASH payments can be deleted")
|
||||||
|
}
|
||||||
|
paymentRepo.delete(payment)
|
||||||
|
}
|
||||||
|
|
||||||
private fun parseMethod(value: String): PaymentMethod {
|
private fun parseMethod(value: String): PaymentMethod {
|
||||||
return try {
|
return try {
|
||||||
PaymentMethod.valueOf(value.trim())
|
PaymentMethod.valueOf(value.trim())
|
||||||
|
|||||||
Reference in New Issue
Block a user