Skip to content

Commit

Permalink
Pi 1880 fix next appointment (#3436)
Browse files Browse the repository at this point in the history
* PI-1880: Fixed next appointment date to merge date and time fields
  • Loading branch information
pmcphee77 authored Mar 6, 2024
1 parent 6a9e7ff commit 9100f01
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.OVERVIEW
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Contact
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.ContactType
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Person
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
Expand Down Expand Up @@ -39,7 +40,7 @@ object ContactGenerator {
fun generateContact(
person: Person,
contactType: ContactType,
startTime: ZonedDateTime,
startDateTime: ZonedDateTime,
rarActivity: Boolean? = null,
attended: Boolean? = null,
complied: Boolean? = null,
Expand All @@ -48,8 +49,8 @@ object ContactGenerator {
IdGenerator.getAndIncrement(),
person.id,
contactType,
startTime,
startTime,
startDateTime.toLocalDate(),
ZonedDateTime.of(LocalDate.EPOCH, startDateTime.toLocalTime(), startDateTime.zone),
rarActivity,
attended,
complied,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package uk.gov.justice.digital.hmpps.api.model.overview

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZonedDateTime

data class NextAppointment(
val date: LocalDateTime,
val date: ZonedDateTime,
val description: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import java.time.LocalDate
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

@Immutable
@Entity
Expand All @@ -27,7 +30,7 @@ class Contact(
val type: ContactType,

@Column(name = "contact_date")
val date: ZonedDateTime = ZonedDateTime.now(),
val date: LocalDate = LocalDate.now(),

@Column(name = "contact_start_time")
val startTime: ZonedDateTime = ZonedDateTime.now(),
Expand All @@ -52,7 +55,9 @@ class Contact(

@Column(name = "soft_deleted", columnDefinition = "NUMBER", nullable = false)
var softDeleted: Boolean = false
)
) {
fun startDateTime(): ZonedDateTime = ZonedDateTime.of(date, startTime.toLocalTime(), EuropeLondon)
}

@Immutable
@Entity
Expand All @@ -76,13 +81,31 @@ class ContactType(
interface ContactRepository : JpaRepository<Contact, Long> {
@Query(
"""
select c from Contact c
join fetch c.type t
where c.personId = :personId
and c.type.attendanceContact = true
and c.date > CURRENT_TIMESTAMP
order by c.date asc
"""
select c.*
from contact c
join r_contact_type ct on c.contact_type_id = ct.contact_type_id
where c.offender_id = :personId and ct.attendance_contact = 'Y'
and (to_char(c.contact_date, 'YYYY-MM-DD') > :dateNow
or (to_char(c.contact_date, 'YYYY-MM-DD') = :dateNow and to_char(c.contact_start_time, 'HH24:MI') > :timeNow))
and c.soft_deleted = 0
order by c.contact_date, c.contact_start_time asc
""",
nativeQuery = true
)
fun findFirstAppointment(personId: Long, pageable: Pageable = PageRequest.of(0, 1)): List<Contact>
}
fun findFirstAppointment(
personId: Long,
dateNow: String,
timeNow: String,
pageable: Pageable = PageRequest.of(0, 1)
): List<Contact>
}

fun ContactRepository.firstAppointment(
personId: Long,
date: LocalDate = LocalDate.now(),
startTime: ZonedDateTime = ZonedDateTime.now()
): Contact? = findFirstAppointment(
personId,
date.format(DateTimeFormatter.ISO_LOCAL_DATE),
startTime.format(DateTimeFormatter.ISO_LOCAL_TIME.withZone(EuropeLondon))
).firstOrNull()
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OverviewService(
fun getOverview(crn: String): Overview {
val person = personRepository.getPerson(crn)
val personalDetails = person.toPersonalDetails()
val schedule = Schedule(contactRepository.findFirstAppointment(person.id).firstOrNull()?.toNextAppointment())
val schedule = Schedule(contactRepository.firstAppointment(person.id)?.toNextAppointment())
val events = eventRepository.findByPersonId(person.id)
val activeEvents = events.filter { it.active }
val sentences = activeEvents.map { it.toSentence() }
Expand Down Expand Up @@ -81,5 +81,6 @@ class OverviewService(
fun uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Provision.toProvision() =
Provision(description = type.description)

fun Contact.toNextAppointment() = NextAppointment(description = type.description, date = date.toLocalDateTime())
fun Contact.toNextAppointment() =
NextAppointment(description = type.description, date = startDateTime())
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator
import uk.gov.justice.digital.hmpps.data.generator.ContactGenerator.FIRST_APPT_CONTACT
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.generateEvent
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.EventRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonOverviewRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.RequirementRepository
import java.time.ZonedDateTime

@ExtendWith(MockitoExtension::class)
internal class OverviewServiceTest {
Expand All @@ -38,9 +40,15 @@ internal class OverviewServiceTest {
@Test
fun `calls overview function`() {
val crn = "X000004"

val expectedAppointmentDateTime = ZonedDateTime.of(
FIRST_APPT_CONTACT.date,
FIRST_APPT_CONTACT.startTime.toLocalTime(),
EuropeLondon
)
whenever(personRepository.findByCrn(crn)).thenReturn(PersonGenerator.OVERVIEW)
whenever(contactRepository.findFirstAppointment(PersonGenerator.OVERVIEW.id)).thenReturn(listOf(ContactGenerator.FIRST_APPT_CONTACT))
whenever(contactRepository.findFirstAppointment(any(), any(), any(), any())).thenReturn(
listOf(FIRST_APPT_CONTACT)
)
whenever(requirementRepository.getRarDays(any())).thenReturn(
listOf(RarDays(1, "COMPLETED"), RarDays(2, "SCHEDULED"))
)
Expand Down Expand Up @@ -79,6 +87,7 @@ internal class OverviewServiceTest {
assertThat(res.sentences[0].rar?.scheduled, equalTo(2))
assertThat(res.sentences[0].rar?.completed, equalTo(1))
assertThat(res.sentences[0].rar?.totalDays, equalTo(3))
assertThat(res.schedule.nextAppointment?.date, equalTo(expectedAppointmentDateTime))
}

data class RarDays(val _days: Int, val _type: String) :
Expand Down

0 comments on commit 9100f01

Please sign in to comment.