Auto-generate property codes and join by code
All checks were successful
build-and-deploy / build-deploy (push) Successful in 40s

This commit is contained in:
androidlover5842
2026-02-01 22:58:25 +05:30
parent 7aca2361ca
commit 5df019ed6e
5 changed files with 19 additions and 9 deletions

View File

@@ -3,7 +3,6 @@ package com.android.trisolarisserver.controller.dto.property
import java.util.UUID import java.util.UUID
data class PropertyCreateRequest( data class PropertyCreateRequest(
val code: String,
val name: String, val name: String,
val addressText: String? = null, val addressText: String? = null,
val timezone: String? = null, val timezone: String? = null,

View File

@@ -33,6 +33,6 @@ data class PropertyAccessCodeResponse(
) )
data class PropertyAccessCodeJoinRequest( data class PropertyAccessCodeJoinRequest(
val propertyId: UUID, val propertyCode: String,
val code: String val code: String
) )

View File

@@ -47,12 +47,10 @@ class Properties(
@RequestBody request: PropertyCreateRequest @RequestBody request: PropertyCreateRequest
): PropertyResponse { ): PropertyResponse {
val user = requireUser(appUserRepo, principal) val user = requireUser(appUserRepo, principal)
if (propertyRepo.existsByCode(request.code)) { val code = generatePropertyCode()
throw ResponseStatusException(HttpStatus.CONFLICT, "Property code already exists")
}
val property = Property( val property = Property(
code = request.code, code = code,
name = request.name, name = request.name,
addressText = request.addressText, addressText = request.addressText,
timezone = request.timezone ?: "Asia/Kolkata", 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 { private fun rankForUser(isSuperAdmin: Boolean, roles: Set<Role>): Int {
if (isSuperAdmin) return 500 if (isSuperAdmin) return 500
return roles.maxOfOrNull { roleRank(it) } ?: 0 return roles.maxOfOrNull { roleRank(it) } ?: 0

View File

@@ -97,10 +97,12 @@ class PropertyAccessCodes(
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Invalid code") throw ResponseStatusException(HttpStatus.NOT_FOUND, "Invalid code")
} }
val now = OffsetDateTime.now() 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") ?: 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)) { if (propertyUserRepo.existsById(membershipId)) {
throw ResponseStatusException(HttpStatus.CONFLICT, "User already a member") throw ResponseStatusException(HttpStatus.CONFLICT, "User already a member")
} }
@@ -111,7 +113,7 @@ class PropertyAccessCodes(
val propertyUser = PropertyUser( val propertyUser = PropertyUser(
id = membershipId, id = membershipId,
property = accessCode.property, property = property,
user = user, user = user,
roles = accessCode.roles.toMutableSet() roles = accessCode.roles.toMutableSet()
) )

View File

@@ -7,4 +7,5 @@ import java.util.UUID
interface PropertyRepo : JpaRepository<Property, UUID> { interface PropertyRepo : JpaRepository<Property, UUID> {
fun existsByCode(code: String): Boolean fun existsByCode(code: String): Boolean
fun existsByCodeAndIdNot(code: String, id: UUID): Boolean fun existsByCodeAndIdNot(code: String, id: UUID): Boolean
fun findByCode(code: String): Property?
} }