Skip to content

Commit

Permalink
EY-5010 Flytte grunnlag-rivers til behandling-kafka (#6976)
Browse files Browse the repository at this point in the history
  • Loading branch information
Watercolours authored Feb 6, 2025
1 parent 5821159 commit 88c581f
Show file tree
Hide file tree
Showing 17 changed files with 496 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import no.nav.etterlatte.brukerdialog.soeknad.journalfoering.JournalfoerSoeknadS
import no.nav.etterlatte.brukerdialog.soeknad.pdf.PdfGeneratorKlient
import no.nav.etterlatte.funksjonsbrytere.FeatureToggleProperties
import no.nav.etterlatte.funksjonsbrytere.FeatureToggleService
import no.nav.etterlatte.grunnlag.GrunnlagKlient
import no.nav.etterlatte.libs.common.EnvEnum
import no.nav.etterlatte.libs.common.Miljoevariabler
import no.nav.etterlatte.libs.ktor.AzureEnums.AZURE_APP_CLIENT_ID
Expand All @@ -36,6 +37,10 @@ class AppBuilder(
)
}

val grunnlagKlient: GrunnlagKlient by lazy {
GrunnlagKlient(behandlingApp, "http://etterlatte-behandling")
}

val tidshendelserService: TidshendelseService by lazy {
TidshendelseService(
behandlingService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import no.nav.etterlatte.brukerdialog.inntektsjustering.InntektsjusteringRiver
import no.nav.etterlatte.brukerdialog.omsmeldinnendring.OmsMeldtInnEndringRiver
import no.nav.etterlatte.brukerdialog.soeknad.NySoeknadRiver
import no.nav.etterlatte.brukerdialog.soeknad.OpprettBehandlingRiver
import no.nav.etterlatte.grunnlag.GrunnlagHendelserRiver
import no.nav.etterlatte.grunnlag.GrunnlagsversjoneringRiver
import no.nav.etterlatte.inntektsjustering.AarligInntektsjusteringJobbRiver
import no.nav.etterlatte.migrering.AvbrytBehandlingHvisMigreringFeilaRiver
import no.nav.etterlatte.opplysningerfrasoknad.StartUthentingFraSoeknadRiver
Expand Down Expand Up @@ -75,4 +77,7 @@ private fun settOppRivers(
behandlingKlient = appBuilder.behandlingKlient,
journalfoerService = appBuilder.journalfoerOmsMeldtInnEndringService,
)

GrunnlagHendelserRiver(rapidsConnection, appBuilder.grunnlagKlient)
GrunnlagsversjoneringRiver(rapidsConnection, appBuilder.grunnlagKlient)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package no.nav.etterlatte.grunnlag.rivers
package no.nav.etterlatte.grunnlag

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.readValue
import no.nav.etterlatte.grunnlag.GrunnlagService
import no.nav.etterlatte.libs.common.grunnlag.Grunnlagsopplysning
import no.nav.etterlatte.libs.common.objectMapper
import no.nav.etterlatte.libs.common.person.Folkeregisteridentifikator
Expand All @@ -21,12 +20,15 @@ import no.nav.etterlatte.rapidsandrivers.migrering.VILKAARSVURDERT_KEY
import no.nav.helse.rapids_rivers.JsonMessage
import no.nav.helse.rapids_rivers.MessageContext
import no.nav.helse.rapids_rivers.RapidsConnection
import org.slf4j.LoggerFactory
import java.util.UUID

class GrunnlagHendelserRiver(
rapidsConnection: RapidsConnection,
private val grunnlagService: GrunnlagService,
private val grunnlagKlient: GrunnlagKlient,
) : ListenerMedLogging() {
private val logger = LoggerFactory.getLogger(this::class.java)

init {
initialiserRiver(rapidsConnection, EventNames.NY_OPPLYSNING) {
validate { it.interestedIn(FNR_KEY) }
Expand All @@ -43,21 +45,25 @@ class GrunnlagHendelserRiver(
packet: JsonMessage,
context: MessageContext,
) {
val sakId = packet[SAK_ID_KEY].asLong().let { SakId(it) }
val sakId = SakId(packet[SAK_ID_KEY].asLong())
val behandlingId = packet[BEHANDLING_ID_KEY].let { UUID.fromString(it.asText()) }

val opplysninger: List<Grunnlagsopplysning<JsonNode>> =
objectMapper.readValue(packet[OPPLYSNING_KEY].toJson())!!

val fnr = packet[FNR_KEY].textValue()
if (fnr == null) {
grunnlagService.lagreNyeSaksopplysninger(
logger.info("Lagrer nye saksopplysninger på sak $sakId")

grunnlagKlient.lagreNyeSaksopplysninger(
sakId,
behandlingId,
opplysninger,
)
} else {
grunnlagService.lagreNyePersonopplysninger(
logger.info("Lagrer nye personopplysninger på sak $sakId")

grunnlagKlient.lagreNyePersonopplysninger(
sakId,
behandlingId,
Folkeregisteridentifikator.of(fnr),
Expand All @@ -66,5 +72,7 @@ class GrunnlagHendelserRiver(
}
packet.eventName = GRUNNLAG_OPPDATERT
context.publish(packet.toJson())

logger.info("Grunnlag opppdatert på sak $sakId")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package no.nav.etterlatte.grunnlag

import com.fasterxml.jackson.databind.JsonNode
import io.ktor.client.HttpClient
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType
import kotlinx.coroutines.runBlocking
import no.nav.etterlatte.libs.common.grunnlag.Grunnlagsopplysning
import no.nav.etterlatte.libs.common.grunnlag.NyePersonopplysninger
import no.nav.etterlatte.libs.common.grunnlag.NyeSaksopplysninger
import no.nav.etterlatte.libs.common.person.Folkeregisteridentifikator
import no.nav.etterlatte.libs.common.sak.SakId
import org.slf4j.LoggerFactory
import java.util.UUID

class GrunnlagKlient(
private val klient: HttpClient,
private val url: String,
) {
private val logger = LoggerFactory.getLogger(GrunnlagKlient::class.java)

fun lagreNyePersonopplysninger(
sakId: SakId,
behandlingId: UUID,
fnr: Folkeregisteridentifikator,
nyeOpplysninger: List<Grunnlagsopplysning<JsonNode>>,
) {
logger.info("Lagrer nye personopplysninger på sak $sakId")

runBlocking {
klient.post("$url/grunnlag/person/behandling/$behandlingId/nye-opplysninger") {
contentType(ContentType.Application.Json)
setBody(NyePersonopplysninger(sakId, fnr, nyeOpplysninger))
}
}
}

fun lagreNyeSaksopplysninger(
sakId: SakId,
behandlingId: UUID,
nyeOpplysninger: List<Grunnlagsopplysning<JsonNode>>,
) {
logger.info("Lagrer nye saksopplysninger på sak $sakId")

runBlocking {
klient.post("$url/grunnlag/behandling/$behandlingId/nye-opplysninger") {
contentType(ContentType.Application.Json)
setBody(NyeSaksopplysninger(sakId, nyeOpplysninger))
}
}
}

fun laasVersjonForBehandling(behandlingId: UUID) {
logger.info("Låser grunnlagsversjon for behandling (id=$behandlingId)")

runBlocking {
klient.post("$url/grunnlag/behandling/$behandlingId/laas") {
contentType(ContentType.Application.Json)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package no.nav.etterlatte.grunnlag.rivers
package no.nav.etterlatte.grunnlag

import no.nav.etterlatte.grunnlag.GrunnlagService
import no.nav.etterlatte.libs.common.vedtak.VedtakKafkaHendelseHendelseType
import no.nav.etterlatte.rapidsandrivers.ListenerMedLogging
import no.nav.helse.rapids_rivers.JsonMessage
Expand All @@ -12,9 +11,9 @@ import java.util.UUID

class GrunnlagsversjoneringRiver(
rapidsConnection: RapidsConnection,
private val grunnlagService: GrunnlagService,
private val grunnlagKlient: GrunnlagKlient,
) : ListenerMedLogging() {
private val logger: Logger = LoggerFactory.getLogger(GrunnlagsversjoneringRiver::class.java)
private val logger: Logger = LoggerFactory.getLogger(this::class.java)

init {
initialiserRiver(rapidsConnection, VedtakKafkaHendelseHendelseType.ATTESTERT) {
Expand All @@ -29,10 +28,10 @@ class GrunnlagsversjoneringRiver(
val behandlingId = UUID.fromString(packet["vedtak.behandlingId"].asText())

try {
logger.info("Mottok melding om attestert behandling (id=$behandlingId)")
grunnlagService.laasVersjonForBehandling(behandlingId)
logger.info("Mottok melding om attestert behandling (id=$behandlingId). Forsøker å låse grunnlagsversjon..")
grunnlagKlient.laasVersjonForBehandling(behandlingId)
} catch (e: Exception) {
logger.error("Feil oppsto ved låsing av attestert behandling (id=$): ", e)
logger.error("Feil oppsto ved låsing av attestert behandling (id=$behandlingId): ", e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package no.nav.etterlatte.grunnlag

import com.fasterxml.jackson.databind.JsonNode
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import no.nav.etterlatte.libs.common.grunnlag.Grunnlagsopplysning
import no.nav.etterlatte.libs.common.grunnlag.opplysningstyper.Opplysningstype
import no.nav.etterlatte.libs.common.objectMapper
import no.nav.etterlatte.libs.common.rapidsandrivers.lagParMedEventNameKey
import no.nav.etterlatte.libs.common.tidspunkt.Tidspunkt
import no.nav.etterlatte.libs.common.toJsonNode
import no.nav.etterlatte.libs.testdata.grunnlag.AVDOED_FOEDSELSNUMMER
import no.nav.etterlatte.libs.testdata.grunnlag.statiskUuid
import no.nav.etterlatte.rapidsandrivers.BEHANDLING_ID_KEY
import no.nav.etterlatte.rapidsandrivers.EventNames
import no.nav.etterlatte.rapidsandrivers.FNR_KEY
import no.nav.etterlatte.rapidsandrivers.OPPLYSNING_KEY
import no.nav.etterlatte.rapidsandrivers.SAK_ID_KEY
import no.nav.helse.rapids_rivers.JsonMessage
import no.nav.helse.rapids_rivers.testsupport.TestRapid
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.util.UUID

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class GrunnlagHendelserRiverTest {
private val grunnlagKlientMock = mockk<GrunnlagKlient>(relaxed = true)

private lateinit var inspector: TestRapid

@BeforeAll
fun beforeAll() {
inspector =
TestRapid().apply {
GrunnlagHendelserRiver(this, grunnlagKlientMock)
}
}

private val fnr = AVDOED_FOEDSELSNUMMER
private val tidspunkt = Tidspunkt.now()
private val kilde = Grunnlagsopplysning.Pdl(tidspunkt, null, null)
private val nyOpplysning =
Grunnlagsopplysning(
id = statiskUuid,
kilde = kilde,
opplysningType = Opplysningstype.NAVN,
meta = objectMapper.createObjectNode(),
opplysning = "Ola".toJsonNode(),
attestering = null,
fnr = fnr,
)

@Test
fun `Ny personopplysning sendes til grunnlag`() {
val melding =
JsonMessage
.newMessage(
mapOf(
EventNames.NY_OPPLYSNING.lagParMedEventNameKey(),
OPPLYSNING_KEY to listOf(nyOpplysning),
FNR_KEY to fnr,
SAK_ID_KEY to 1,
BEHANDLING_ID_KEY to UUID.randomUUID(),
),
).toJson()

inspector.sendTestMessage(melding)

val opplysningSlot = slot<List<Grunnlagsopplysning<JsonNode>>>()

verify { grunnlagKlientMock.lagreNyePersonopplysninger(any(), any(), any(), capture(opplysningSlot)) }

with(opplysningSlot.captured.first()) {
assertEquals(this.id, nyOpplysning.id)
assertEquals(this.kilde, nyOpplysning.kilde)
assertEquals(this.opplysningType, nyOpplysning.opplysningType)
assertEquals(this.meta, nyOpplysning.meta)
assertEquals(this.kilde, nyOpplysning.kilde)
assertEquals(this.attestering, nyOpplysning.attestering)
assertEquals(this.fnr!!.value, nyOpplysning.fnr!!.value)
assertEquals(this.opplysning, nyOpplysning.opplysning)
}
}

@Test
fun `Ny saksopplysning sendes til grunnlag`() {
val melding =
JsonMessage
.newMessage(
mapOf(
EventNames.NY_OPPLYSNING.lagParMedEventNameKey(),
OPPLYSNING_KEY to listOf(nyOpplysning),
// UTEN fnr for å trigge lagring til saksnivå
SAK_ID_KEY to 1,
BEHANDLING_ID_KEY to UUID.randomUUID(),
),
).toJson()

inspector.sendTestMessage(melding)

val opplysningSlot = slot<List<Grunnlagsopplysning<JsonNode>>>()

verify { grunnlagKlientMock.lagreNyeSaksopplysninger(any(), any(), capture(opplysningSlot)) }

with(opplysningSlot.captured.first()) {
assertEquals(this.id, nyOpplysning.id)
assertEquals(this.kilde, nyOpplysning.kilde)
assertEquals(this.opplysningType, nyOpplysning.opplysningType)
assertEquals(this.meta, nyOpplysning.meta)
assertEquals(this.kilde, nyOpplysning.kilde)
assertEquals(this.attestering, nyOpplysning.attestering)
assertEquals(this.fnr!!.value, nyOpplysning.fnr!!.value)
assertEquals(this.opplysning, nyOpplysning.opplysning)
}
}
}
Loading

0 comments on commit 88c581f

Please sign in to comment.