Allow rate plan code per room type
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 35s
This commit is contained in:
@@ -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)"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,14 +50,14 @@ class RatePlans(
|
|||||||
@RequestBody request: RatePlanCreateRequest
|
@RequestBody request: RatePlanCreateRequest
|
||||||
): RatePlanResponse {
|
): RatePlanResponse {
|
||||||
requireRole(propertyAccess, propertyId, principal, Role.ADMIN, Role.MANAGER)
|
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 {
|
val property = propertyRepo.findById(propertyId).orElseThrow {
|
||||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||||
}
|
}
|
||||||
val roomType = roomTypeRepo.findByPropertyIdAndCodeIgnoreCase(propertyId, request.roomTypeCode)
|
val roomType = roomTypeRepo.findByPropertyIdAndCodeIgnoreCase(propertyId, request.roomTypeCode)
|
||||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room type not found")
|
?: 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(
|
val plan = RatePlan(
|
||||||
property = property,
|
property = property,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import java.util.UUID
|
|||||||
@Table(
|
@Table(
|
||||||
name = "rate_plan",
|
name = "rate_plan",
|
||||||
uniqueConstraints = [
|
uniqueConstraints = [
|
||||||
UniqueConstraint(columnNames = ["property_id", "code"])
|
UniqueConstraint(columnNames = ["property_id", "room_type_id", "code"])
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
class RatePlan(
|
class RatePlan(
|
||||||
|
|||||||
@@ -10,4 +10,5 @@ interface RatePlanRepo : JpaRepository<RatePlan, UUID> {
|
|||||||
fun findByPropertyIdOrderByCode(propertyId: UUID): List<RatePlan>
|
fun findByPropertyIdOrderByCode(propertyId: UUID): List<RatePlan>
|
||||||
fun findByIdAndPropertyId(id: UUID, propertyId: UUID): RatePlan?
|
fun findByIdAndPropertyId(id: UUID, propertyId: UUID): RatePlan?
|
||||||
fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean
|
fun existsByPropertyIdAndCode(propertyId: UUID, code: String): Boolean
|
||||||
|
fun existsByPropertyIdAndRoomTypeIdAndCode(propertyId: UUID, roomTypeId: UUID, code: String): Boolean
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user