Allow access code join by property code or id
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
This commit is contained in:
@@ -33,6 +33,7 @@ data class PropertyAccessCodeResponse(
|
||||
)
|
||||
|
||||
data class PropertyAccessCodeJoinRequest(
|
||||
val propertyCode: String,
|
||||
val propertyCode: String? = null,
|
||||
val propertyId: String? = null,
|
||||
val code: String
|
||||
)
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.android.trisolarisserver.controller.dto.property.PropertyAccessCodeJo
|
||||
import com.android.trisolarisserver.controller.dto.property.PropertyAccessCodeResponse
|
||||
import com.android.trisolarisserver.controller.dto.property.PropertyUserResponse
|
||||
import com.android.trisolarisserver.models.property.PropertyAccessCode
|
||||
import com.android.trisolarisserver.models.property.Property
|
||||
import com.android.trisolarisserver.models.property.PropertyUser
|
||||
import com.android.trisolarisserver.models.property.PropertyUserId
|
||||
import com.android.trisolarisserver.models.property.Role
|
||||
@@ -97,8 +98,7 @@ class PropertyAccessCodes(
|
||||
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Invalid code")
|
||||
}
|
||||
val now = OffsetDateTime.now()
|
||||
val property = propertyRepo.findByCode(request.propertyCode.trim())
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
val property = resolveProperty(request)
|
||||
val accessCode = accessCodeRepo.findActiveByPropertyAndCode(property.id!!, code, now)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Invalid code")
|
||||
|
||||
@@ -130,6 +130,27 @@ class PropertyAccessCodes(
|
||||
)
|
||||
}
|
||||
|
||||
private fun resolveProperty(request: PropertyAccessCodeJoinRequest): Property {
|
||||
val code = request.propertyCode?.trim().orEmpty()
|
||||
if (code.isNotBlank()) {
|
||||
return propertyRepo.findByCode(code)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
val rawId = request.propertyId?.trim().orEmpty()
|
||||
if (rawId.isBlank()) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Property code required")
|
||||
}
|
||||
val asUuid = runCatching { UUID.fromString(rawId) }.getOrNull()
|
||||
return if (asUuid != null) {
|
||||
propertyRepo.findById(asUuid).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
} else {
|
||||
propertyRepo.findByCode(rawId)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseRoles(input: Set<String>): Set<Role> {
|
||||
return try {
|
||||
input.map { Role.valueOf(it) }.toSet()
|
||||
|
||||
Reference in New Issue
Block a user