generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ContactGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package uk.gov.justice.digital.hmpps.data.generator | ||
|
||
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.AttendanceOutcome | ||
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.Contact | ||
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType | ||
import java.time.LocalDate | ||
|
||
object ContactGenerator { | ||
val ATTENDANCE_CONTACT_TYPE = ContactType( | ||
IdGenerator.getAndIncrement(), | ||
"C295", | ||
"Attendance Type", | ||
true, | ||
true | ||
) | ||
|
||
val ATTENDANCE_OUTCOME = AttendanceOutcome( | ||
IdGenerator.getAndIncrement(), | ||
"Attendance Outcome" | ||
) | ||
|
||
val ATTENDANCE_CONTACT_1 = generateAttendanceContact( | ||
PersonGenerator.CURRENTLY_MANAGED.id, | ||
SentenceGenerator.CURRENTLY_MANAGED.id, | ||
enforcementContact = true | ||
) | ||
val ATTENDANCE_CONTACT_2 = generateAttendanceContact( | ||
PersonGenerator.CURRENTLY_MANAGED.id, | ||
SentenceGenerator.CURRENTLY_MANAGED.id, | ||
enforcementContact = false, | ||
outcome = ATTENDANCE_OUTCOME | ||
) | ||
|
||
fun generateAttendanceContact( | ||
personId: Long, | ||
eventId: Long?, | ||
outcome: AttendanceOutcome? = null, | ||
enforcementContact: Boolean? = null | ||
) = Contact( | ||
id = IdGenerator.getAndIncrement(), | ||
type = ATTENDANCE_CONTACT_TYPE, | ||
outcome = outcome, | ||
date = LocalDate.now().minusDays(1), | ||
offenderId = personId, | ||
eventId = eventId, | ||
enforcementContact = enforcementContact | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
.../court-case-and-delius/src/dev/resources/simulations/__files/get_attendances_C123456.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"attendances": [ | ||
{ | ||
"attended": false, | ||
"complied": false, | ||
"attendanceDate": "2024-08-05", | ||
"contactId": 145, | ||
"contactType": { | ||
"code": "C295", | ||
"description": "Attendance Type" | ||
} | ||
} | ||
] | ||
} |
13 changes: 13 additions & 0 deletions
13
projects/court-case-and-delius/src/dev/resources/simulations/mappings/get-convictions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...urt-case-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/Attendances.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package uk.gov.justice.digital.hmpps.api.model | ||
|
||
import java.time.LocalDate | ||
|
||
data class Attendances( | ||
val attendances: List<Attendance> = emptyList() | ||
) | ||
|
||
data class Attendance( | ||
val attended: Boolean, | ||
val complied: Boolean, | ||
val attendanceDate: LocalDate, | ||
val contactId: Long, | ||
val outcome: String?, | ||
val contactType: ContactTypeDetail | ||
) | ||
|
||
data class ContactTypeDetail( | ||
val code: String?, | ||
val description: String? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact/entity/Attendance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius.contact.entity | ||
|
||
import jakarta.persistence.* | ||
import org.hibernate.annotations.Immutable | ||
import org.hibernate.type.YesNoConverter | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import java.time.LocalDate | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "contact") | ||
class Contact( | ||
@Id | ||
@Column(name = "contact_id") | ||
val id: Long = 0, | ||
|
||
@Column(updatable = false) | ||
val offenderId: Long, | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "contact_type_id") | ||
val type: ContactType? = null, | ||
|
||
@Column(name = "contact_date") | ||
val date: LocalDate, | ||
|
||
@Column(name = "enforcement", columnDefinition = "number") | ||
val enforcementContact: Boolean? = null, | ||
|
||
@Column(name = "event_id") | ||
val eventId: Long? = null, | ||
|
||
@Column(name = "attended") | ||
@Convert(converter = YesNoConverter::class) | ||
val attended: Boolean? = null, | ||
|
||
@Column(name = "complied") | ||
@Convert(converter = YesNoConverter::class) | ||
val complied: Boolean? = null, | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "contact_outcome_type_id") | ||
val outcome: AttendanceOutcome? = null, | ||
|
||
@Column(columnDefinition = "NUMBER") | ||
val softDeleted: Boolean = false | ||
) | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "r_contact_outcome_type") | ||
class AttendanceOutcome( | ||
@Id | ||
@Column(name = "contact_outcome_type_id") | ||
val id: Long, | ||
|
||
@Column | ||
val description: String | ||
) | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "r_contact_type") | ||
class ContactType( | ||
@Id | ||
@Column(name = "contact_type_id") | ||
val id: Long, | ||
|
||
val code: String, | ||
|
||
val description: String, | ||
|
||
@Column(name = "national_standards_contact", length = 1) | ||
@Convert(converter = YesNoConverter::class) | ||
val nationalStandards: Boolean, | ||
|
||
@Column(name = "attendance_contact") | ||
@Convert(converter = YesNoConverter::class) | ||
val attendanceContact: Boolean = false, | ||
) | ||
|
||
interface AttendanceRepository : JpaRepository<CaseNote, Long> { | ||
|
||
@Query( | ||
""" | ||
SELECT contact FROM Contact contact | ||
LEFT OUTER JOIN AttendanceOutcome cot ON cot = contact.outcome | ||
WHERE contact.offenderId = :personId | ||
AND contact.eventId = :eventId | ||
AND contact.date <= :contactDate | ||
AND (contact.enforcementContact = true OR contact.outcome != null) | ||
AND contact.type.attendanceContact = true | ||
AND contact.type.nationalStandards = true | ||
""" | ||
) | ||
fun findByOffenderAndEventId(eventId: Long, personId: Long, contactDate: LocalDate): List<Contact> | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
...main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/service/AttendanceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius.service | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.api.model.Attendance | ||
import uk.gov.justice.digital.hmpps.api.model.Attendances | ||
import uk.gov.justice.digital.hmpps.api.model.ContactTypeDetail | ||
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.AttendanceRepository | ||
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.Contact | ||
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository | ||
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.getPerson | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class AttendanceService( | ||
private val personRepository: PersonRepository, | ||
private val attendanceRepository: AttendanceRepository | ||
) { | ||
|
||
fun getAttendancesFor(crn: String, eventId: Long): Attendances { | ||
val person = personRepository.getPerson(crn) | ||
val attendances = | ||
attendanceRepository.findByOffenderAndEventId(eventId, person.id, LocalDate.now()).map { it.toAttendance() } | ||
return Attendances(attendances) | ||
} | ||
} | ||
|
||
fun Contact.toAttendance() = Attendance( | ||
attended = attended ?: false, | ||
complied = complied ?: false, | ||
attendanceDate = date, | ||
contactId = id, | ||
outcome = outcome?.description, | ||
contactType = ContactTypeDetail(code = type?.code, description = type?.description) | ||
) |