Skip to content

Commit

Permalink
update staff code finder to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Jan 21, 2025
1 parent eb53758 commit 3c0d555
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ interface PrisonStaffRepository : JpaRepository<PrisonStaff, Long> {
@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
""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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 {
Expand Down

0 comments on commit 3c0d555

Please sign in to comment.