Skip to content

Commit

Permalink
henter aktørId fra speed-api
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsteinsland committed Nov 7, 2024
1 parent 6aabe90 commit 5b60cc5
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 89 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ val junitJupiterVersion = "5.10.2"
val postgresqlVersion = "42.7.2"
val tbdLibsVersion = "2024.11.07-09.04-76a1afb0"
val rapidsAndRiversVersion = "2024110709231730967802.bf0cda480345"
val mockkVersion = "1.13.13"

plugins {
kotlin("jvm") version "2.0.20"
Expand All @@ -28,6 +29,9 @@ dependencies {
implementation("org.postgresql:postgresql:$postgresqlVersion")
implementation("com.github.navikt:rapids-and-rivers:$rapidsAndRiversVersion")
implementation("com.github.navikt.tbd-libs:spurtedu-client:$tbdLibsVersion")
implementation("com.github.navikt.tbd-libs:azure-token-client-default:$tbdLibsVersion")
implementation("com.github.navikt.tbd-libs:retry:$tbdLibsVersion")
implementation("com.github.navikt.tbd-libs:speed-client:$tbdLibsVersion")

implementation("com.zaxxer:HikariCP:5.0.1")
implementation("org.flywaydb:flyway-database-postgresql:10.8.1")
Expand All @@ -36,6 +40,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:$junitJupiterVersion")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

testImplementation("io.mockk:mockk:$mockkVersion")
testImplementation("com.github.navikt.tbd-libs:rapids-and-rivers-test:$tbdLibsVersion")
testImplementation("com.github.navikt.tbd-libs:postgres-testdatabaser:$tbdLibsVersion")
}
Expand Down
5 changes: 5 additions & 0 deletions deploy/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ spec:
outbound:
rules:
- application: spurtedu
- application: speed-api
azure:
application:
enabled: true
tenant: trygdeetaten.no
gcp:
sqlInstances:
- type: POSTGRES_15
Expand Down
5 changes: 5 additions & 0 deletions deploy/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ spec:
outbound:
rules:
- application: spurtedu
- application: speed-api
azure:
application:
enabled: true
tenant: nav.no
gcp:
sqlInstances:
- type: POSTGRES_15
Expand Down
20 changes: 15 additions & 5 deletions src/main/kotlin/no/nav/helse/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.github.navikt.tbd_libs.azure.AzureToken
import com.github.navikt.tbd_libs.azure.AzureTokenProvider
import com.github.navikt.tbd_libs.azure.createAzureTokenClientFromEnvironment
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import com.github.navikt.tbd_libs.result_object.Result
import com.github.navikt.tbd_libs.speed.SpeedClient
import com.github.navikt.tbd_libs.spurtedu.SpurteDuClient
import io.micrometer.core.instrument.Clock
import io.micrometer.prometheusmetrics.PrometheusConfig
Expand All @@ -22,6 +24,7 @@ import no.nav.helse.ventetilstand.VedtaksperiodeVenterIkkeRiver
import no.nav.helse.ventetilstand.VedtaksperiodeVenterRiver
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.net.http.HttpClient
import javax.sql.DataSource

internal val objectMapper: ObjectMapper = jacksonObjectMapper()
Expand Down Expand Up @@ -53,20 +56,27 @@ fun main() {
val dataSourceBuilder = DataSourceBuilder(env.db)
val dataSource = dataSourceBuilder.getDataSource()

val azureClient = createAzureTokenClientFromEnvironment(env.raw)
val speedClient = SpeedClient(
httpClient = HttpClient.newHttpClient(),
objectMapper = objectMapper,
tokenProvider = azureClient
)
RapidApplication.create(env.raw, meterRegistry = meterRegistry)
.setupRivers(dataSource)
.setupRivers(dataSource, speedClient)
.setupMigration(dataSourceBuilder)
.start()
}

internal fun <T : RapidsConnection> T.setupRivers(
dataSource: DataSource,
speedClient: SpeedClient,
vedtaksperiodeVentetilstandDao: VedtaksperiodeVentetilstandDao = VedtaksperiodeVentetilstandDao(dataSource),
oppsummeringDao: OppsummeringDao = OppsummeringDao(dataSource),
spurteDuClient: SpurteDuClient = spurteDuClient()
) = apply {
AnnulleringRiver(this, dataSource)
GodkjenningLøsningRiver(this, dataSource)
GodkjenningLøsningRiver(this, dataSource, speedClient)
VedtaksperiodeTilGodkjenningRiver(this, dataSource)
VedtaksperiodeBehandletRiver(this, dataSource)
TidFraGodkjenningTilUtbetalingRiver(this, dataSource)
Expand All @@ -86,9 +96,9 @@ internal fun <T : RapidsConnection> T.setupRivers(
FunksjonellFeilOgVarselRiver(this, dataSource)
SendtSøknadRiver(this, dataSource)
OppgaveEndretRiver(this, dataSource)
VedtaksperiodeEndretRiver(this, dataSource)
VedtaksperiodeOpprettetRiver(this, dataSource)
VedtaksperiodeAvstemt(this, dataSource)
VedtaksperiodeEndretRiver(this, dataSource, speedClient)
VedtaksperiodeOpprettetRiver(this, dataSource, speedClient)
VedtaksperiodeAvstemt(this, dataSource, speedClient)
SkatteinntekterLagtTilGrunnRiver(this, dataSource)
UtkastTilVedtakRiver(this, dataSource)
}
Expand Down
18 changes: 14 additions & 4 deletions src/main/kotlin/no/nav/helse/GodkjenningLøsningRiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import com.fasterxml.jackson.databind.JsonNode
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers.asLocalDateTime
import com.github.navikt.tbd_libs.rapids_and_rivers.be
import com.github.navikt.tbd_libs.rapids_and_rivers.isMissingOrNull
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import com.github.navikt.tbd_libs.result_object.getOrThrow
import com.github.navikt.tbd_libs.retry.retryBlocking
import com.github.navikt.tbd_libs.speed.SpeedClient
import io.micrometer.core.instrument.Counter
import net.logstash.logback.argument.StructuredArguments.kv
import java.util.*
import javax.sql.DataSource

class GodkjenningLøsningRiver(rapid: RapidsConnection, private val dataSource: DataSource): River.PacketListener {
class GodkjenningLøsningRiver(
rapid: RapidsConnection,
private val dataSource: DataSource,
private val speedClient: SpeedClient
): River.PacketListener {
val godkjentCounter = Counter.builder("vedtaksperioder_godkjent")
.description("Antall godkjente vedtaksperioder")
.register(meterRegistry)
Expand All @@ -25,7 +33,6 @@ class GodkjenningLøsningRiver(rapid: RapidsConnection, private val dataSource:
it.require("@løsning.Godkjenning", ::tilLøsning)
it.requireKey(
"vedtaksperiodeId",
"aktørId",
"fødselsnummer",
"Godkjenning.periodetype",
"Godkjenning.inntektskilde",
Expand All @@ -44,12 +51,15 @@ class GodkjenningLøsningRiver(rapid: RapidsConnection, private val dataSource:
override fun onPacket(packet: JsonMessage, context: MessageContext) {
if (godkjenningAlleredeLagret(packet)) return

val ident = packet["fødselsnummer"].asText()
val behandlingId = UUID.fromString(packet["Godkjenning.behandlingId"].asText())

val identer = retryBlocking { speedClient.hentFødselsnummerOgAktørId(ident, behandlingId.toString()).getOrThrow() }

val behov = Godkjenningsbehov(
vedtaksperiodeId = UUID.fromString(packet["vedtaksperiodeId"].asText()),
fødselsnummer = packet["fødselsnummer"].asText(),
aktørId = packet["aktørId"].asText(),
fødselsnummer = identer.fødselsnummer,
aktørId = identer.aktørId,
periodetype = packet["Godkjenning.periodetype"].asText(),
inntektskilde = packet["Godkjenning.inntektskilde"].asText(),
utbetalingType = packet["Godkjenning.utbetalingtype"].asText(),
Expand Down
20 changes: 11 additions & 9 deletions src/main/kotlin/no/nav/helse/PersonAvstemtRiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.github.navikt.tbd_libs.rapids_and_rivers.asLocalDate
import com.github.navikt.tbd_libs.rapids_and_rivers.asLocalDateTime
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import com.github.navikt.tbd_libs.result_object.getOrThrow
import com.github.navikt.tbd_libs.retry.retryBlocking
import com.github.navikt.tbd_libs.speed.SpeedClient
import kotliquery.queryOf
import kotliquery.sessionOf
import no.nav.helse.Util.asUuid
Expand All @@ -17,16 +20,14 @@ import javax.sql.DataSource

class VedtaksperiodeAvstemt(
rapidApplication: RapidsConnection,
private val dataSource: DataSource
private val dataSource: DataSource,
private val speedClient: SpeedClient
) : River.PacketListener {
init {
River(rapidApplication).apply {
validate {
it.demandValue("@event_name", "person_avstemt")
it.requireKey(
"aktørId",
"fødselsnummer"
)
it.requireKey("@id", "fødselsnummer")
it.requireArray("arbeidsgivere") {
requireKey("organisasjonsnummer")
requireArray("vedtaksperioder") {
Expand All @@ -39,15 +40,16 @@ class VedtaksperiodeAvstemt(
}

override fun onPacket(packet: JsonMessage, context: MessageContext) {
val fnr = packet["fødselsnummer"].asText()
val aktørId = packet["aktørId"].asText()
val ident = packet["fødselsnummer"].asText()
val callId = packet["@id"].asText()
val identer = retryBlocking { speedClient.hentFødselsnummerOgAktørId(ident, callId).getOrThrow() }
val data: List<VedtaksperiodeData> = packet["arbeidsgivere"].flatMap { arbeidsgiver ->
val orgnr = arbeidsgiver["organisasjonsnummer"].asText()
arbeidsgiver["vedtaksperioder"].map { vedtaksperiode ->
VedtaksperiodeData(
id = vedtaksperiode["id"].asUuid(),
fnr = fnr,
aktørId = aktørId,
fnr = identer.fødselsnummer,
aktørId = identer.aktørId,
yrkesaktivitet = orgnr,
tilstand = vedtaksperiode["tilstand"].asText(),
fom = vedtaksperiode["fom"].asLocalDate(),
Expand Down
44 changes: 25 additions & 19 deletions src/main/kotlin/no/nav/helse/VedtaksperiodeEndretRiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers.asLocalDate
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import com.github.navikt.tbd_libs.result_object.getOrThrow
import com.github.navikt.tbd_libs.retry.retryBlocking
import com.github.navikt.tbd_libs.speed.SpeedClient
import kotliquery.queryOf
import kotliquery.sessionOf
import org.postgresql.util.PSQLException
Expand All @@ -15,14 +18,15 @@ import javax.sql.DataSource

class VedtaksperiodeEndretRiver(
rapidApplication: RapidsConnection,
private val dataSource: DataSource
private val dataSource: DataSource,
private val speedClient: SpeedClient
) : River.PacketListener {
init {
River(rapidApplication).apply {
validate {
it.demandValue("@event_name", "vedtaksperiode_endret")
it.requireKey(
"aktørId",
"@id",
"fødselsnummer",
"organisasjonsnummer",
"vedtaksperiodeId",
Expand All @@ -37,19 +41,20 @@ class VedtaksperiodeEndretRiver(
}

override fun onPacket(packet: JsonMessage, context: MessageContext) {
lagreVedtaksperiodedata(packet, dataSource)
lagreVedtaksperiodedata(speedClient, packet["gjeldendeTilstand"].asText(), packet, dataSource)
}
}
class VedtaksperiodeOpprettetRiver(
rapidApplication: RapidsConnection,
private val dataSource: DataSource
private val dataSource: DataSource,
private val speedClient: SpeedClient
) : River.PacketListener {
init {
River(rapidApplication).apply {
validate {
it.demandValue("@event_name", "vedtaksperiode_opprettet")
it.requireKey(
"aktørId",
"@id",
"fødselsnummer",
"organisasjonsnummer",
"vedtaksperiodeId",
Expand All @@ -63,22 +68,23 @@ class VedtaksperiodeOpprettetRiver(
}

override fun onPacket(packet: JsonMessage, context: MessageContext) {
lagreVedtaksperiodedata(packet, dataSource)
lagreVedtaksperiodedata(speedClient, "START", packet, dataSource)
}
}

private val minsteDato = LocalDate.of(-4500, 1, 1)

private fun lagreVedtaksperiodedata(packet: JsonMessage, dataSource: DataSource) {
val json = objectMapper.readTree(packet.toJson())
val vedtaksperiodeId = UUID.fromString(json["vedtaksperiodeId"].asText())
val aktørId = json["aktørId"].asText()
val fødselsnummer = json["fødselsnummer"].asText()
val yrkesaktivitet = json["organisasjonsnummer"].asText()
val fom = json["fom"].asLocalDate()
val tom = json["tom"].asLocalDate()
val skjæringstidspunkt = json["skjæringstidspunkt"].asLocalDate().coerceAtLeast(minsteDato)
val tilstand = json["gjeldendeTilstand"]?.asText() ?: "START"
private fun lagreVedtaksperiodedata(speedClient: SpeedClient, tilstand: String, packet: JsonMessage, dataSource: DataSource) {
val ident = packet["fødselsnummer"].asText()
val callId = packet["@id"].asText()

val identer = retryBlocking { speedClient.hentFødselsnummerOgAktørId(ident, callId).getOrThrow() }
val vedtaksperiodeId = UUID.fromString(packet["vedtaksperiodeId"].asText())

val yrkesaktivitet = packet["organisasjonsnummer"].asText()
val fom = packet["fom"].asLocalDate()
val tom = packet["tom"].asLocalDate()
val skjæringstidspunkt = packet["skjæringstidspunkt"].asLocalDate().coerceAtLeast(minsteDato)
try {
sessionOf(dataSource).use { session ->
val upsert = """
Expand All @@ -98,8 +104,8 @@ private fun lagreVedtaksperiodedata(packet: JsonMessage, dataSource: DataSource)
queryOf(
upsert, mapOf(
"vedtaksperiodeId" to vedtaksperiodeId,
"fnr" to fødselsnummer,
"aktorId" to aktørId,
"fnr" to identer.fødselsnummer,
"aktorId" to identer.aktørId,
"yrkesaktivitet" to yrkesaktivitet,
"fom" to fom,
"tom" to tom,
Expand All @@ -112,6 +118,6 @@ private fun lagreVedtaksperiodedata(packet: JsonMessage, dataSource: DataSource)
}
} catch (err: PSQLException) {
logg.warn("klarte ikke oppdatere vedtaksdata fra dump")
sikkerlogg.warn("klarte ikke oppdatere vedtaksdata fra dump; Error: $err \nPacket dump: $json")
sikkerlogg.warn("klarte ikke oppdatere vedtaksdata fra dump; Error: $err \nPacket dump: ${packet.toJson()}")
}
}
23 changes: 19 additions & 4 deletions src/test/kotlin/no/nav/helse/E2eTestApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.read.ListAppender
import com.github.navikt.tbd_libs.rapids_and_rivers.test_support.TestRapid
import com.github.navikt.tbd_libs.result_object.ok
import com.github.navikt.tbd_libs.speed.IdentResponse
import com.github.navikt.tbd_libs.speed.SpeedClient
import com.github.navikt.tbd_libs.test_support.CleanupStrategy
import com.github.navikt.tbd_libs.test_support.DatabaseContainers
import com.github.navikt.tbd_libs.test_support.TestDataSource
import io.mockk.every
import io.mockk.mockk
import no.nav.helse.TestData.toJson
import no.nav.helse.TestUtil.failOnExceptions
import org.slf4j.LoggerFactory

private val tables = "annullering,annullering_arsak,begrunnelse,flyway_schema_history,funksjonell_feil,godkjenning,godkjenning_overstyringer,godkjenningsbehov,godkjenningsbehov_losning,godkjenningsbehov_losning_begrunnelse,hendelse_ikke_håndtert_årsak,oppgave,oppgave_endret,regelverksvarsel,revurdering,revurdering_vedtaksperiode,schedule,skatteinntekter_lagt_til_grunn,soknad,soknad_haandtert,utkast_til_vedtak,varsel,vedtaksperiode_aktivitet,vedtaksperiode_data,vedtaksperiode_tilstandsendring,vedtaksperiode_venter,warning_for_godkjenning"
Expand All @@ -21,12 +25,23 @@ class E2eTestApp() {
private lateinit var testDataSource: TestDataSource
val dataSource get() = testDataSource.ds

val standardAktørId = "9999999999999"
val speedClient = mockk<SpeedClient> {
val innkommendefnr = mutableListOf<String>()
every { hentFødselsnummerOgAktørId(capture(innkommendefnr), any()) } answers {
IdentResponse(
fødselsnummer = innkommendefnr.last(),
aktørId = standardAktørId,
npid = null,
kilde = IdentResponse.KildeResponse.PDL
).ok()
}
}

private fun start() {
mockLog()
testDataSource = databaseContainer.nyTilkobling()
rapid.setupRivers(dataSource)
rapid.failOnExceptions()
rapid.start()
rapid.setupRivers(dataSource, speedClient)
}

fun Annullering.sendTilRapid() {
Expand Down
Loading

0 comments on commit 5b60cc5

Please sign in to comment.