diff --git a/src/main/kotlin/com/android/trisolarisserver/TrisolarisServerApplication.kt b/src/main/kotlin/com/android/trisolarisserver/TrisolarisServerApplication.kt index daf0994..bda2be6 100644 --- a/src/main/kotlin/com/android/trisolarisserver/TrisolarisServerApplication.kt +++ b/src/main/kotlin/com/android/trisolarisserver/TrisolarisServerApplication.kt @@ -3,11 +3,13 @@ package com.android.trisolarisserver import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.scheduling.annotation.EnableScheduling +import java.util.TimeZone @SpringBootApplication @EnableScheduling class TrisolarisServerApplication fun main(args: Array) { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata")) runApplication(*args) } diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/RatePlans.kt b/src/main/kotlin/com/android/trisolarisserver/controller/RatePlans.kt index b73e460..74632aa 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/RatePlans.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/RatePlans.kt @@ -2,7 +2,7 @@ package com.android.trisolarisserver.controller import com.android.trisolarisserver.component.PropertyAccess import com.android.trisolarisserver.controller.dto.RateCalendarResponse -import com.android.trisolarisserver.controller.dto.RateCalendarUpsertRequest +import com.android.trisolarisserver.controller.dto.RateCalendarRangeUpsertRequest import com.android.trisolarisserver.controller.dto.RatePlanCreateRequest import com.android.trisolarisserver.controller.dto.RatePlanResponse import com.android.trisolarisserver.controller.dto.RatePlanUpdateRequest @@ -126,25 +126,36 @@ class RatePlans( @PathVariable propertyId: UUID, @PathVariable ratePlanId: UUID, @AuthenticationPrincipal principal: MyPrincipal?, - @RequestBody requests: List + @RequestBody request: RateCalendarRangeUpsertRequest ): List { requireRole(propertyAccess, propertyId, principal, Role.ADMIN, Role.MANAGER) val plan = ratePlanRepo.findByIdAndPropertyId(ratePlanId, propertyId) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Rate plan not found") - if (requests.isEmpty()) return emptyList() + val fromDate = parseDate(request.from) + val toDate = parseDate(request.to) + if (toDate.isBefore(fromDate)) { + throw ResponseStatusException(HttpStatus.BAD_REQUEST, "to must be on/after from") + } + val existing = rateCalendarRepo.findByRatePlanIdAndRateDateBetweenOrderByRateDateAsc( + plan.id!!, + fromDate, + toDate + ).associateBy { it.rateDate } - val updates = requests.map { req -> - val date = parseDate(req.rateDate) - val existing = rateCalendarRepo.findByRatePlanIdAndRateDate(plan.id!!, date) - if (existing != null) { - existing.rate = req.rate - existing + val updates = mutableListOf() + for (date in datesBetween(fromDate, toDate)) { + val row = existing[date] + if (row != null) { + row.rate = request.rate + updates.add(row) } else { - RateCalendar( - property = plan.property, - ratePlan = plan, - rateDate = date, - rate = req.rate + updates.add( + RateCalendar( + property = plan.property, + ratePlan = plan, + rateDate = date, + rate = request.rate + ) ) } } @@ -192,6 +203,13 @@ class RatePlans( throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date") } } + + private fun datesBetween(from: LocalDate, to: LocalDate): Sequence { + return generateSequence(from) { current -> + val next = current.plusDays(1) + if (next.isAfter(to)) null else next + } + } } private fun RatePlan.toResponse(): RatePlanResponse { diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/dto/RateDtos.kt b/src/main/kotlin/com/android/trisolarisserver/controller/dto/RateDtos.kt index 2afb8eb..61750c9 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/dto/RateDtos.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/dto/RateDtos.kt @@ -28,8 +28,9 @@ data class RatePlanResponse( val currency: String ) -data class RateCalendarUpsertRequest( - val rateDate: String, +data class RateCalendarRangeUpsertRequest( + val from: String, + val to: String, val rate: Long )