Skip to content

Commit

Permalink
PI-2584 Create contacts from emails (#4385)
Browse files Browse the repository at this point in the history
* PI-2584 Create contacts from emails

* Enable AsyncApi

* Fix integration test errors for approved-premises-and-delius

* Added tests for duplicate emails
  • Loading branch information
marcus-bcl authored Nov 4, 2024
1 parent 4b369fc commit f3e3c30
Show file tree
Hide file tree
Showing 40 changed files with 776 additions and 86 deletions.
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ plugins {
id("com.google.cloud.tools.jib") apply false
id("base")
id("org.sonarqube")
id("idea")
}

idea {
module {
isDownloadJavadoc = true
isDownloadSources = true
}
}

val agentDeps: Configuration by configurations.creating
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@ object ResourceLoader {
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(SimpleModule().addDeserializer(ZonedDateTime::class.java, ZonedDateTimeDeserializer()))

fun event(filename: String): HmppsDomainEvent =
MAPPER.readValue(ResourceUtils.getFile("classpath:messages/$filename.json"))
fun event(filename: String): HmppsDomainEvent = get(filename)

inline fun <reified T> message(filename: String): T =
MAPPER.readValue(
MAPPER.readValue<Notification<String>>(
ResourceUtils.getFile("classpath:messages/$filename.json")
).message
)
inline fun <reified T> get(filename: String): T =
MAPPER.readValue<T>(ResourceUtils.getFile("classpath:messages/$filename.json"))

inline fun <reified T> message(filename: String): T = MAPPER.readValue(get<Notification<String>>(filename).message)

inline fun <reified T> notification(filename: String): Notification<T> {
val file = ResourceUtils.getFile("classpath:messages/$filename.json")
val stringMessage = MAPPER.readValue<Notification<String>>(file)
val stringMessage = get<Notification<String>>(filename)
return Notification(
message = MAPPER.readValue(stringMessage.message, T::class.java),
attributes = stringMessage.attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ spring.datasource.url: jdbc:h2:mem:./test;MODE=Oracle;DEFAULT_NULL_ORDERING=HIGH

---
spring.config.activate.on-profile: oracle
spring.datasource.url: 'jdbc:tc:oracle:slim-faststart:///XEPDB1'
spring:
datasource.url: 'jdbc:tc:oracle:slim-faststart:///XEPDB1'
jpa.hibernate.ddl-auto: create

---
spring.config.activate.on-profile: delius-db
Expand Down
7 changes: 5 additions & 2 deletions projects/justice-email-and-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ apply(plugin = "com.google.cloud.tools.jib")
dependencies {
implementation(project(":libs:audit"))
implementation(project(":libs:commons"))
implementation(project(":libs:oauth-server"))
implementation(project(":libs:messaging"))

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-data-ldap")
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.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation(libs.springdoc)
implementation(libs.azure.identity)
implementation(libs.microsoft.graph)

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

Expand Down
3 changes: 3 additions & 0 deletions projects/justice-email-and-delius/deploy/database/access.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ database:
access:
username_key: /justice-email-and-delius/db-username
password_key: /justice-email-and-delius/db-password
tables:
- audited_interaction
- contact

audit:
username: JusticeEmailAndDelius
Expand Down
4 changes: 0 additions & 4 deletions projects/justice-email-and-delius/deploy/values-dev.yml
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: justice-email-and-delius-dev.hmpps.service.justice.gov.uk
Expand All @@ -10,8 +8,6 @@ generic-service:
env:
SENTRY_ENVIRONMENT: dev
LOGGING_LEVEL_UK_GOV_DIGITAL_JUSTICE_HMPPS: DEBUG
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI: https://sign-in-dev.hmpps.service.justice.gov.uk/auth/.well-known/jwks.json
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI: https://sign-in-dev.hmpps.service.justice.gov.uk/auth/issuer

generic-prometheus-alerts:
businessHoursOnly: true
2 changes: 0 additions & 2 deletions projects/justice-email-and-delius/deploy/values-preprod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ generic-service:

env:
SENTRY_ENVIRONMENT: preprod
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI: https://sign-in-preprod.hmpps.service.justice.gov.uk/auth/.well-known/jwks.json
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI: https://sign-in-preprod.hmpps.service.justice.gov.uk/auth/issuer

generic-prometheus-alerts:
businessHoursOnly: true
2 changes: 0 additions & 2 deletions projects/justice-email-and-delius/deploy/values-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ generic-service:

env:
SENTRY_ENVIRONMENT: prod
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI: https://sign-in.hmpps.service.justice.gov.uk/auth/.well-known/jwks.json
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI: https://sign-in.hmpps.service.justice.gov.uk/auth/issuer
11 changes: 11 additions & 0 deletions projects/justice-email-and-delius/deploy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
generic-service:
productId: HMPPS518
nameOverride: justice-email-and-delius
serviceAccountName: justice-email-and-delius

image:
repository: ghcr.io/ministryofjustice/hmpps-probation-integration-services/justice-email-and-delius
Expand All @@ -12,11 +13,21 @@ generic-service:
namespace_secrets:
common:
SPRING_DATASOURCE_URL: DB_URL
SPRING_LDAP_URLS: LDAP_URL
SPRING_LDAP_PASSWORD: LDAP_PASSWORD
justice-email-and-delius-database:
SPRING_DATASOURCE_USERNAME: DB_USERNAME
SPRING_DATASOURCE_PASSWORD: DB_PASSWORD
justice-email-and-delius-sentry:
SENTRY_DSN: SENTRY_DSN
justice-email-and-delius-queue:
MESSAGING_PRODUCER_QUEUE: QUEUE_NAME
MESSAGING_CONSUMER_QUEUE: QUEUE_NAME
justice-email-and-delius-microsoft-graph:
MICROSOFT-GRAPH_TENANT-ID: TENANT_ID
MICROSOFT-GRAPH_CLIENT-ID: CLIENT_ID
MICROSOFT-GRAPH_CLIENT-SECRET: CLIENT_SECRET
MICROSOFT-GRAPH_EMAIL-ADDRESS: EMAIL_ADDRESS

generic-prometheus-alerts:
targetApplication: justice-email-and-delius
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
package uk.gov.justice.digital.hmpps.data

import jakarta.annotation.PostConstruct
import jakarta.persistence.EntityManager
import jakarta.transaction.Transactional
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.Data.BUSINESS_INTERACTIONS
import uk.gov.justice.digital.hmpps.data.generator.Data.CONTACT_TYPES
import uk.gov.justice.digital.hmpps.data.generator.Data.DUPLICATE_STAFF_1
import uk.gov.justice.digital.hmpps.data.generator.Data.DUPLICATE_STAFF_2
import uk.gov.justice.digital.hmpps.data.generator.Data.MANAGER
import uk.gov.justice.digital.hmpps.data.generator.Data.MANAGER_STAFF
import uk.gov.justice.digital.hmpps.data.generator.Data.PERSON
import uk.gov.justice.digital.hmpps.data.generator.Data.STAFF
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
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...
listOf(
PERSON,
STAFF,
STAFF.user,
DUPLICATE_STAFF_1,
DUPLICATE_STAFF_1.user,
DUPLICATE_STAFF_2,
DUPLICATE_STAFF_2.user,
MANAGER_STAFF,
MANAGER,
*CONTACT_TYPES,
*BUSINESS_INTERACTIONS,
).forEach {
entityManager.persist(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.audit.BusinessInteraction
import uk.gov.justice.digital.hmpps.audit.BusinessInteractionCode
import uk.gov.justice.digital.hmpps.entity.*
import uk.gov.justice.digital.hmpps.set
import java.time.ZonedDateTime

object Data {
val PERSON = Person(id(), crn = "A000001")
val STAFF = staffWithUser(StaffUser(id(), username = "test-user"))
val DUPLICATE_STAFF_1 = staffWithUser(StaffUser(id(), username = "duplicate1"))
val DUPLICATE_STAFF_2 = staffWithUser(StaffUser(id(), username = "duplicate2"))
val MANAGER_STAFF = Staff(id())
val MANAGER = PersonManager(id(), PERSON, MANAGER_STAFF.id, 102, 103)
val CONTACT_TYPES = ContactType.Code.entries.map { ContactType(id(), it.code) }.toTypedArray()
val BUSINESS_INTERACTIONS = BusinessInteractionCode.entries
.map { BusinessInteraction(id(), it.code, ZonedDateTime.now()) }.toTypedArray()

private fun id() = IdGenerator.getAndIncrement()

private fun staffWithUser(user: StaffUser) = Staff(id(), user = user).also { user.set("staff", it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "RE: A000001 and B000002",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "RE: A000001",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "No CRN here!",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "RE: A000001",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "RE: A000001",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"subject": "RE: A000001",
"bodyContent": "Example message",
"fromEmailAddress": "[email protected]",
"receivedDateTime": "2020-01-01T12:34:56Z[Europe/London]"
}
26 changes: 26 additions & 0 deletions projects/justice-email-and-delius/src/dev/resources/schema.ldif
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
dn: ou=Users,dc=moj,dc=com
objectclass: top
objectclass: organizationalUnit
ou: Users

dn: cn=test-user,ou=Users,dc=moj,dc=com
objectclass: top
objectclass: inetOrgPerson
cn: test-user
sn: test-user
mail: [email protected]

dn: cn=duplicate1,ou=Users,dc=moj,dc=com
objectclass: top
objectclass: inetOrgPerson
cn: duplicate1
sn: duplicate1
mail: [email protected]

dn: cn=duplicate2,ou=Users,dc=moj,dc=com
objectclass: top
objectclass: inetOrgPerson
cn: duplicate2
sn: duplicate2
mail: [email protected]

Loading

0 comments on commit f3e3c30

Please sign in to comment.