Skip to content

Commit

Permalink
PI-2304 Fix isNew check on offence code creation (#3943)
Browse files Browse the repository at this point in the history
Now takes into account new offences where we already have a r_detailed_offence record.
  • Loading branch information
marcus-bcl authored Jun 25, 2024
1 parent e170b43 commit d4af1ff
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ class Handler(
return
}

val isNew = mergeDetailedOffence(offence)
if (featureFlags.enabled(FF_CREATE_OFFENCE)) {
mergeReferenceOffence(offence)
}
val newDetailedOffence = mergeDetailedOffence(offence)
val newReferenceOffence = mergeReferenceOffence(offence)
val isNew = newDetailedOffence || newReferenceOffence

telemetryService.trackEvent(if (isNew) "OffenceCodeCreated" else "OffenceCodeUpdated", offence.telemetry)
}
Expand All @@ -63,12 +62,13 @@ class Handler(
return existingEntity == null
}

private fun mergeReferenceOffence(offence: Offence) {
if (offence.homeOfficeCode == null) return
private fun mergeReferenceOffence(offence: Offence): Boolean {
if (!featureFlags.enabled(FF_CREATE_OFFENCE) || offence.homeOfficeCode == null) return false
val existingEntity = offenceRepository.findByCode(offence.homeOfficeCode!!)
if (existingEntity != null && !featureFlags.enabled(FF_UPDATE_OFFENCE)) return
if (existingEntity != null && !featureFlags.enabled(FF_UPDATE_OFFENCE)) return false
val highLevelOffence = offenceRepository.getByCode(offence.highLevelCode!!)
offenceRepository.save(existingEntity.mergeWith(offence.toReferenceOffence(highLevelOffence)))
return existingEntity == null
}

private fun Offence.toDetailedOffence() = DetailedOffence(
Expand Down

0 comments on commit d4af1ff

Please sign in to comment.