diff --git a/projects/unpaid-work-and-delius/build.gradle.kts b/projects/unpaid-work-and-delius/build.gradle.kts index 2a17034144..b3460dbea5 100644 --- a/projects/unpaid-work-and-delius/build.gradle.kts +++ b/projects/unpaid-work-and-delius/build.gradle.kts @@ -19,8 +19,6 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation(libs.springdoc) - implementation(libs.mapstruct) - kapt(libs.mapstructprocessor) dev(project(":libs:dev-tools")) dev("com.h2database:h2") diff --git a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseDetailsService.kt b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseDetailsService.kt index 0f7910adf8..ed855417d3 100644 --- a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseDetailsService.kt +++ b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseDetailsService.kt @@ -5,17 +5,120 @@ import uk.gov.justice.digital.hmpps.controller.casedetails.entity.CaseRepository import uk.gov.justice.digital.hmpps.controller.casedetails.entity.EventRepository import uk.gov.justice.digital.hmpps.controller.casedetails.entity.getCase import uk.gov.justice.digital.hmpps.controller.casedetails.entity.getEvent -import uk.gov.justice.digital.hmpps.controller.casedetails.model.CaseDetails +import uk.gov.justice.digital.hmpps.controller.casedetails.model.* +import uk.gov.justice.digital.hmpps.integrations.common.model.* @Service class CaseDetailsService( val caseRepository: CaseRepository, - val eventRepository: EventRepository, - val caseMapper: CaseMapper + val eventRepository: EventRepository ) { fun getCaseDetails(crn: String, eventId: Long): CaseDetails { val case = caseRepository.getCase(crn) val event = eventRepository.getEvent(eventId) - return caseMapper.withAdditionalMappings(case, event) + return CaseDetails( + crn = case.crn, + name = Name( + forename = case.forename, + middleName = case.secondName, + surname = case.surname + ), + dateOfBirth = case.dateOfBirth, + gender = case.gender?.description, + genderIdentity = case.genderIdentity?.description, + croNumber = case.croNumber, + pncNumber = case.pncNumber, + aliases = case.aliases.map { Alias(it.name(), it.dateOfBirth) }, + emailAddress = case.emailAddress, + phoneNumbers = listOfNotNull( + case.mobileNumber?.let { PhoneNumber("MOBILE", it) }, + case.telephoneNumber?.let { PhoneNumber("TELEPHONE", it) } + ), + mainAddress = case.addresses.firstOrNull()?.let { + Address(it.buildingName, it.addressNumber, it.streetName, it.district, it.town, it.county, it.postcode) + }, + ethnicity = case.ethnicity?.description, + disabilities = case.disabilities.map { + Disability( + type = Type(it.type.code, it.type.description), + condition = it.condition?.let { c -> Type(c.code, c.description) }, + notes = it.notes + ) + }, + provisions = case.provisions.map { + Provision( + type = Type(it.type.code, it.type.description), + category = it.category?.let { c -> Type(c.code, c.description) }, + notes = it.notes + ) + }, + language = case.primaryLanguage?.description?.let { + Language( + requiresInterpreter = case.requiresInterpreter ?: false, + primaryLanguage = it + ) + }, + personalCircumstances = case.personalCircumstances.map { + PersonalCircumstance( + type = Type(it.type.code, it.type.description), + subType = it.subType?.let { t -> Type(t.code, t.description) }, + notes = it.notes, + evidenced = it.evidenced ?: false + ) + }, + personalContacts = case.personalContacts.map { + PersonalContact( + relationship = it.relationship, + relationshipType = Type(it.relationshipType.code, it.relationshipType.description), + name = it.name(), + mobileNumber = it.mobileNumber, + telephoneNumber = it.address?.telephoneNumber, + address = it.address?.let { address -> + Address( + buildingName = address.buildingName, + addressNumber = address.addressNumber, + streetName = address.streetName, + district = address.district, + town = address.town, + county = address.county, + postcode = address.postcode + ) + } + ) + }, + mappaRegistration = case.registrations.filter { it.type.code == "MAPPA" }.maxByOrNull { it.startDate } + ?.let { + MappaRegistration( + startDate = it.startDate, + level = Type(it.level.code, it.level.description), + category = Type(it.category.code, it.category.description) + ) + }, + registerFlags = case.registrations.map { + RegisterFlag( + code = it.type.code, + description = it.type.description, + riskColour = it.type.riskColour + ) + }, + sentence = if (event.mainOffence != null && event.disposal != null) { + Sentence( + startDate = event.disposal.disposalDate, + mainOffence = MainOffence( + Type( + event.mainOffence.offence.mainCategoryCode, + event.mainOffence.offence.mainCategoryDescription + ), + Type( + event.mainOffence.offence.subCategoryCode, + event.mainOffence.offence.subCategoryDescription + ) + ) + ) + } else { + null + } + + ) } } diff --git a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseMapper.kt b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseMapper.kt deleted file mode 100644 index 6aabea4608..0000000000 --- a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/casedetails/CaseMapper.kt +++ /dev/null @@ -1,163 +0,0 @@ -package uk.gov.justice.digital.hmpps.controller.casedetails - -import org.mapstruct.Mapper -import org.mapstruct.Mapping -import uk.gov.justice.digital.hmpps.controller.casedetails.entity.CaseAddress -import uk.gov.justice.digital.hmpps.controller.casedetails.entity.CaseEntity -import uk.gov.justice.digital.hmpps.controller.casedetails.entity.CasePersonalCircumstanceEntity -import uk.gov.justice.digital.hmpps.controller.casedetails.entity.CasePersonalContactEntity -import uk.gov.justice.digital.hmpps.controller.casedetails.entity.DisabilityEntity -import uk.gov.justice.digital.hmpps.controller.casedetails.entity.Event -import uk.gov.justice.digital.hmpps.controller.casedetails.model.Alias -import uk.gov.justice.digital.hmpps.controller.casedetails.model.CaseDetails -import uk.gov.justice.digital.hmpps.controller.casedetails.model.Disability -import uk.gov.justice.digital.hmpps.controller.casedetails.model.Language -import uk.gov.justice.digital.hmpps.controller.casedetails.model.MainOffence -import uk.gov.justice.digital.hmpps.controller.casedetails.model.MappaRegistration -import uk.gov.justice.digital.hmpps.controller.casedetails.model.PhoneNumber -import uk.gov.justice.digital.hmpps.controller.casedetails.model.Provision -import uk.gov.justice.digital.hmpps.controller.casedetails.model.RegisterFlag -import uk.gov.justice.digital.hmpps.controller.casedetails.model.Sentence -import uk.gov.justice.digital.hmpps.controller.casedetails.model.name -import uk.gov.justice.digital.hmpps.integrations.common.mapper.AddressMapper -import uk.gov.justice.digital.hmpps.integrations.common.model.Address -import uk.gov.justice.digital.hmpps.integrations.common.model.PersonalCircumstance -import uk.gov.justice.digital.hmpps.integrations.common.model.PersonalContact -import uk.gov.justice.digital.hmpps.integrations.common.model.Type - -@Mapper( - componentModel = "spring", - uses = [ - CasePersonalCircumstanceMapper::class, - CasePersonalContactMapper::class, - AddressMapper::class, - CaseAddressMapper::class, - DisabilityMapper::class - ] -) -interface CaseMapper { - @Mapping(source = "surname", target = "name.surname") - @Mapping(source = "forename", target = "name.forename") - @Mapping(source = "secondName", target = "name.middleName") - @Mapping(source = "gender.description", target = "gender") - @Mapping(source = "genderIdentity.description", target = "genderIdentity") - @Mapping(source = "ethnicity.description", target = "ethnicity") - @Mapping(target = "aliases", ignore = true) - @Mapping(target = "phoneNumbers", ignore = true) - @Mapping(target = "registerFlags", ignore = true) - @Mapping(target = "language", ignore = true) - @Mapping(target = "mappaRegistration", ignore = true) - @Mapping(target = "sentence", ignore = true) - @Mapping(target = "mainAddress", ignore = true) - fun convertToModel(case: CaseEntity): CaseDetails -} - -fun CaseMapper.withAdditionalMappings(case: CaseEntity, event: Event): CaseDetails { - fun String.language(requiresInterpreter: Boolean) = Language(requiresInterpreter, this) - - val phoneNumbers = listOfNotNull( - case.mobileNumber?.let { PhoneNumber("MOBILE", it) }, - case.telephoneNumber?.let { PhoneNumber("TELEPHONE", it) } - ) - - val aliases = case.aliases.map { - Alias(it.name(), it.dateOfBirth) - } - - val sentence = if (event.mainOffence != null && event.disposal != null) { - Sentence( - event.disposal.disposalDate, - MainOffence( - Type(event.mainOffence.offence.mainCategoryCode, event.mainOffence.offence.mainCategoryDescription), - Type(event.mainOffence.offence.subCategoryCode, event.mainOffence.offence.subCategoryDescription) - ) - ) - } else { - null - } - - val model = convertToModel(case) - - val disabilities = case.disabilities.map { d -> - Disability( - Type(d.type.code, d.type.description), - d.condition?.let { con -> Type(con.code, con.description) }, - d.notes - ) - } - - val provisions = case.provisions.map { p -> - Provision( - Type(p.type.code, p.type.description), - p.category?.let { cat -> Type(cat.code, cat.description) }, - p.notes - ) - } - - val mainAddress = case.addresses.firstOrNull()?.let { - Address(it.buildingName, it.addressNumber, it.streetName, it.district, it.town, it.county, it.postcode) - } - - return model.copy( - aliases = aliases, - phoneNumbers = phoneNumbers, - mappaRegistration = populateMappaRegistration(case), - registerFlags = populateRegisterFlags(case), - language = case.primaryLanguage?.description?.language(case.requiresInterpreter ?: false), - sentence = sentence, - disabilities = disabilities, - provisions = provisions, - mainAddress = mainAddress - ) -} - -fun populateRegisterFlags(case: CaseEntity): List { - return case.registrations.map { - RegisterFlag( - it.type.code, - it.type.description, - it.type.riskColour - ) - } -} - -fun populateMappaRegistration(case: CaseEntity): MappaRegistration? { - val mappaRegistrations = - case.registrations.sortedByDescending { it.startDate }.stream().filter { - it.type.code == "MAPPA" - } - - return mappaRegistrations.findFirst().map { - MappaRegistration( - it.startDate, - Type(it.level.code, it.level.description), - Type(it.category.code, it.category.description) - ) - }.orElse(null) -} - -@Mapper(componentModel = "spring") -interface CasePersonalCircumstanceMapper { - @Mapping(source = "evidenced", target = "evidenced", defaultValue = "false") - fun convertToModel(personalCircumstanceEntity: CasePersonalCircumstanceEntity): PersonalCircumstance -} - -@Mapper(componentModel = "spring") -interface CaseAddressMapper { - fun convertToModel(caseAddress: CaseAddress): Address -} - -@Mapper(componentModel = "spring") -interface CasePersonalContactMapper { - - @Mapping(source = "surname", target = "name.surname") - @Mapping(source = "forename", target = "name.forename") - @Mapping(source = "middleName", target = "name.middleName") - @Mapping(source = "address.telephoneNumber", target = "telephoneNumber") - fun convertToModel(personalContactEntity: CasePersonalContactEntity): PersonalContact -} - -@Mapper(componentModel = "spring") -interface DisabilityMapper { - fun convertToModel(disabilityEntity: DisabilityEntity): Disability -} diff --git a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonMapper.kt b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonMapper.kt deleted file mode 100644 index 0a1276d934..0000000000 --- a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonMapper.kt +++ /dev/null @@ -1,34 +0,0 @@ -package uk.gov.justice.digital.hmpps.controller.personaldetails - -import org.mapstruct.Mapper -import org.mapstruct.Mapping -import uk.gov.justice.digital.hmpps.controller.personaldetails.entity.Person -import uk.gov.justice.digital.hmpps.controller.personaldetails.entity.PersonalCircumstanceEntity -import uk.gov.justice.digital.hmpps.controller.personaldetails.entity.PersonalContactEntity -import uk.gov.justice.digital.hmpps.controller.personaldetails.model.PersonalDetails -import uk.gov.justice.digital.hmpps.integrations.common.mapper.AddressMapper -import uk.gov.justice.digital.hmpps.integrations.common.model.PersonalCircumstance -import uk.gov.justice.digital.hmpps.integrations.common.model.PersonalContact - -@Mapper( - componentModel = "spring", - uses = [PersonalCircumstanceMapper::class, PersonalContactMapper::class, AddressMapper::class] -) -interface PersonMapper { - fun convertToModel(person: Person): PersonalDetails -} - -@Mapper(componentModel = "spring") -interface PersonalCircumstanceMapper { - fun convertToModel(personalCircumstanceEntity: PersonalCircumstanceEntity): PersonalCircumstance -} - -@Mapper(componentModel = "spring") -interface PersonalContactMapper { - - @Mapping(source = "surname", target = "name.surname") - @Mapping(source = "forename", target = "name.forename") - @Mapping(source = "middleName", target = "name.middleName") - @Mapping(source = "address.telephoneNumber", target = "telephoneNumber") - fun convertToModel(personalContactEntity: PersonalContactEntity): PersonalContact -} diff --git a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonalDetailsService.kt b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonalDetailsService.kt index e7c01ba2a6..7527740895 100644 --- a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonalDetailsService.kt +++ b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/personaldetails/PersonalDetailsService.kt @@ -4,11 +4,45 @@ import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.controller.personaldetails.entity.PersonRepository import uk.gov.justice.digital.hmpps.controller.personaldetails.entity.getPerson import uk.gov.justice.digital.hmpps.controller.personaldetails.model.PersonalDetails +import uk.gov.justice.digital.hmpps.controller.personaldetails.model.name +import uk.gov.justice.digital.hmpps.integrations.common.model.Address +import uk.gov.justice.digital.hmpps.integrations.common.model.PersonalCircumstance +import uk.gov.justice.digital.hmpps.integrations.common.model.PersonalContact +import uk.gov.justice.digital.hmpps.integrations.common.model.Type @Service -class PersonalDetailsService(val personRepository: PersonRepository, val personMapper: PersonMapper) { - fun getPersonalDetails(crn: String): PersonalDetails { - val person = personRepository.getPerson(crn) - return personMapper.convertToModel(person) +class PersonalDetailsService(val personRepository: PersonRepository) { + fun getPersonalDetails(crn: String) = with(personRepository.getPerson(crn)) { + PersonalDetails( + crn = crn, + personalCircumstances = personalCircumstances.map { + PersonalCircumstance( + type = Type(it.type.code, it.type.description), + subType = it.subType?.let { t -> Type(t.code, t.description) }, + notes = it.notes, + evidenced = it.evidenced ?: false + ) + }, + personalContacts = personalContacts.map { + PersonalContact( + relationship = it.relationship, + relationshipType = Type(it.relationshipType.code, it.relationshipType.description), + name = it.name(), + telephoneNumber = it.address?.telephoneNumber, + mobileNumber = it.mobileNumber, + address = it.address?.let { address -> + Address( + buildingName = address.buildingName, + addressNumber = address.addressNumber, + streetName = address.streetName, + district = address.district, + town = address.town, + county = address.county, + postcode = address.postcode + ) + } + ) + } + ) } } diff --git a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/common/mapper/AddressMapper.kt b/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/common/mapper/AddressMapper.kt deleted file mode 100644 index b6a21e0427..0000000000 --- a/projects/unpaid-work-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/common/mapper/AddressMapper.kt +++ /dev/null @@ -1,10 +0,0 @@ -package uk.gov.justice.digital.hmpps.integrations.common.mapper - -import org.mapstruct.Mapper -import uk.gov.justice.digital.hmpps.integrations.common.entity.AddressEntity -import uk.gov.justice.digital.hmpps.integrations.common.model.Address - -@Mapper(componentModel = "spring") -interface AddressMapper { - fun convertToModel(addressEntity: AddressEntity): Address -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 0f57ec11da..089ac5711f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -88,8 +88,6 @@ dependencyResolutionManagement { bundle("telemetry", listOf("insights", "opentelemetry-annotations", "sentry")) library("springdoc", "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0") library("wiremock", "org.wiremock:wiremock-standalone:3.6.0") - library("mapstruct", "org.mapstruct:mapstruct:1.5.5.Final") - library("mapstructprocessor", "org.mapstruct:mapstruct-processor:1.5.5.Final") library("flipt", "io.flipt:flipt-java:1.1.1") } }