Return average rate for calendar range
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m34s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m34s
This commit is contained in:
@@ -2,6 +2,7 @@ package com.android.trisolarisserver.controller
|
|||||||
|
|
||||||
import com.android.trisolarisserver.component.PropertyAccess
|
import com.android.trisolarisserver.component.PropertyAccess
|
||||||
import com.android.trisolarisserver.controller.dto.RateCalendarResponse
|
import com.android.trisolarisserver.controller.dto.RateCalendarResponse
|
||||||
|
import com.android.trisolarisserver.controller.dto.RateCalendarAverageResponse
|
||||||
import com.android.trisolarisserver.controller.dto.RateCalendarRangeUpsertRequest
|
import com.android.trisolarisserver.controller.dto.RateCalendarRangeUpsertRequest
|
||||||
import com.android.trisolarisserver.controller.dto.RatePlanCreateRequest
|
import com.android.trisolarisserver.controller.dto.RatePlanCreateRequest
|
||||||
import com.android.trisolarisserver.controller.dto.RatePlanResponse
|
import com.android.trisolarisserver.controller.dto.RatePlanResponse
|
||||||
@@ -169,14 +170,32 @@ class RatePlans(
|
|||||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||||
@RequestParam from: String,
|
@RequestParam from: String,
|
||||||
@RequestParam to: String
|
@RequestParam to: String
|
||||||
): List<RateCalendarResponse> {
|
): RateCalendarAverageResponse {
|
||||||
requireMember(propertyAccess, propertyId, principal)
|
requireMember(propertyAccess, propertyId, principal)
|
||||||
val plan = ratePlanRepo.findByIdAndPropertyId(ratePlanId, propertyId)
|
val plan = ratePlanRepo.findByIdAndPropertyId(ratePlanId, propertyId)
|
||||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Rate plan not found")
|
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Rate plan not found")
|
||||||
val fromDate = parseDate(from)
|
val fromDate = parseDate(from)
|
||||||
val toDate = parseDate(to)
|
val toDate = parseDate(to)
|
||||||
|
if (toDate.isBefore(fromDate)) {
|
||||||
|
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "to must be on/after from")
|
||||||
|
}
|
||||||
val items = rateCalendarRepo.findByRatePlanIdAndRateDateBetweenOrderByRateDateAsc(plan.id!!, fromDate, toDate)
|
val items = rateCalendarRepo.findByRatePlanIdAndRateDateBetweenOrderByRateDateAsc(plan.id!!, fromDate, toDate)
|
||||||
return items.map { it.toResponse() }
|
val overrides = items.associateBy { it.rateDate }
|
||||||
|
var total = 0L
|
||||||
|
var days = 0
|
||||||
|
for (date in datesBetween(fromDate, toDate)) {
|
||||||
|
days += 1
|
||||||
|
total += overrides[date]?.rate ?: plan.baseRate
|
||||||
|
}
|
||||||
|
val average = if (days == 0) 0.0 else total.toDouble() / days
|
||||||
|
return RateCalendarAverageResponse(
|
||||||
|
ratePlanId = plan.id!!,
|
||||||
|
from = fromDate,
|
||||||
|
to = toDate,
|
||||||
|
averageRate = average,
|
||||||
|
days = days,
|
||||||
|
currency = plan.currency
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{ratePlanId}/calendar/{rateDate}")
|
@DeleteMapping("/{ratePlanId}/calendar/{rateDate}")
|
||||||
|
|||||||
@@ -41,6 +41,15 @@ data class RateCalendarResponse(
|
|||||||
val rate: Long
|
val rate: Long
|
||||||
)
|
)
|
||||||
|
|
||||||
|
data class RateCalendarAverageResponse(
|
||||||
|
val ratePlanId: UUID,
|
||||||
|
val from: LocalDate,
|
||||||
|
val to: LocalDate,
|
||||||
|
val averageRate: Double,
|
||||||
|
val days: Int,
|
||||||
|
val currency: String
|
||||||
|
)
|
||||||
|
|
||||||
data class RoomStayRateChangeRequest(
|
data class RoomStayRateChangeRequest(
|
||||||
val effectiveAt: String,
|
val effectiveAt: String,
|
||||||
val nightlyRate: Long,
|
val nightlyRate: Long,
|
||||||
|
|||||||
Reference in New Issue
Block a user