diff --git a/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/repository/PrisonStaffRepository.kt b/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/repository/PrisonStaffRepository.kt index 54ed1d971..2630e49d6 100644 --- a/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/repository/PrisonStaffRepository.kt +++ b/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/repository/PrisonStaffRepository.kt @@ -15,7 +15,10 @@ interface PrisonStaffRepository : JpaRepository { @Query( """ select officer_code from staff - where regexp_like(officer_code, ?1, 'i') + where officer_code like :regex || '%' + and substr(officer_code,5,1) in ('0','1','2','3','4','5','6','7','8','9') + and substr(officer_code,6,1) in ('0','1','2','3','4','5','6','7','8','9') + and substr(officer_code,7,1) in ('0','1','2','3','4','5','6','7','8','9') order by officer_code desc fetch next 1 rows only """, diff --git a/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGenerator.kt b/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGenerator.kt index 8706bd82a..247e61558 100644 --- a/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGenerator.kt +++ b/libs/prison-staff/src/main/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGenerator.kt @@ -13,7 +13,7 @@ class OfficerCodeGenerator(private val staffRepository: PrisonStaffRepository) { throw StaffCodeExhaustedException(probationAreaCode) } val prefix = probationAreaCode.substring(0, 3) + alphabet[index] - val latest = staffRepository.getLatestStaffReference("^$prefix\\d{3}$") + val latest = staffRepository.getLatestStaffReference(prefix) val number = latest?.substring(latest.length - 3)?.toInt()?.plus(1) ?: 1 return if (number > 999) { generateFor(probationAreaCode, index + 1) diff --git a/libs/prison-staff/src/test/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGeneratorTest.kt b/libs/prison-staff/src/test/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGeneratorTest.kt index e89d1014a..b92995178 100644 --- a/libs/prison-staff/src/test/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGeneratorTest.kt +++ b/libs/prison-staff/src/test/kotlin/uk/gov/justice/digital/hmpps/service/OfficerCodeGeneratorTest.kt @@ -29,8 +29,7 @@ class OfficerCodeGeneratorTest { fun `if all possible options exhausted exception thrown`() { whenever(staffRepository.getLatestStaffReference(anyString())) .thenAnswer { - val regex = it.arguments[0] as String - val prefix = regex.substring(1, regex.length - 6) + val prefix = it.arguments[0] as String prefix + "999" } @@ -54,8 +53,7 @@ class OfficerCodeGeneratorTest { fun `roll over to next letter is successful`() { whenever(staffRepository.getLatestStaffReference(anyString())) .thenAnswer { - val regex = it.arguments[0] as String - val prefix = regex.substring(1, regex.length - 6) + val prefix = it.arguments[0] as String if (prefix == "${probationAreaCode}A") { prefix + "999" } else {