diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/OrgPropertyDtos.kt b/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/OrgPropertyDtos.kt index b93137d..1bd074c 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/OrgPropertyDtos.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/OrgPropertyDtos.kt @@ -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, diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/UserDirectoryDtos.kt b/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/UserDirectoryDtos.kt index b699614..c8e262c 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/UserDirectoryDtos.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/dto/property/UserDirectoryDtos.kt @@ -33,6 +33,6 @@ data class PropertyAccessCodeResponse( ) data class PropertyAccessCodeJoinRequest( - val propertyId: UUID, + val propertyCode: String, val code: String ) diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/property/Properties.kt b/src/main/kotlin/com/android/trisolarisserver/controller/property/Properties.kt index 3f2f6ef..4705799 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/property/Properties.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/property/Properties.kt @@ -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): Int { if (isSuperAdmin) return 500 return roles.maxOfOrNull { roleRank(it) } ?: 0 diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/property/PropertyAccessCodes.kt b/src/main/kotlin/com/android/trisolarisserver/controller/property/PropertyAccessCodes.kt index b4bc144..d56f477 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/property/PropertyAccessCodes.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/property/PropertyAccessCodes.kt @@ -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() ) diff --git a/src/main/kotlin/com/android/trisolarisserver/repo/property/PropertyRepo.kt b/src/main/kotlin/com/android/trisolarisserver/repo/property/PropertyRepo.kt index 442aa88..3b4d5e2 100644 --- a/src/main/kotlin/com/android/trisolarisserver/repo/property/PropertyRepo.kt +++ b/src/main/kotlin/com/android/trisolarisserver/repo/property/PropertyRepo.kt @@ -7,4 +7,5 @@ import java.util.UUID interface PropertyRepo : JpaRepository { fun existsByCode(code: String): Boolean fun existsByCodeAndIdNot(code: String, id: UUID): Boolean + fun findByCode(code: String): Property? }