From 242f8be3ea04ea418afda02e8aaa6d68f5464798 Mon Sep 17 00:00:00 2001 From: Anthony Britton <105213050+anthony-britton-moj@users.noreply.github.com> Date: Mon, 11 Mar 2024 16:55:48 +0000 Subject: [PATCH] PI-1957 - additions (#3456) * PI-1957 - use send to avoid oom * PI-1957 - add lenient variation to dates as `partial` match --- .../hmpps/publisher/AwsQueuePublisher.kt | 2 +- .../digital/hmpps/sevice/model/DateMatcher.kt | 35 +++++++++++++++++++ .../digital/hmpps/sevice/model/Matches.kt | 15 +++----- 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/DateMatcher.kt diff --git a/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/publisher/AwsQueuePublisher.kt b/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/publisher/AwsQueuePublisher.kt index 935d5872d0..6ed58b66ba 100644 --- a/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/publisher/AwsQueuePublisher.kt +++ b/libs/messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/publisher/AwsQueuePublisher.kt @@ -21,7 +21,7 @@ class AwsQueuePublisher( ) : NotificationPublisher { override fun publish(notification: Notification<*>) { notification.message?.let { message -> - sqsTemplate.sendAsync( + sqsTemplate.send( queue, MessageBuilder.createMessage( objectMapper.writeValueAsString( Notification( diff --git a/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/DateMatcher.kt b/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/DateMatcher.kt new file mode 100644 index 0000000000..a924abcdaf --- /dev/null +++ b/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/DateMatcher.kt @@ -0,0 +1,35 @@ +package uk.gov.justice.digital.hmpps.sevice.model + +import java.time.DateTimeException +import java.time.LocalDate +import java.time.temporal.ChronoUnit +import kotlin.math.abs + +data object DateMatcher { + + fun variations(date: LocalDate): List = buildList { + swapMonthDay(date)?.also(::add) + addAll(aroundDateInSameMonth(date)) + addAll(everyOtherValidMonth(date)) + } + + private fun aroundDateInSameMonth(date: LocalDate) = + listOf(date.minusDays(1), date.minusDays(-1), date).filter { it.month == date.month } + + private fun everyOtherValidMonth(date: LocalDate): List = + (1..12).filterNot { date.monthValue == it }.mapNotNull { setMonthDay(date, it) } + + private fun swapMonthDay(date: LocalDate): LocalDate? = try { + LocalDate.of(date.year, date.dayOfMonth, date.monthValue) + } catch (e: DateTimeException) { + null + } + + private fun setMonthDay(date: LocalDate, monthValue: Int): LocalDate? = try { + LocalDate.of(date.year, monthValue, date.dayOfMonth) + } catch (e: DateTimeException) { + null + } +} + +fun LocalDate.withinDays(date: LocalDate, days: Int = 7): Boolean = abs(ChronoUnit.DAYS.between(this, date)) <= days diff --git a/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/Matches.kt b/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/Matches.kt index 77ab4f7e7d..c089ed6ede 100644 --- a/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/Matches.kt +++ b/projects/prison-identifier-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/sevice/model/Matches.kt @@ -2,9 +2,6 @@ package uk.gov.justice.digital.hmpps.sevice.model import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person import uk.gov.justice.digital.hmpps.integrations.prison.PrisonSearchResult -import java.time.LocalDate -import java.time.temporal.ChronoUnit -import kotlin.math.abs data class PersonMatch( val person: Person, @@ -26,9 +23,9 @@ data class PersonMatch( private fun nameMatch(prisoner: PrisonSearchResult) = ComponentMatch.Name(nameMatchType(prisoner)) private fun dobMatch(prisoner: PrisonSearchResult) = ComponentMatch.DateOfBirth( - when { - prisoner.dateOfBirth == person.dateOfBirth -> ComponentMatch.MatchType.MATCH - // TODO add logic from p2p update around date matching + when (prisoner.dateOfBirth) { + person.dateOfBirth -> ComponentMatch.MatchType.MATCH + in DateMatcher.variations(person.dateOfBirth) -> ComponentMatch.MatchType.PARTIAL else -> ComponentMatch.MatchType.INCONCLUSIVE } ) @@ -46,7 +43,7 @@ data class PersonMatch( private fun exclusiveField(first: String?, second: String?): Boolean = (first == null && second != null) || (first != null && second == null) - fun sentenceDateMatch(prisoner: PrisonSearchResult) = ComponentMatch.SentenceDate( + private fun sentenceDateMatch(prisoner: PrisonSearchResult) = ComponentMatch.SentenceDate( when { person.isSentenced() && (prisoner.sentenceStartDate != null && person.sentenceDates() .any { it.withinDays(prisoner.sentenceStartDate) }) -> ComponentMatch.MatchType.MATCH @@ -90,6 +87,4 @@ sealed interface ComponentMatch { } } -data class PotentialMatch(val prisoner: PrisonSearchResult, val matches: List) - -fun LocalDate.withinDays(date: LocalDate, days: Int = 7): Boolean = abs(ChronoUnit.DAYS.between(this, date)) <= days \ No newline at end of file +data class PotentialMatch(val prisoner: PrisonSearchResult, val matches: List) \ No newline at end of file