diff --git a/src/main/kotlin/com/android/trisolarisserver/config/IssuedCardSchemaFix.kt b/src/main/kotlin/com/android/trisolarisserver/config/IssuedCardSchemaFix.kt index 34979f2..a041557 100644 --- a/src/main/kotlin/com/android/trisolarisserver/config/IssuedCardSchemaFix.kt +++ b/src/main/kotlin/com/android/trisolarisserver/config/IssuedCardSchemaFix.kt @@ -46,27 +46,9 @@ class IssuedCardSchemaFix( Int::class.java ) ?: 0 - if (uniqueIndexExists == 0) { - logger.info("Removing duplicate issued_card rows by property_id + card_id") - jdbcTemplate.execute( - """ - with ranked as ( - select id, - row_number() over ( - partition by property_id, lower(card_id) - order by issued_at desc, id desc - ) as rn - from issued_card - where card_id is not null - ) - delete from issued_card - where id in (select id from ranked where rn > 1) - """.trimIndent() - ) - logger.info("Creating unique index on issued_card(property_id, lower(card_id))") - jdbcTemplate.execute( - "create unique index if not exists idx_issued_card_property_card_id_unique on issued_card (property_id, lower(card_id))" - ) + if (uniqueIndexExists > 0) { + logger.info("Dropping unique index on issued_card(property_id, lower(card_id))") + jdbcTemplate.execute("drop index if exists idx_issued_card_property_card_id_unique") } } } diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/IssuedCards.kt b/src/main/kotlin/com/android/trisolarisserver/controller/IssuedCards.kt index 0e8dbb6..c6ffe07 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/IssuedCards.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/IssuedCards.kt @@ -150,14 +150,14 @@ class IssuedCards( .map { it.toResponse() } } - @PostMapping("/cards/{cardId}/revoke") + @PostMapping("/cards/{cardIndex}/revoke") fun revoke( @PathVariable propertyId: UUID, - @PathVariable cardId: String, + @PathVariable cardIndex: Int, @AuthenticationPrincipal principal: MyPrincipal? ): CardRevokeResponse { requireRevokeActor(propertyId, principal) - val card = findCardForRevoke(cardId, propertyId) + val card = issuedCardRepo.findByPropertyIdAndCardIndex(propertyId, cardIndex) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Card not found") if (card.revokedAt == null) { val now = nowForProperty(card.property.timezone) @@ -238,21 +238,6 @@ class IssuedCards( propertyAccess.requireAnyRole(propertyId, principal.userId, Role.ADMIN) } - private fun findCardForRevoke(cardId: String, propertyId: UUID): IssuedCard? { - val trimmed = cardId.trim() - if (trimmed.isBlank()) return null - val uuid = try { - java.util.UUID.fromString(trimmed) - } catch (_: Exception) { - null - } - return if (uuid != null) { - issuedCardRepo.findByIdAndPropertyId(uuid, propertyId) - } else { - issuedCardRepo.findAllByCardIdIgnoreCaseAndPropertyId(trimmed, propertyId) - .maxByOrNull { it.issuedAt } - } - } private fun nextCardIndex(propertyId: UUID): Int { var counter = counterRepo.findByPropertyIdForUpdate(propertyId) diff --git a/src/main/kotlin/com/android/trisolarisserver/repo/IssuedCardRepo.kt b/src/main/kotlin/com/android/trisolarisserver/repo/IssuedCardRepo.kt index 7fc3c25..be64f02 100644 --- a/src/main/kotlin/com/android/trisolarisserver/repo/IssuedCardRepo.kt +++ b/src/main/kotlin/com/android/trisolarisserver/repo/IssuedCardRepo.kt @@ -9,6 +9,7 @@ interface IssuedCardRepo : JpaRepository { fun findByIdAndPropertyId(id: UUID, propertyId: UUID): IssuedCard? fun findByCardIdIgnoreCaseAndPropertyId(cardId: String, propertyId: UUID): IssuedCard? fun findAllByCardIdIgnoreCaseAndPropertyId(cardId: String, propertyId: UUID): List + fun findByPropertyIdAndCardIndex(propertyId: UUID, cardIndex: Int): IssuedCard? @org.springframework.data.jpa.repository.Query(""" select case when count(c) > 0 then true else false end