Skip to content

Commit

Permalink
PI-1441 add contact when adding licence conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Oct 16, 2023
1 parent 58b68d2 commit 7480270
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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<Contact, Long>

interface ContactTypeRepository : JpaRepository<ContactType, Long> {
fun findByCode(code: String): ContactType?
}

fun ContactTypeRepository.getByCode(code: String): ContactType =
findByCode(code) ?: throw NotFoundException("ContactType", "code", code)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<ActionResult> {
fun applyLicenceConditions(
crn: String,
activatedLicence: ActivatedLicence,
occurredAt: ZonedDateTime
): List<ActionResult> {
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<ActionResult> {
val standardResult = activatedLicence.groupedConditions(
sentencedCase,
Expand All @@ -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,
Expand Down

0 comments on commit 7480270

Please sign in to comment.