From 748027026740be0633079f01cafea55bf760251a Mon Sep 17 00:00:00 2001 From: Anthony Britton Date: Mon, 16 Oct 2023 17:34:11 +0100 Subject: [PATCH] PI-1441 add contact when adding licence conditions --- .../justice/digital/hmpps/data/DataLoader.kt | 1 + .../data/generator/ReferenceDataGenerator.kt | 4 + .../delius/contact.entity/Contact.kt | 103 ++++++++++++++++++ .../delius/sentence/entity/CvlMapping.kt | 8 +- .../digital/hmpps/service/ContactService.kt | 35 ++++++ .../hmpps/service/LicenceActivatedHandler.kt | 2 +- .../hmpps/service/LicenceConditionApplier.kt | 22 +++- 7 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact.entity/Contact.kt create mode 100644 projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ContactService.kt diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt index 50ed9d4e36..307025fcf2 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt @@ -78,6 +78,7 @@ class DataLoader( ReferenceDataGenerator.LC_STANDARD_SUB_CATEGORY, ReferenceDataGenerator.LC_BESPOKE_CATEGORY, ReferenceDataGenerator.LC_BESPOKE_SUB_CATEGORY, + ReferenceDataGenerator.CONTACT_TYPE_LPOP, PersonGenerator.PERSON_CREATE_LC, SentenceGenerator.EVENT_CREATE_LC, SentenceGenerator.SENTENCE_CREATE_LC, diff --git a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferenceDataGenerator.kt b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferenceDataGenerator.kt index 823f704907..988f0c2619 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferenceDataGenerator.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/ReferenceDataGenerator.kt @@ -1,5 +1,6 @@ package uk.gov.justice.digital.hmpps.data.generator +import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CvlMapping import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Dataset import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.LicenceConditionCategory @@ -25,6 +26,7 @@ object ReferenceDataGenerator { generateLcSubCategory("ADD2S") ) ) + val CONTACT_TYPE_LPOP = generateContactType(ContactType.LPOP) fun generateLcCategory(code: String, id: Long = IdGenerator.getAndIncrement()) = LicenceConditionCategory(code, id) fun generateDataset(code: String, id: Long = IdGenerator.getAndIncrement()) = Dataset(code, id) @@ -44,4 +46,6 @@ object ReferenceDataGenerator { subCategory: ReferenceData, id: Long = IdGenerator.getAndIncrement() ) = CvlMapping(cvlCode, mainCategory, subCategory, id) + + fun generateContactType(code: String, id: Long = IdGenerator.getAndIncrement()) = ContactType(code, id) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact.entity/Contact.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact.entity/Contact.kt new file mode 100644 index 0000000000..ab4fa76898 --- /dev/null +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/contact.entity/Contact.kt @@ -0,0 +1,103 @@ +package uk.gov.justice.digital.hmpps.integrations.delius.contact.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.EntityListeners +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.Lob +import jakarta.persistence.ManyToOne +import jakarta.persistence.SequenceGenerator +import jakarta.persistence.Table +import jakarta.persistence.Version +import org.hibernate.annotations.Immutable +import org.springframework.data.annotation.CreatedBy +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedBy +import org.springframework.data.annotation.LastModifiedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import org.springframework.data.jpa.repository.JpaRepository +import uk.gov.justice.digital.hmpps.exception.NotFoundException +import java.time.LocalDate +import java.time.ZonedDateTime + +@Entity +@EntityListeners(AuditingEntityListener::class) +@Table(name = "contact") +@SequenceGenerator(name = "contact_id_generator", sequenceName = "contact_id_seq", allocationSize = 1) +class Contact( + + @Column(name = "offender_id") + val personId: Long, + val eventId: Long?, + + @Column(name = "contact_date") + val date: LocalDate, + + @Column(name = "contact_start_time") + val startTime: ZonedDateTime?, + + @ManyToOne + @JoinColumn(name = "contact_type_id") + val type: ContactType, + + val teamId: Long, + val staffId: Long, + + @Lob + @Column + val notes: String?, + + @Column(columnDefinition = "number") + val softDeleted: Boolean = false, + + @Version + @Column(name = "row_version") + val version: Long = 0, + + @Id + @Column(name = "contact_id") + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contact_id_generator") + val id: Long = 0 +) { + val partitionAreaId: Long = 0 + + @CreatedDate + var createdDatetime: ZonedDateTime = ZonedDateTime.now() + + @CreatedBy + var createdByUserId: Long = 0 + + @LastModifiedDate + var lastUpdatedDatetime: ZonedDateTime = ZonedDateTime.now() + + @LastModifiedBy + var lastUpdatedUserId: Long = 0 +} + +@Immutable +@Entity +@Table(name = "r_contact_type") +class ContactType( + + val code: String, + + @Id + @Column(name = "contact_type_id") + val id: Long +) { + companion object { + val LPOP = "LPOP" + } +} + +interface ContactRepository : JpaRepository + +interface ContactTypeRepository : JpaRepository { + fun findByCode(code: String): ContactType? +} + +fun ContactTypeRepository.getByCode(code: String): ContactType = + findByCode(code) ?: throw NotFoundException("ContactType", "code", code) diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CvlMapping.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CvlMapping.kt index 1cd823e934..d64ccf5340 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CvlMapping.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/sentence/entity/CvlMapping.kt @@ -29,10 +29,10 @@ class CvlMapping( val id: Long ) { companion object { - val STANDARD_CATEGORY_CODE = "StdMain" - val STANDARD_SUB_CATEGORY_CODE = "StdSub" - val BESPOKE_CATEGORY_CODE = "BesMain" - val BESPOKE_SUB_CATEGORY_CODE = "BesSub" + val STANDARD_CATEGORY_CODE = "SL1" + val STANDARD_SUB_CATEGORY_CODE = "SL1" + val BESPOKE_CATEGORY_CODE = "BESP" + val BESPOKE_SUB_CATEGORY_CODE = "NSTT9" } } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ContactService.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ContactService.kt new file mode 100644 index 0000000000..a60ac30167 --- /dev/null +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/ContactService.kt @@ -0,0 +1,35 @@ +package uk.gov.justice.digital.hmpps.service + +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.Contact +import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactRepository +import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType +import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactTypeRepository +import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.getByCode +import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.PersonManager +import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Disposal +import java.time.ZonedDateTime + +@Service +class ContactService( + private val contactTypeRepository: ContactTypeRepository, + private val contactRepository: ContactRepository +) { + fun createContact(disposal: Disposal, com: PersonManager, startDateTime: ZonedDateTime): Contact { + return contactRepository.save( + Contact( + disposal.event.person.id, + disposal.event.id, + startDateTime.toLocalDate(), + startDateTime, + contactTypeRepository.getByCode(ContactType.LPOP), + com.team.id, + com.staff.id, + """ + |Delius has been updated with licence conditions entered in the Create and Vary a licence service. + |Select the following link to navigate to Create and Vary a licence service and view the licence. + """.trimMargin() + ) + ) + } +} diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceActivatedHandler.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceActivatedHandler.kt index f0d8d77fac..e347e77e68 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceActivatedHandler.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceActivatedHandler.kt @@ -15,7 +15,7 @@ class LicenceActivatedHandler( val (crn, url) = validateEvent(domainEvent) val activatedLicence = cvlClient.getActivatedLicence(url) ?: throw NotFoundException("Activated Licence", "detailUrl", url) - lca.applyLicenceConditions(crn, activatedLicence) + lca.applyLicenceConditions(crn, activatedLicence, domainEvent.occurredAt) } catch (e: Exception) { listOf(ActionResult.Failure(e)) } diff --git a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionApplier.kt b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionApplier.kt index 613a77462a..937b05ed78 100644 --- a/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionApplier.kt +++ b/projects/create-and-vary-a-licence-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/LicenceConditionApplier.kt @@ -23,6 +23,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Referenc import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.getByCode import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.getByCvlCode import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.getLicenceConditionSubCategory +import java.time.ZonedDateTime @Service class LicenceConditionApplier( @@ -31,23 +32,30 @@ class LicenceConditionApplier( private val cvlMappingRepository: CvlMappingRepository, private val licenceConditionCategoryRepository: LicenceConditionCategoryRepository, private val referenceDataRepository: ReferenceDataRepository, - private val licenceConditionService: LicenceConditionService + private val licenceConditionService: LicenceConditionService, + private val contactService: ContactService ) { @Transactional - fun applyLicenceConditions(crn: String, activatedLicence: ActivatedLicence): List { + fun applyLicenceConditions( + crn: String, + activatedLicence: ActivatedLicence, + occurredAt: ZonedDateTime + ): List { val com = personManagerRepository.findByPersonCrn(crn) ?: throw NotFoundException("Person", "crn", crn) return disposalRepository.findCustodialSentences(crn) .flatMap { applyLicenceConditions( SentencedCase(com, it, licenceConditionService.findByDisposalId(it.id)), - activatedLicence + activatedLicence, + occurredAt ) } } private fun applyLicenceConditions( sentencedCase: SentencedCase, - activatedLicence: ActivatedLicence + activatedLicence: ActivatedLicence, + occurredAt: ZonedDateTime ): List { val standardResult = activatedLicence.groupedConditions( sentencedCase, @@ -64,7 +72,11 @@ class LicenceConditionApplier( activatedLicence.bespokeLicenceConditions, ActionResult.Type.BespokeLicenceConditionAdded ) - return listOfNotNull(standardResult, additionalResult, bespokeResult).ifEmpty { + val results = listOfNotNull(standardResult, additionalResult, bespokeResult) + if (results.isNotEmpty()) { + contactService.createContact(sentencedCase.sentence, sentencedCase.com, occurredAt) + } + return results.ifEmpty { listOf( ActionResult.Success( ActionResult.Type.NoChangeToLicenceConditions,