Skip to content

Commit

Permalink
PI-1957 - additions (#3456)
Browse files Browse the repository at this point in the history
* PI-1957 - use send to avoid oom

* PI-1957 - add lenient variation to dates as `partial` match
  • Loading branch information
anthony-britton-moj authored Mar 11, 2024
1 parent d8b11e3 commit 242f8be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LocalDate> = 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<LocalDate> =
(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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
)
Expand All @@ -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
Expand Down Expand Up @@ -90,6 +87,4 @@ sealed interface ComponentMatch {
}
}

data class PotentialMatch(val prisoner: PrisonSearchResult, val matches: List<ComponentMatch>)

fun LocalDate.withinDays(date: LocalDate, days: Int = 7): Boolean = abs(ChronoUnit.DAYS.between(this, date)) <= days
data class PotentialMatch(val prisoner: PrisonSearchResult, val matches: List<ComponentMatch>)

0 comments on commit 242f8be

Please sign in to comment.