Auto-generate property codes and join by code
All checks were successful
build-and-deploy / build-deploy (push) Successful in 40s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 40s
This commit is contained in:
@@ -3,7 +3,6 @@ package com.android.trisolarisserver.controller.dto.property
|
||||
import java.util.UUID
|
||||
|
||||
data class PropertyCreateRequest(
|
||||
val code: String,
|
||||
val name: String,
|
||||
val addressText: String? = null,
|
||||
val timezone: String? = null,
|
||||
|
||||
@@ -33,6 +33,6 @@ data class PropertyAccessCodeResponse(
|
||||
)
|
||||
|
||||
data class PropertyAccessCodeJoinRequest(
|
||||
val propertyId: UUID,
|
||||
val propertyCode: String,
|
||||
val code: String
|
||||
)
|
||||
|
||||
@@ -47,12 +47,10 @@ class Properties(
|
||||
@RequestBody request: PropertyCreateRequest
|
||||
): PropertyResponse {
|
||||
val user = requireUser(appUserRepo, principal)
|
||||
if (propertyRepo.existsByCode(request.code)) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Property code already exists")
|
||||
}
|
||||
val code = generatePropertyCode()
|
||||
|
||||
val property = Property(
|
||||
code = request.code,
|
||||
code = code,
|
||||
name = request.name,
|
||||
addressText = request.addressText,
|
||||
timezone = request.timezone ?: "Asia/Kolkata",
|
||||
@@ -278,6 +276,16 @@ class Properties(
|
||||
}
|
||||
}
|
||||
|
||||
private fun generatePropertyCode(): String {
|
||||
repeat(10) {
|
||||
val code = "HOTE" + (100..999).random()
|
||||
if (!propertyRepo.existsByCode(code)) {
|
||||
return code
|
||||
}
|
||||
}
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Unable to generate property code")
|
||||
}
|
||||
|
||||
private fun rankForUser(isSuperAdmin: Boolean, roles: Set<Role>): Int {
|
||||
if (isSuperAdmin) return 500
|
||||
return roles.maxOfOrNull { roleRank(it) } ?: 0
|
||||
|
||||
@@ -97,10 +97,12 @@ class PropertyAccessCodes(
|
||||
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Invalid code")
|
||||
}
|
||||
val now = OffsetDateTime.now()
|
||||
val accessCode = accessCodeRepo.findActiveByPropertyAndCode(request.propertyId, code, now)
|
||||
val property = propertyRepo.findByCode(request.propertyCode.trim())
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
val accessCode = accessCodeRepo.findActiveByPropertyAndCode(property.id!!, code, now)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Invalid code")
|
||||
|
||||
val membershipId = PropertyUserId(propertyId = request.propertyId, userId = resolved.userId)
|
||||
val membershipId = PropertyUserId(propertyId = property.id, userId = resolved.userId)
|
||||
if (propertyUserRepo.existsById(membershipId)) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "User already a member")
|
||||
}
|
||||
@@ -111,7 +113,7 @@ class PropertyAccessCodes(
|
||||
|
||||
val propertyUser = PropertyUser(
|
||||
id = membershipId,
|
||||
property = accessCode.property,
|
||||
property = property,
|
||||
user = user,
|
||||
roles = accessCode.roles.toMutableSet()
|
||||
)
|
||||
|
||||
@@ -7,4 +7,5 @@ import java.util.UUID
|
||||
interface PropertyRepo : JpaRepository<Property, UUID> {
|
||||
fun existsByCode(code: String): Boolean
|
||||
fun existsByCodeAndIdNot(code: String, id: UUID): Boolean
|
||||
fun findByCode(code: String): Property?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user