TariffService.kt

package fr.codetics.ev_server.services.billing

import fr.codetics.ev_server.core.Database
import fr.codetics.ev_server.core.ensure
import fr.codetics.ev_server.core.invalid
import fr.codetics.ev_server.core.msg
import fr.codetics.ev_server.models.billing.ChargingTariff
import fr.codetics.ev_server.models.entities.RecordKey
import fr.codetics.ev_server.repositories.RecordRepository
import java.math.BigDecimal
import java.util.UUID
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

@Service
class TariffService(private val records: RecordRepository, private val db: Database) {
    fun find(tenantId: UUID, id: UUID): ChargingTariff {
        val record =
            records.findById(RecordKey(tenantId, "pricing-definitions", id)).orElse(null)
                ?: throw ResponseStatusException(
                    HttpStatus.NOT_FOUND,
                    msg("tariff.not_in_workspace"),
                )
        val data = db.decode(record.data)
        fun amount(field: String): BigDecimal {
            val value = data.path(field).asText("0")
            ensure(value.length <= 30) { msg("tariff.invalid_amount") }
            val decimal =
                value.toBigDecimalOrNull()
                    ?: invalid(msg("tariff.invalid_amount"))
            ensure(decimal <= BigDecimal(10000)) { msg("tariff.amount_too_high") }
            return decimal
        }
        return ChargingTariff(
            data.path("currency").asText("EUR"),
            amount("energyPrice"),
            amount("timePrice"),
            amount("flatPrice"),
        )
    }
}