Skip to content

Commit

Permalink
PI-2564 Copy API endpoints from CVL (#4452)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Nov 20, 2024
1 parent 2f253ac commit bc559ec
Show file tree
Hide file tree
Showing 36 changed files with 1,615 additions and 33 deletions.
3 changes: 3 additions & 0 deletions projects/assess-for-early-release-and-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-ldap")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation(libs.springdoc)

dev(project(":libs:dev-tools"))
dev("com.unboundid:unboundid-ldapsdk")
dev("com.h2database:h2")
dev("org.testcontainers:oracle-xe")

Expand All @@ -31,6 +33,7 @@ configure<ClassPathExtension> {
jacocoExclusions = listOf(
"**/config/**",
"**/entity/**",
"**/ldap/**",
"**/AppKt.class"
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
enabled: false # TODO set this to true when you're ready to deploy your service

generic-service:
ingress:
host: assess-for-early-release-and-delius-dev.hmpps.service.justice.gov.uk
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
enabled: false # TODO set this to true when you're ready to deploy your service

generic-service:
ingress:
host: assess-for-early-release-and-delius-preprod.hmpps.service.justice.gov.uk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ generic-service:
namespace_secrets:
common:
SPRING_DATASOURCE_URL: DB_URL
SPRING_LDAP_URLS: LDAP_URL
SPRING_LDAP_USERNAME: LDAP_USERNAME
SPRING_LDAP_PASSWORD: LDAP_PASSWORD
assess-for-early-release-and-delius-database:
SPRING_DATASOURCE_USERNAME: DB_USERNAME
SPRING_DATASOURCE_PASSWORD: DB_PASSWORD
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,92 @@
package uk.gov.justice.digital.hmpps.data

import jakarta.annotation.PostConstruct
import jakarta.persistence.EntityManager
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.user.AuditUserRepository

@Component
@ConditionalOnProperty("seed.database")
class DataLoader(
private val auditUserRepository: AuditUserRepository
private val auditUserRepository: AuditUserRepository,
private val entityManager: EntityManager
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
fun saveAuditUser() {
auditUserRepository.save(UserGenerator.AUDIT_USER)
}

@Transactional
override fun onApplicationEvent(are: ApplicationReadyEvent) {
// Perform dev/test database setup here, using JPA repositories and generator classes...
entityManager.persist(ProviderGenerator.DEFAULT_PROVIDER)
entityManager.persist(StaffGenerator.PDUHEAD)
entityManager.persist(StaffGenerator.DEFAULT_PDUSTAFF_USER)
entityManager.persist(ProviderGenerator.DEFAULT_BOROUGH)
entityManager.persist(ProviderGenerator.DEFAULT_DISTRICT)

createOfficeLocationsAndDistricts()

entityManager.persist(ProviderGenerator.DEFAULT_TEAM)
entityManager.persist(ProviderGenerator.TEAM_ENDED_OR_NULL_LOCATIONS)

StaffGenerator.DEFAULT = StaffGenerator.generateStaff(
StaffGenerator.DEFAULT.code,
StaffGenerator.DEFAULT.forename,
StaffGenerator.DEFAULT.surname,
listOf(ProviderGenerator.DEFAULT_TEAM),
ProviderGenerator.DEFAULT_PROVIDER,
StaffGenerator.DEFAULT.middleName,
StaffGenerator.DEFAULT.user,
StaffGenerator.DEFAULT.id
)
entityManager.persist(StaffGenerator.DEFAULT)

entityManager.persist(StaffGenerator.DEFAULT_STAFF_USER)
entityManager.flush()

entityManager.persist(PersonGenerator.DEFAULT_PERSON)
entityManager.persist(PersonGenerator.PERSON_ENDED_TEAM_LOCATION)
entityManager.persist(PersonGenerator.DEFAULT_CM)
entityManager.persist(PersonGenerator.CM_ENDED_TEAM_LOCATION)

val person = PersonGenerator.generatePerson("N123456").also(entityManager::persist)
PersonGenerator.generateManager(person).also(entityManager::persist)

createCaseloadData()
}

private fun createOfficeLocationsAndDistricts() {
entityManager.persistAll(
ProviderGenerator.DISTRICT_BRK,
ProviderGenerator.DISTRICT_MKY,
ProviderGenerator.DISTRICT_OXF,
ProviderGenerator.LOCATION_BRK_1,
ProviderGenerator.LOCATION_BRK_2,
ProviderGenerator.LOCATION_ENDED,
ProviderGenerator.LOCATION_NULL
)
}

private fun createCaseloadData() {
entityManager.persistAll(
CaseloadGenerator.TEAM1,
CaseloadGenerator.STAFF1,
CaseloadGenerator.STAFF2,
CaseloadGenerator.CASELOAD_ROLE_OM_1,
CaseloadGenerator.CASELOAD_ROLE_OM_2,
CaseloadGenerator.CASELOAD_ROLE_OM_3,
CaseloadGenerator.CASELOAD_ROLE_OM_4,
CaseloadGenerator.CASELOAD_ROLE_OS_1
)
}

private fun EntityManager.persistAll(vararg entities: Any) {
entities.forEach { persist(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.api.model.ManagedOffender
import uk.gov.justice.digital.hmpps.api.model.Name
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.DEFAULT_TEAM
import uk.gov.justice.digital.hmpps.entity.Caseload
import uk.gov.justice.digital.hmpps.entity.Staff
import uk.gov.justice.digital.hmpps.entity.Team
import uk.gov.justice.digital.hmpps.service.asStaff
import uk.gov.justice.digital.hmpps.service.asTeam
import java.time.LocalDate

object CaseloadGenerator {

val STAFF1 = StaffGenerator.generateStaff("STCDE01", "Bob", "Smith")
val STAFF2 = StaffGenerator.generateStaff("STCDE02", "Joe", "Bloggs")

val TEAM1 = ProviderGenerator.generateTeam("N02BDT")

val CASELOAD_ROLE_OM_1 = generateCaseload(
staff = STAFF1,
team = DEFAULT_TEAM,
crn = "crn0001",
firstName = "John",
secondName = "x",
surname = "Brown",
allocationDate = LocalDate.of(2024, 1, 8),
roleCode = Caseload.CaseloadRole.OFFENDER_MANAGER.value
)

val CASELOAD_ROLE_OM_2 = generateCaseload(
staff = STAFF1,
team = DEFAULT_TEAM,
crn = "crn0022",
firstName = "Jane",
secondName = "y",
surname = "Doe",
allocationDate = LocalDate.of(2024, 1, 9),
roleCode = Caseload.CaseloadRole.OFFENDER_MANAGER.value
)

val CASELOAD_ROLE_OM_3 = generateCaseload(
staff = STAFF2,
team = DEFAULT_TEAM,
crn = "crn0077",
firstName = "Ano",
secondName = "no",
surname = "mys",
allocationDate = LocalDate.of(2024, 1, 10),
roleCode = Caseload.CaseloadRole.OFFENDER_MANAGER.value
)

val CASELOAD_ROLE_OM_4 = generateCaseload(
staff = STAFF2,
team = TEAM1,
crn = "crn0078",
firstName = "Joe",
secondName = "Denis",
surname = "Doe",
allocationDate = LocalDate.of(2024, 1, 10),
roleCode = Caseload.CaseloadRole.OFFENDER_MANAGER.value
)

val CASELOAD_ROLE_OS_1 = generateCaseload(
staff = STAFF2,
team = DEFAULT_TEAM,
crn = "crn0088",
firstName = "Robert",
secondName = "Alan",
surname = "Brown",
roleCode = Caseload.CaseloadRole.ORDER_SUPERVISOR.value
)

val MANAGED_OFFENDER = generateManagedOffender(CASELOAD_ROLE_OM_1, STAFF1, DEFAULT_TEAM)

fun generateCaseload(
staff: Staff,
team: Team,
allocationDate: LocalDate? = LocalDate.now(),
roleCode: String,
crn: String,
firstName: String,
secondName: String?,
surname: String,
startDate: LocalDate? = LocalDate.now(),
id: Long = IdGenerator.getAndIncrement()
) = Caseload(
staff,
team,
allocationDate,
roleCode,
crn,
firstName,
secondName,
surname,
startDate,
id
)

fun generateManagedOffender(
caseload: Caseload,
staff: Staff,
team: Team
) = ManagedOffender(
caseload.crn,
Name(caseload.firstName, caseload.secondName, caseload.surname),
caseload.allocationDate,
staff.asStaff(),
team.asTeam()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.entity.*

object PersonGenerator {
val DEFAULT_PERSON = generatePerson("T123456")
val PERSON_ENDED_TEAM_LOCATION = generatePerson("T123457")
val DEFAULT_CM = generateManager(DEFAULT_PERSON)
val CM_ENDED_TEAM_LOCATION =
generateManager(person = PERSON_ENDED_TEAM_LOCATION, team = ProviderGenerator.TEAM_ENDED_OR_NULL_LOCATIONS)

fun generatePerson(
crn: String,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Person(
crn,
softDeleted,
id
)

fun generateManager(
person: Person,
provider: Provider = ProviderGenerator.DEFAULT_PROVIDER,
team: Team = ProviderGenerator.DEFAULT_TEAM,
staff: Staff = StaffGenerator.DEFAULT,
softDeleted: Boolean = false,
active: Boolean = true,
id: Long = IdGenerator.getAndIncrement()
) = PersonManager(person, provider, team, staff, softDeleted, active, id)
}
Loading

0 comments on commit bc559ec

Please sign in to comment.