Skip to content

Commit

Permalink
PI-1958 Only send domain event if identifiers have changed (#3531)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Mar 25, 2024
1 parent 5e747e8 commit 239d042
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ class MatchWriter(
private val orderManagerRepository: OrderManagerRepository,
) {
@Transactional
fun update(prisonIdentifiers: PrisonIdentifiers, person: Person, custody: Custody? = null) {
if (person.nomsNumber != prisonIdentifiers.prisonerNumber) {
fun update(prisonIdentifiers: PrisonIdentifiers, person: Person, custody: Custody? = null): Boolean {
val nomsNumberChanged = person.nomsNumber != prisonIdentifiers.prisonerNumber
if (nomsNumberChanged) {
removeDuplicateNomsNumbers(person, prisonIdentifiers.prisonerNumber)
updateNomsNumber(person, prisonIdentifiers.prisonerNumber)
}
if (custody != null && custody.prisonerNumber != prisonIdentifiers.prisonerNumber) {
val bookingNumberChanged = custody?.prisonerNumber != prisonIdentifiers.bookingNumber
if (bookingNumberChanged && custody != null) {
custody.prisonerNumber = prisonIdentifiers.bookingNumber
custodyRepository.save(custody)
person.mostRecentPrisonerNumber = prisonIdentifiers.bookingNumber
personRepository.save(person)
person.rebuildPrisonerLinks()
custody.createContactForChange()
}
return nomsNumberChanged || bookingNumberChanged
}

private fun removeDuplicateNomsNumbers(person: Person, nomsNumber: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class PrisonMatchingService(
val matchResult = findMatchingPrisonRecord(crn)
if (!dryRun && matchResult is Success) {
with(matchResult) {
matchWriter.update(prisonIdentifiers, person, custody)
notifier.identifierAdded(crn, prisonIdentifiers)
val changes = matchWriter.update(prisonIdentifiers, person, custody)
if (changes) notifier.identifierAdded(person.crn, prisonIdentifiers)
}
}
return matchResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ProbationMatchingService(
val matchResult = findMatchingProbationRecord(nomsNumber)
if (!dryRun && matchResult is Success) {
with(matchResult) {
matchWriter.update(prisonIdentifiers, person, custody)
notifier.identifierAdded(person.crn, prisonIdentifiers)
val changes = matchWriter.update(prisonIdentifiers, person, custody)
if (changes) notifier.identifierAdded(person.crn, prisonIdentifiers)
}
}
return matchResult
Expand All @@ -43,8 +43,8 @@ class ProbationMatchingService(
}
if (!dryRun) {
existing.forEach {
matchWriter.update(PrisonIdentifiers(newNomsNumber), it)
notifier.identifierUpdated(it.crn, newNomsNumber, oldNomsNumber)
val changes = matchWriter.update(PrisonIdentifiers(newNomsNumber), it)
if (changes) notifier.identifierUpdated(it.crn, newNomsNumber, oldNomsNumber)
}
}
return MergeResult.Success(
Expand Down

0 comments on commit 239d042

Please sign in to comment.