Allow rate plan code per room type
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s

This commit is contained in:
androidlover5842
2026-01-29 05:35:19 +05:30
parent ac9df5f4bd
commit 3546b2de62
4 changed files with 47 additions and 4 deletions

View File

@@ -0,0 +1,42 @@
package com.android.trisolarisserver.config
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
@Component
class RatePlanSchemaFix(
private val jdbcTemplate: JdbcTemplate
) : PostgresSchemaFix(jdbcTemplate) {
override fun runPostgres(jdbcTemplate: JdbcTemplate) {
val constraints = jdbcTemplate.query(
"""
select tc.constraint_name,
array_agg(kcu.column_name order by kcu.ordinal_position) as cols
from information_schema.table_constraints tc
join information_schema.key_column_usage kcu
on tc.constraint_name = kcu.constraint_name
and tc.table_schema = kcu.table_schema
where tc.table_name = 'rate_plan'
and tc.constraint_type = 'UNIQUE'
group by tc.constraint_name
""".trimIndent()
) { rs, _ ->
rs.getString("constraint_name") to (rs.getArray("cols").array as Array<*>).map { it.toString() }
}
val oldConstraint = constraints.firstOrNull { it.second == listOf("property_id", "code") }
if (oldConstraint != null) {
logger.info("Dropping old unique constraint on rate_plan(property_id, code)")
jdbcTemplate.execute("alter table rate_plan drop constraint if exists ${oldConstraint.first}")
}
val hasNew = constraints.any { it.second == listOf("property_id", "room_type_id", "code") }
if (!hasNew) {
logger.info("Adding unique constraint on rate_plan(property_id, room_type_id, code)")
jdbcTemplate.execute(
"alter table rate_plan add constraint rate_plan_property_roomtype_code_key unique (property_id, room_type_id, code)"
)
}
}
}

View File

@@ -50,14 +50,14 @@ class RatePlans(
@RequestBody request: RatePlanCreateRequest
): RatePlanResponse {
requireRole(propertyAccess, propertyId, principal, Role.ADMIN, Role.MANAGER)
if (ratePlanRepo.existsByPropertyIdAndCode(propertyId, request.code.trim())) {
throw ResponseStatusException(HttpStatus.CONFLICT, "Rate plan code already exists")
}
val property = propertyRepo.findById(propertyId).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
}
val roomType = roomTypeRepo.findByPropertyIdAndCodeIgnoreCase(propertyId, request.roomTypeCode)
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room type not found")
if (ratePlanRepo.existsByPropertyIdAndRoomTypeIdAndCode(propertyId, roomType.id!!, request.code.trim())) {
throw ResponseStatusException(HttpStatus.CONFLICT, "Rate plan code already exists for room type")
}
val plan = RatePlan(
property = property,

View File

@@ -9,7 +9,7 @@ import java.util.UUID
@Table(
name = "rate_plan",
uniqueConstraints = [
UniqueConstraint(columnNames = ["property_id", "code"])
UniqueConstraint(columnNames = ["property_id", "room_type_id", "code"])
]
)
class RatePlan(

View File

@@ -10,4 +10,5 @@ interface RatePlanRepo : JpaRepository<RatePlan, UUID> {
fun findByPropertyIdOrderByCode(propertyId: UUID): List<RatePlan>
fun findByIdAndPropertyId(id: UUID, propertyId: UUID): RatePlan?
fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean
fun existsByPropertyIdAndRoomTypeIdAndCode(propertyId: UUID, roomTypeId: UUID, code: String): Boolean
}