Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Samle logikk for utsatt oppgave i service #374

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/syfo/koin/LocalKoinProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fun localDevConfig(config: ApplicationConfig) = module {
single { FeiletRepositoryImp(get()) } bind FeiletRepository::class
single { UtsattOppgaveRepositoryImp(get()) } bind UtsattOppgaveRepository::class

single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class
single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class

single {
JoarkHendelseKafkaClient(
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/syfo/koin/PreprodKoinProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fun preprodConfig(config: ApplicationConfig) = module {
config.getString("lagringstidMåneder").toInt()
)
} bind FjernInntektsmeldingByBehandletProcessor::class
single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class
single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class

single { FeiletService(FeiletRepositoryImp(get())) } bind FeiletService::class

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/syfo/koin/ProdKoinProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fun prodConfig(config: ApplicationConfig) = module {
config.getString("lagringstidMåneder").toInt()
)
} bind FjernInntektsmeldingByBehandletProcessor::class
single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class
single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class

single { FeiletService(FeiletRepositoryImp(get())) } bind FeiletService::class

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package no.nav.syfo.prosesser

import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import no.nav.helse.arbeidsgiver.utils.RecurringJob
import no.nav.helse.arbeidsgiver.utils.logger
import no.nav.syfo.behandling.OpprettOppgaveException
import no.nav.syfo.client.OppgaveClient
import no.nav.syfo.dto.Tilstand
import no.nav.syfo.repository.InntektsmeldingRepository
import no.nav.syfo.service.BehandlendeEnhetConsumer
import no.nav.syfo.util.MDCOperations
import no.nav.syfo.util.Metrikk
import no.nav.syfo.utsattoppgave.UtsattOppgaveDAO
import no.nav.syfo.utsattoppgave.opprettOppgaveIGosys
import no.nav.syfo.utsattoppgave.UtsattOppgaveService
import java.time.Duration
import java.time.LocalDateTime
import java.util.UUID

class FinnAlleUtgaandeOppgaverProcessor(
private val utsattOppgaveService: UtsattOppgaveService,
private val utsattOppgaveDAO: UtsattOppgaveDAO,
private val oppgaveClient: OppgaveClient,
private val behandlendeEnhetConsumer: BehandlendeEnhetConsumer,
private val metrikk: Metrikk,
private val inntektsmeldingRepository: InntektsmeldingRepository,
private val om: ObjectMapper
) : RecurringJob(CoroutineScope(Dispatchers.IO), Duration.ofHours(6).toMillis()) {
private val logger = this.logger()

Expand All @@ -36,7 +31,7 @@ class FinnAlleUtgaandeOppgaverProcessor(
try {
logger.info("Skal opprette oppgave for inntektsmelding: ${it.arkivreferanse}")
val inntektsmeldingEntitet = inntektsmeldingRepository.findByArkivReferanse(it.arkivreferanse)
opprettOppgaveIGosys(it, oppgaveClient, utsattOppgaveDAO, behandlendeEnhetConsumer, it.speil, inntektsmeldingEntitet, om)
utsattOppgaveService.opprettOppgaveIGosys(it, it.speil, inntektsmeldingEntitet)
it.tilstand = Tilstand.OpprettetTimeout
it.oppdatert = LocalDateTime.now()
metrikk.tellUtsattOppgave_OpprettTimeout()
Expand Down
5 changes: 0 additions & 5 deletions src/main/kotlin/no/nav/syfo/util/Metrikk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ class Metrikk {
"syfoinntektsmelding_utsatt_oppgave_utsett",
"Oppdaterte timeout for inntektsmelding", "type"
)
val UTSATT_OPPGAVE_UTSETT_UTEN_DATO = proseseringsMetrikker(
"syfoinntektsmelding_utsatt_oppgave_ingen_dato",
"Timeout på utsettelse mangler", "type"
)
val UTSATT_OPPGAVE_FORKAST = proseseringsMetrikker(
"syfoinntektsmelding_utsatt_oppgave_forkast",
"Forkaster oppgave", "type"
Expand Down Expand Up @@ -208,7 +204,6 @@ class Metrikk {

fun tellUtsattOppgave_Ukjent() = UTSATT_OPPGAVE_UKJENT.labels("info").inc()
fun tellUtsattOppgave_Utsett() = UTSATT_OPPGAVE_UTSETT.labels("info").inc()
fun tellUtsattOppgave_UtenDato() = UTSATT_OPPGAVE_UTSETT_UTEN_DATO.labels("info").inc()
fun tellUtsattOppgave_Forkast() = UTSATT_OPPGAVE_FORKAST.labels("info").inc()
fun tellUtsattOppgave_Opprett() = UTSATT_OPPGAVE_OPPRETT.labels("info").inc()
fun tellUtsattOppgave_OpprettTimeout() = UTSATT_OPPGAVE_OPPRETT_TIMEOUT.labels("info").inc()
Expand Down
154 changes: 86 additions & 68 deletions src/main/kotlin/no/nav/syfo/utsattoppgave/UtsattOppgaveService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import no.nav.syfo.repository.InntektsmeldingRepository
import no.nav.syfo.service.BehandlendeEnhetConsumer
import no.nav.syfo.service.SYKEPENGER_UTLAND
import no.nav.syfo.util.Metrikk
import org.slf4j.LoggerFactory
import java.time.LocalDateTime
import java.util.UUID

Expand All @@ -26,9 +25,43 @@ class UtsattOppgaveService(
private val om: ObjectMapper,
private val metrikk: Metrikk
) {

private val logger = this.logger()

fun opprettOppgaveIGosys(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Denne er bare flyttet på, men noen småforbedringer som ikke endrer funksjonalitet.

utsattOppgave: UtsattOppgaveEntitet,
speil: Boolean,
imEntitet: InntektsmeldingEntitet,
): OppgaveResultat {
val behandlendeEnhet = behandlendeEnhetConsumer.hentBehandlendeEnhet(
utsattOppgave.fnr,
utsattOppgave.inntektsmeldingId
)
val gjelderUtland = (SYKEPENGER_UTLAND == behandlendeEnhet)
val inntektsmelding = om.readValue<Inntektsmelding>(imEntitet.data!!)
val behandlingsTema = finnBehandlingsTema(inntektsmelding)
logger.info("Fant enhet $behandlendeEnhet for ${utsattOppgave.arkivreferanse}")
val resultat = runBlocking {
oppgaveClient.opprettOppgave(
journalpostId = utsattOppgave.journalpostId,
tildeltEnhetsnr = null,
aktoerId = utsattOppgave.aktørId,
gjelderUtland = gjelderUtland,
gjelderSpeil = speil,
tema = behandlingsTema
)
}

utsattOppgave
.apply {
enhet = behandlendeEnhet
gosysOppgaveId = resultat.oppgaveId.toString()
utbetalingBruker = resultat.utbetalingBruker
}
.lagre()

return resultat
}

fun prosesser(oppdatering: OppgaveOppdatering) {
val oppgave = utsattOppgaveDAO.finn(oppdatering.id.toString())
if (oppgave == null) {
Expand All @@ -39,84 +72,69 @@ class UtsattOppgaveService(
logger.info("Fant oppgave for inntektsmelding: ${oppgave.arkivreferanse} med tilstand: ${Tilstand.Forkastet.name}")
val gjelderSpeil = oppdatering.oppdateringstype == OppdateringstypeDTO.OpprettSpeilRelatert

if (oppgave.tilstand == Tilstand.Utsatt && oppdatering.handling == no.nav.syfo.utsattoppgave.Handling.Utsett) {
if (oppgave.timeout == null) {
metrikk.tellUtsattOppgave_UtenDato()
Comment on lines -43 to -44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sletter denne, da den aldri til oppstå. timeout er ikke nullable. Fjerner også tellUtsattOppgave_UtenDato-metrikken, da den kun ble brukt her.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timeout ER jo nullable:

class OppgaveOppdatering(
val id: UUID,
val handling: Handling,
val timeout: LocalDateTime?,
val oppdateringstype: OppdateringstypeDTO
)

return when {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Her bytter jeg bare if-ene med en when, og bruker piping der det føles naturlig.

oppgave.tilstand == Tilstand.Utsatt && oppdatering.handling == Handling.Utsett -> {
oppdatering.timeout ?: error("Timeout på utsettelse mangler for inntektsmelding: ${oppgave.arkivreferanse}")

oppgave
.apply {
timeout = oppdatering.timeout
oppdatert = LocalDateTime.now()
speil = gjelderSpeil
}
.lagre()

metrikk.tellUtsattOppgave_Utsett()
logger.info("Oppdaterte timeout på inntektsmelding: ${oppgave.arkivreferanse} til ${oppdatering.timeout}")
}
oppdatering.timeout ?: error("Timeout på utsettelse mangler for inntektsmelding: ${oppgave.arkivreferanse}")
oppgave.timeout = oppdatering.timeout
oppgave.oppdatert = LocalDateTime.now()
oppgave.speil = gjelderSpeil
lagre(oppgave)
metrikk.tellUtsattOppgave_Utsett()
logger.info("Oppdaterte timeout på inntektsmelding: ${oppgave.arkivreferanse} til ${oppdatering.timeout}")
return
}

if (oppgave.tilstand == Tilstand.Utsatt && oppdatering.handling == no.nav.syfo.utsattoppgave.Handling.Forkast) {
oppgave.oppdatert = LocalDateTime.now()
lagre(oppgave.copy(tilstand = Tilstand.Forkastet, speil = gjelderSpeil))
metrikk.tellUtsattOppgave_Forkast()
logger.info("Endret oppgave: ${oppgave.arkivreferanse} til tilstand: ${Tilstand.Forkastet.name}")
return
}
oppgave.tilstand == Tilstand.Utsatt && oppdatering.handling == Handling.Forkast -> {
oppgave
.apply {
oppdatert = LocalDateTime.now()
}
.copy(
tilstand = Tilstand.Forkastet,
speil = gjelderSpeil,
)
.lagre()
Comment on lines +92 to +100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeg forstår ikke helt hvorfor oppgave blir muterer oppdatert-feltet, for så å bruke den ikke-muterende copy-funksjonen 🤷


if ((oppgave.tilstand == Tilstand.Utsatt || oppgave.tilstand == Tilstand.Forkastet) && oppdatering.handling == Handling.Opprett) {
val inntektsmeldingEntitet = inntektsmeldingRepository.findByArkivReferanse(oppgave.arkivreferanse)
val resultat = opprettOppgaveIGosys(oppgave, oppgaveClient, utsattOppgaveDAO, behandlendeEnhetConsumer, gjelderSpeil, inntektsmeldingEntitet, om)
oppgave.oppdatert = LocalDateTime.now()
lagre(oppgave.copy(tilstand = Tilstand.Opprettet, speil = gjelderSpeil))
metrikk.tellUtsattOppgave_Opprett()
logger.info("Endret oppgave: ${oppgave.inntektsmeldingId} til tilstand: ${Tilstand.Opprettet.name} gosys oppgaveID: ${resultat.oppgaveId} duplikat? ${resultat.duplikat}")
return
}
metrikk.tellUtsattOppgave_Forkast()
logger.info("Endret oppgave: ${oppgave.arkivreferanse} til tilstand: ${Tilstand.Forkastet.name}")
}

metrikk.tellUtsattOppgave_Irrelevant()
logger.info("Oppdatering på dokumentId: ${oppdatering.id} ikke relevant")
}
oppgave.tilstand in listOf(Tilstand.Utsatt, Tilstand.Forkastet) && oppdatering.handling == Handling.Opprett -> {
val inntektsmeldingEntitet = inntektsmeldingRepository.findByArkivReferanse(oppgave.arkivreferanse)
val resultat = opprettOppgaveIGosys(oppgave, gjelderSpeil, inntektsmeldingEntitet)

oppgave
.apply {
oppdatert = LocalDateTime.now()
}
.copy(
tilstand = Tilstand.Opprettet,
speil = gjelderSpeil,
)
.lagre()

fun lagre(oppgave: UtsattOppgaveEntitet) {
utsattOppgaveDAO.lagre(oppgave)
metrikk.tellUtsattOppgave_Opprett()
logger.info("Endret oppgave: ${oppgave.inntektsmeldingId} til tilstand: ${Tilstand.Opprettet.name} gosys oppgaveID: ${resultat.oppgaveId} duplikat? ${resultat.duplikat}")
}

else -> {
metrikk.tellUtsattOppgave_Irrelevant()
logger.info("Oppdatering på dokumentId: ${oppdatering.id} ikke relevant")
}
}
}

fun opprett(utsattOppgave: UtsattOppgaveEntitet) {
utsattOppgaveDAO.opprett(utsattOppgave)
}
}

fun opprettOppgaveIGosys(
utsattOppgave: UtsattOppgaveEntitet,
oppgaveClient: OppgaveClient,
utsattOppgaveDAO: UtsattOppgaveDAO,
behandlendeEnhetConsumer: BehandlendeEnhetConsumer,
speil: Boolean,
imEntitet: InntektsmeldingEntitet,
om: ObjectMapper
): OppgaveResultat {
val logger = LoggerFactory.getLogger(UtsattOppgaveService::class.java)!!
val behandlendeEnhet = behandlendeEnhetConsumer.hentBehandlendeEnhet(
utsattOppgave.fnr,
utsattOppgave.inntektsmeldingId
)
val gjelderUtland = (SYKEPENGER_UTLAND == behandlendeEnhet)
val inntektsmelding = om.readValue<Inntektsmelding>(imEntitet.data!!)
val behandlingsTema = finnBehandlingsTema(inntektsmelding)
logger.info("Fant enhet $behandlendeEnhet for ${utsattOppgave.arkivreferanse}")
val resultat = runBlocking {
oppgaveClient.opprettOppgave(
journalpostId = utsattOppgave.journalpostId,
tildeltEnhetsnr = null,
aktoerId = utsattOppgave.aktørId,
gjelderUtland = gjelderUtland,
gjelderSpeil = speil,
tema = behandlingsTema
)
private fun UtsattOppgaveEntitet.lagre() {
utsattOppgaveDAO.lagre(this)
}
utsattOppgave.enhet = behandlendeEnhet
utsattOppgave.gosysOppgaveId = resultat.oppgaveId.toString()
utsattOppgave.utbetalingBruker = resultat.utbetalingBruker
utsattOppgaveDAO.lagre(utsattOppgave)
return resultat
}

class OppgaveOppdatering(
Expand Down