From 190a4f0b87d61ae0e025b44efa226ca31ec0144f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20J=2E=20Over=C3=A5?= Date: Thu, 2 May 2024 09:27:46 +0200 Subject: [PATCH] chore: wrap mq processing in span --- build.gradle.kts | 10 +- src/main/kotlin/no/nav/syfo/Application.kt | 5 +- .../application/BlockingApplicationRunner.kt | 956 +++++++++--------- 3 files changed, 488 insertions(+), 483 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 35d616ca..a5ec7a31 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,6 +30,7 @@ val commonsCodecVersion = "1.16.1" val ktfmtVersion = "0.44" val snappyJavaVersion = "1.1.10.5" val jsonVersion = "20240303" +val opentelemetryVersion = "2.3.0" plugins { id("application") @@ -47,7 +48,7 @@ application { repositories { mavenCentral() - maven(url= "https://packages.confluent.io/maven/") + maven(url = "https://packages.confluent.io/maven/") maven { url = uri("https://github-package-registry-mirror.gc.nav.no/cached/maven-release") } @@ -91,6 +92,7 @@ dependencies { implementation("no.nav.helse.xml:kith-hodemelding:$syfoXmlCodegenVersion") implementation("no.nav.helse.xml:kith-apprec:$syfoXmlCodegenVersion") implementation("com.ibm.mq:com.ibm.mq.allclient:$ibmMqVersion") + implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:$opentelemetryVersion") constraints { implementation("org.json:json:$jsonVersion") { because("override transient from com.ibm.mq:com.ibm.mq.allclient") @@ -127,9 +129,9 @@ dependencies { tasks { shadowJar { -mergeServiceFiles { - setPath("META-INF/services/org.flywaydb.core.extensibility.Plugin") - } + mergeServiceFiles { + setPath("META-INF/services/org.flywaydb.core.extensibility.Plugin") + } archiveBaseName.set("app") archiveClassifier.set("") isZip64 = true diff --git a/src/main/kotlin/no/nav/syfo/Application.kt b/src/main/kotlin/no/nav/syfo/Application.kt index 7e05f578..397871ac 100644 --- a/src/main/kotlin/no/nav/syfo/Application.kt +++ b/src/main/kotlin/no/nav/syfo/Application.kt @@ -206,11 +206,10 @@ fun launchListeners( virusScanService, duplicationService, smtssClient, - ) - .run( - inputconsumer, + inputconsumer, backoutProducer, ) + .run() } } } diff --git a/src/main/kotlin/no/nav/syfo/application/BlockingApplicationRunner.kt b/src/main/kotlin/no/nav/syfo/application/BlockingApplicationRunner.kt index c70f19ea..d71619c1 100644 --- a/src/main/kotlin/no/nav/syfo/application/BlockingApplicationRunner.kt +++ b/src/main/kotlin/no/nav/syfo/application/BlockingApplicationRunner.kt @@ -1,5 +1,6 @@ package no.nav.syfo.application +import io.opentelemetry.instrumentation.annotations.WithSpan import java.io.StringReader import java.time.LocalDateTime import java.time.ZoneOffset @@ -81,6 +82,8 @@ import no.nav.syfo.vedlegg.model.BehandlerInfo import org.apache.kafka.clients.producer.KafkaProducer import org.slf4j.LoggerFactory import org.xml.sax.InputSource +import javax.jms.Message + val sikkerlogg = LoggerFactory.getLogger("securelog") @@ -101,236 +104,349 @@ class BlockingApplicationRunner( private val virusScanService: VirusScanService, private val duplicationService: DuplicationService, private val smtssClient: SmtssClient, + private val inputconsumer: MessageConsumer, + private val backoutProducer: MessageProducer, ) { - suspend fun run( - inputconsumer: MessageConsumer, - backoutProducer: MessageProducer, - ) { + suspend fun run() { wrapExceptions { loop@ while (applicationState.ready) { val message = inputconsumer.receive(1000) - var loggingMeta: LoggingMeta? = null if (message == null) { delay(100) continue } - try { - val inputMessageText = - when (message) { - is TextMessage -> message.text - else -> - throw RuntimeException( - "Incoming message needs to be a byte message or text message", - ) - } - INCOMING_MESSAGE_COUNTER.inc() - val requestLatency = REQUEST_TIME.startTimer() - val fellesformat = safeUnmarshal(inputMessageText) + processMqMessage(message) + } + } + } - val vedlegg = getVedlegg(fellesformat) - if (vedlegg.isNotEmpty()) { - SYKMELDING_VEDLEGG_COUNTER.inc() - removeVedleggFromFellesformat(fellesformat) - } - val fellesformatText = - when (vedlegg.isNotEmpty()) { - true -> fellesformatMarshaller.toString(fellesformat) - false -> inputMessageText - } - val receiverBlock = fellesformat.get() - val msgHead = fellesformat.get() - val legekontorOrgNr = - extractOrganisationNumberFromSender(fellesformat) - ?.id - ?.replace(" ", "") - ?.trim() - loggingMeta = - LoggingMeta( - mottakId = receiverBlock.ediLoggId, - orgNr = legekontorOrgNr, - msgId = msgHead.msgInfo.msgId, + @WithSpan + private suspend fun processMqMessage(message: Message) { + var loggingMeta: LoggingMeta? = null + try { + val inputMessageText = + when (message) { + is TextMessage -> message.text + else -> + throw RuntimeException( + "Incoming message needs to be a byte message or text message", ) - logger.info("Received message, {}", StructuredArguments.fields(loggingMeta)) + } + INCOMING_MESSAGE_COUNTER.inc() + val requestLatency = REQUEST_TIME.startTimer() + val fellesformat = safeUnmarshal(inputMessageText) + + val vedlegg = getVedlegg(fellesformat) + if (vedlegg.isNotEmpty()) { + SYKMELDING_VEDLEGG_COUNTER.inc() + removeVedleggFromFellesformat(fellesformat) + } + val fellesformatText = + when (vedlegg.isNotEmpty()) { + true -> fellesformatMarshaller.toString(fellesformat) + false -> inputMessageText + } + val receiverBlock = fellesformat.get() + val msgHead = fellesformat.get() + val legekontorOrgNr = + extractOrganisationNumberFromSender(fellesformat) + ?.id + ?.replace(" ", "") + ?.trim() + loggingMeta = + LoggingMeta( + mottakId = receiverBlock.ediLoggId, + orgNr = legekontorOrgNr, + msgId = msgHead.msgInfo.msgId, + ) + logger.info("Received message, {}", StructuredArguments.fields(loggingMeta)) + + val healthInformation = extractHelseOpplysningerArbeidsuforhet(fellesformat) + val ediLoggId = receiverBlock.ediLoggId + val sha256String = sha256hashstring(healthInformation) + val msgId = msgHead.msgInfo.msgId + + + val legekontorHerId = extractOrganisationHerNumberFromSender(fellesformat)?.id + val legekontorReshId = extractOrganisationRashNumberFromSender(fellesformat)?.id + val legekontorOrgName = + msgHead.msgInfo.sender.organisation.organisationName.replace( + "[^\\p{ASCII}]".toRegex(), + "", + ) + + + val partnerReferanse = receiverBlock.partnerReferanse + + val originaltPasientFnr = healthInformation.pasient.fodselsnummer.id + val erVirksomhetSykmelding = receiverBlock.ebService == "SykmeldingVirksomhet" + + val mottatDato = + receiverBlock.mottattDatotid + .toGregorianCalendar() + .toZonedDateTime() + .withZoneSameInstant(ZoneOffset.UTC) + .toLocalDateTime() + + val avsenderSystem = healthInformation.avsenderSystem.toAvsenderSystem() + + val sykmeldingId = UUID.randomUUID().toString() + + val rulesetVersion = healthInformation.regelSettVersjon + + val duplicateCheck = + DuplicateCheck( + sykmeldingId, + sha256String, + ediLoggId, + msgId, + mottatDato, + avsenderSystem.navn, + avsenderSystem.versjon, + legekontorOrgNr, + rulesetVersion, + ) + + logger.info( + "Extracted data, ready to make sync calls to get more data, {}", + StructuredArguments.fields(loggingMeta), + ) - val healthInformation = extractHelseOpplysningerArbeidsuforhet(fellesformat) - val ediLoggId = receiverBlock.ediLoggId - val sha256String = sha256hashstring(healthInformation) - val msgId = msgHead.msgInfo.msgId + sikkerlogg.info( + "fellesformat: $fellesformatText", + StructuredArguments.fields(loggingMeta), + ) + if (legekontorOrgNr == null) { + SYKMELDING_MISSNG_ORG_NUMBER_COUNTER.inc() + logger.info( + "Missing org number, from epj ${avsenderSystem.navn} {}", + StructuredArguments.fields(loggingMeta), + ) + } - val legekontorHerId = extractOrganisationHerNumberFromSender(fellesformat)?.id - val legekontorReshId = extractOrganisationRashNumberFromSender(fellesformat)?.id - val legekontorOrgName = - msgHead.msgInfo.sender.organisation.organisationName.replace( - "[^\\p{ASCII}]".toRegex(), - "", + val signaturFnr = + if (erVirksomhetSykmelding) { + logger.info( + "Mottatt virksomhetssykmelding, {}", + StructuredArguments.fields(loggingMeta), + ) + VIRKSOMHETSYKMELDING.inc() + val hpr = extractHpr(fellesformat)?.id + if (hpr == null) { + handleVirksomhetssykmeldingOgHprMangler( + loggingMeta, + fellesformat, + ediLoggId, + msgId, + msgHead, + env, + kafkaproducerApprec, + duplicationService, + duplicateCheck, ) + return + } + val formatedHpr = padHpr(hpr.trim())!! - val partnerReferanse = receiverBlock.partnerReferanse - - val originaltPasientFnr = healthInformation.pasient.fodselsnummer.id - val erVirksomhetSykmelding = receiverBlock.ebService == "SykmeldingVirksomhet" + val fnr = + norskHelsenettClient + .getByHpr(hprNummer = formatedHpr, loggingMeta = loggingMeta) + ?.fnr + if (fnr == null) { + handleVirksomhetssykmeldingOgFnrManglerIHPR( + loggingMeta, + fellesformat, + ediLoggId, + msgId, + msgHead, + env, + kafkaproducerApprec, + duplicationService, + duplicateCheck, + ) + return + } else { + fnr + } + } else { + receiverBlock.avsenderFnrFraDigSignatur + } - val mottatDato = - receiverBlock.mottattDatotid - .toGregorianCalendar() - .toZonedDateTime() - .withZoneSameInstant(ZoneOffset.UTC) - .toLocalDateTime() + val identer = + pdlPersonService.getIdenter( + listOf(signaturFnr, originaltPasientFnr), + loggingMeta, + ) + + val tssIdEmottak = + smtssClient.findBestTssIdEmottak( + signaturFnr, + legekontorOrgName, + loggingMeta, + sykmeldingId, + ) + + val tssIdInfotrygd = + if (!tssIdEmottak.isNullOrEmpty()) { + tssIdEmottak + } else { + smtssClient.findBestTssInfotrygdId( + signaturFnr, + legekontorOrgName, + loggingMeta, + sykmeldingId, + ) + } - val avsenderSystem = healthInformation.avsenderSystem.toAvsenderSystem() + logger.info( + "tssIdEmottak is $tssIdEmottak {}", + StructuredArguments.fields(loggingMeta), + ) + logger.info( + "tssIdInfotrygd is $tssIdInfotrygd {}", + StructuredArguments.fields(loggingMeta), + ) - val sykmeldingId = UUID.randomUUID().toString() + handleEmottakSubscription( + tssIdEmottak, + emottakSubscriptionClient, + msgHead, + msgId, + partnerReferanse, + loggingMeta, + ) - val rulesetVersion = healthInformation.regelSettVersjon + val duplicationCheckSha256String = + duplicationService.getDuplicationCheck(sha256String, ediLoggId) - val duplicateCheck = - DuplicateCheck( - sykmeldingId, - sha256String, - ediLoggId, - msgId, - mottatDato, - avsenderSystem.navn, - avsenderSystem.versjon, - legekontorOrgNr, - rulesetVersion, - ) + if (duplicationCheckSha256String != null) { + val duplicate = + Duplicate( + sykmeldingId, + ediLoggId, + msgId, + duplicationCheckSha256String.sykmeldingId, + mottatDato, + avsenderSystem.navn, + avsenderSystem.versjon, + legekontorOrgNr, + ) - logger.info( - "Extracted data, ready to make sync calls to get more data, {}", - StructuredArguments.fields(loggingMeta), + handleDuplicateSM2013Content( + duplicationCheckSha256String.mottakId, + loggingMeta, + fellesformat, + ediLoggId, + msgId, + msgHead, + env, + kafkaproducerApprec, + duplicationService, + duplicate, + ) + return + } else { + val pasient = identer[originaltPasientFnr] + val behandler = identer[signaturFnr] + + if ( + checkSM2013Content( + pasient, + behandler, + healthInformation, + originaltPasientFnr, + loggingMeta, + fellesformat, + ediLoggId, + msgId, + msgHead, + env, + kafkaproducerApprec, + duplicationService, + duplicateCheck, ) + ) { + return + } - sikkerlogg.info( - "fellesformat: $fellesformatText", - StructuredArguments.fields(loggingMeta), + val signerendeBehandler = + norskHelsenettClient.getByFnr( + fnr = signaturFnr, + loggingMeta = loggingMeta, ) - if (legekontorOrgNr == null) { - SYKMELDING_MISSNG_ORG_NUMBER_COUNTER.inc() - logger.info( - "Missing org number, from epj ${avsenderSystem.navn} {}", - StructuredArguments.fields(loggingMeta), + val behandlenedeBehandler = + if ( + extractFnrDnrFraBehandler(healthInformation) != null || + padHpr(extractHpr(fellesformat)?.id?.trim()) != null || + padHpr(extractHprBehandler(healthInformation)?.trim()) != null + ) { + getBehandlenedeBehandler( + padHpr(extractHprBehandler(healthInformation)?.trim()), + padHpr(extractHpr(fellesformat)?.id?.trim()), + padHpr(extractFnrDnrFraBehandler(healthInformation)?.trim()), + loggingMeta, ) + } else { + null } - val signaturFnr = - if (erVirksomhetSykmelding) { - logger.info( - "Mottatt virksomhetssykmelding, {}", - StructuredArguments.fields(loggingMeta), - ) - VIRKSOMHETSYKMELDING.inc() - val hpr = extractHpr(fellesformat)?.id - if (hpr == null) { - handleVirksomhetssykmeldingOgHprMangler( - loggingMeta, - fellesformat, - ediLoggId, - msgId, - msgHead, - env, - kafkaproducerApprec, - duplicationService, - duplicateCheck, - ) - continue@loop - } - - val formatedHpr = padHpr(hpr.trim())!! - - val fnr = - norskHelsenettClient - .getByHpr(hprNummer = formatedHpr, loggingMeta = loggingMeta) - ?.fnr - if (fnr == null) { - handleVirksomhetssykmeldingOgFnrManglerIHPR( - loggingMeta, - fellesformat, - ediLoggId, - msgId, - msgHead, - env, - kafkaproducerApprec, - duplicationService, - duplicateCheck, - ) - continue@loop - } else { - fnr - } - } else { - receiverBlock.avsenderFnrFraDigSignatur - } - - val identer = - pdlPersonService.getIdenter( - listOf(signaturFnr, originaltPasientFnr), - loggingMeta, + val behandlenedeBehandlerFnr = + extractFnrDnrFraBehandler(healthInformation) + ?: getBehandlerFnr( + avsenderHpr = padHpr(extractHpr(fellesformat)?.id?.trim()), + signerendeBehandler = signerendeBehandler, + behandlenedeBehandler = behandlenedeBehandler, ) - - val tssIdEmottak = - smtssClient.findBestTssIdEmottak( - signaturFnr, - legekontorOrgName, - loggingMeta, - sykmeldingId, + ?: signaturFnr + + val behandlenedeBehandlerhprNummer = + padHpr(extractHpr(fellesformat)?.id?.trim()) + ?: getBehandlerHprNr( + behandlerHpr = + padHpr(extractHprBehandler(healthInformation)?.trim()), + avsenderHpr = padHpr(extractHpr(fellesformat)?.id?.trim()), + behandlenedeBehandler = behandlenedeBehandler, ) - val tssIdInfotrygd = - if (!tssIdEmottak.isNullOrEmpty()) { - tssIdEmottak - } else { - smtssClient.findBestTssInfotrygdId( - signaturFnr, - legekontorOrgName, - loggingMeta, - sykmeldingId, - ) - } - - logger.info( - "tssIdEmottak is $tssIdEmottak {}", - StructuredArguments.fields(loggingMeta), + val sykmelding = + healthInformation.toSykmelding( + sykmeldingId = sykmeldingId, + pasientAktoerId = pasient?.aktorId!!, + legeAktoerId = behandler?.aktorId!!, + msgId = msgId, + signaturDato = getLocalDateTime(msgHead.msgInfo.genDate), + behandlerFnr = behandlenedeBehandlerFnr, + behandlerHprNr = behandlenedeBehandlerhprNummer, ) + if (originaltPasientFnr != pasient.fnr) { logger.info( - "tssIdInfotrygd is $tssIdInfotrygd {}", + "Sykmeldingen inneholder eldre ident for pasient, benytter nyeste fra PDL {}", StructuredArguments.fields(loggingMeta), ) - - handleEmottakSubscription( - tssIdEmottak, - emottakSubscriptionClient, - msgHead, - msgId, - partnerReferanse, - loggingMeta, + sikkerlogg.info( + "Sykmeldingen inneholder eldre ident for pasient, benytter nyeste fra PDL" + + "originaltPasientFnr: {}, pasientFnr: {}, {}", + originaltPasientFnr, + pasient.fnr, + StructuredArguments.fields(loggingMeta), ) + } - - // HERE IS THE WRON GCODE - val duplicationCheckSha256String = - duplicationService.getDuplicationCheck(sha256String, ediLoggId) - - if (duplicationCheckSha256String != null) { - val duplicate = - Duplicate( - sykmeldingId, - ediLoggId, - msgId, - duplicationCheckSha256String.sykmeldingId, - mottatDato, - avsenderSystem.navn, - avsenderSystem.versjon, - legekontorOrgNr, + if (vedlegg.isNotEmpty()) { + val vedleggOver300MegaByte = + vedlegg.filter { + fileSizeLagerThan300MegaBytes( + Base64.getMimeDecoder().decode(it.content.content), ) + } - handleDuplicateSM2013Content( - duplicationCheckSha256String.mottakId, + if (vedleggOver300MegaByte.isNotEmpty()) { + handleVedleggOver300MB( loggingMeta, fellesformat, ediLoggId, @@ -339,311 +455,199 @@ class BlockingApplicationRunner( env, kafkaproducerApprec, duplicationService, - duplicate, + duplicateCheck, ) - continue@loop - } else { - val pasient = identer[originaltPasientFnr] - val behandler = identer[signaturFnr] - - if ( - checkSM2013Content( - pasient, - behandler, - healthInformation, - originaltPasientFnr, - loggingMeta, - fellesformat, - ediLoggId, - msgId, - msgHead, - env, - kafkaproducerApprec, - duplicationService, - duplicateCheck, - ) - ) { - continue@loop - } + return + } - val signerendeBehandler = - norskHelsenettClient.getByFnr( - fnr = signaturFnr, - loggingMeta = loggingMeta, - ) + if (virusScanService.vedleggContainsVirus(vedlegg)) { + handleVedleggContainsVirus( + loggingMeta, + fellesformat, + ediLoggId, + msgId, + msgHead, + env, + kafkaproducerApprec, + duplicationService, + duplicateCheck, + ) + return + } + } - val behandlenedeBehandler = - if ( - extractFnrDnrFraBehandler(healthInformation) != null || - padHpr(extractHpr(fellesformat)?.id?.trim()) != null || - padHpr(extractHprBehandler(healthInformation)?.trim()) != null - ) { - getBehandlenedeBehandler( - padHpr(extractHprBehandler(healthInformation)?.trim()), - padHpr(extractHpr(fellesformat)?.id?.trim()), - padHpr(extractFnrDnrFraBehandler(healthInformation)?.trim()), - loggingMeta, - ) - } else { - null - } - - val behandlenedeBehandlerFnr = - extractFnrDnrFraBehandler(healthInformation) - ?: getBehandlerFnr( - avsenderHpr = padHpr(extractHpr(fellesformat)?.id?.trim()), - signerendeBehandler = signerendeBehandler, - behandlenedeBehandler = behandlenedeBehandler, - ) - ?: signaturFnr - - val behandlenedeBehandlerhprNummer = - padHpr(extractHpr(fellesformat)?.id?.trim()) - ?: getBehandlerHprNr( - behandlerHpr = - padHpr(extractHprBehandler(healthInformation)?.trim()), - avsenderHpr = padHpr(extractHpr(fellesformat)?.id?.trim()), - behandlenedeBehandler = behandlenedeBehandler, - ) - - val sykmelding = - healthInformation.toSykmelding( - sykmeldingId = sykmeldingId, - pasientAktoerId = pasient?.aktorId!!, - legeAktoerId = behandler?.aktorId!!, - msgId = msgId, - signaturDato = getLocalDateTime(msgHead.msgInfo.genDate), - behandlerFnr = behandlenedeBehandlerFnr, - behandlerHprNr = behandlenedeBehandlerhprNummer, - ) - if (originaltPasientFnr != pasient.fnr) { - logger.info( - "Sykmeldingen inneholder eldre ident for pasient, benytter nyeste fra PDL {}", - StructuredArguments.fields(loggingMeta), - ) - sikkerlogg.info( - "Sykmeldingen inneholder eldre ident for pasient, benytter nyeste fra PDL" + - "originaltPasientFnr: {}, pasientFnr: {}, {}", - originaltPasientFnr, - pasient.fnr, - StructuredArguments.fields(loggingMeta), - ) - } - if (vedlegg.isNotEmpty()) { - val vedleggOver300MegaByte = - vedlegg.filter { - fileSizeLagerThan300MegaBytes( - Base64.getMimeDecoder().decode(it.content.content), - ) - } - - if (vedleggOver300MegaByte.isNotEmpty()) { - handleVedleggOver300MB( - loggingMeta, - fellesformat, - ediLoggId, - msgId, - msgHead, - env, - kafkaproducerApprec, - duplicationService, - duplicateCheck, - ) - continue@loop - } - - if (virusScanService.vedleggContainsVirus(vedlegg)) { - handleVedleggContainsVirus( - loggingMeta, - fellesformat, - ediLoggId, - msgId, - msgHead, - env, - kafkaproducerApprec, - duplicationService, - duplicateCheck, - ) - continue@loop - } - } + if (sykmelding.signaturDato.isAfter(LocalDateTime.now())) { + handleSignaturDatoInTheFuture( + loggingMeta, + fellesformat, + ediLoggId, + msgId, + msgHead, + env, + kafkaproducerApprec, + duplicationService, + duplicateCheck, + ) + return + } + val vedleggListe: List = + if (vedlegg.isNotEmpty()) { + bucketUploadService.lastOppVedlegg( + vedlegg = vedlegg, + msgId = msgId, + personNrPasient = pasient.fnr!!, + behandlerInfo = + BehandlerInfo( + fornavn = sykmelding.behandler.fornavn, + etternavn = sykmelding.behandler.etternavn, + fnr = signaturFnr, + ), + pasientAktoerId = sykmelding.pasientAktoerId, + sykmeldingId = sykmelding.id, + loggingMeta = loggingMeta, + ) + } else { + emptyList() + } - if (sykmelding.signaturDato.isAfter(LocalDateTime.now())) { - handleSignaturDatoInTheFuture( - loggingMeta, - fellesformat, - ediLoggId, - msgId, - msgHead, - env, - kafkaproducerApprec, - duplicationService, - duplicateCheck, + val receivedSykmelding = + ReceivedSykmelding( + sykmelding = sykmelding, + personNrPasient = pasient.fnr!!, + tlfPasient = + extractTlfFromKontaktInfo( + healthInformation.pasient.kontaktInfo, + ), + personNrLege = signaturFnr, + navLogId = ediLoggId, + msgId = msgId, + legeHprNr = signerendeBehandler?.hprNummer, + legeHelsepersonellkategori = + signerendeBehandler?.godkjenninger?.let { + getHelsepersonellKategori( + it, ) - continue@loop - } + }, + legekontorOrgNr = legekontorOrgNr, + legekontorOrgName = legekontorOrgName, + legekontorHerId = legekontorHerId, + legekontorReshId = legekontorReshId, + mottattDato = mottatDato, + rulesetVersion = rulesetVersion, + fellesformat = fellesformatText, + tssid = tssIdInfotrygd ?: "", + merknader = null, + partnerreferanse = partnerReferanse, + vedlegg = vedleggListe, + utenlandskSykmelding = null, + ) - val vedleggListe: List = - if (vedlegg.isNotEmpty()) { - bucketUploadService.lastOppVedlegg( - vedlegg = vedlegg, - msgId = msgId, - personNrPasient = pasient.fnr!!, - behandlerInfo = - BehandlerInfo( - fornavn = sykmelding.behandler.fornavn, - etternavn = sykmelding.behandler.etternavn, - fnr = signaturFnr, - ), - pasientAktoerId = sykmelding.pasientAktoerId, - sykmeldingId = sykmelding.id, - loggingMeta = loggingMeta, - ) - } else { - emptyList() - } - - // ASSUMED OK HER - val receivedSykmelding = - ReceivedSykmelding( - sykmelding = sykmelding, - personNrPasient = pasient.fnr!!, - tlfPasient = - extractTlfFromKontaktInfo( - healthInformation.pasient.kontaktInfo, - ), - personNrLege = signaturFnr, - navLogId = ediLoggId, - msgId = msgId, - legeHprNr = signerendeBehandler?.hprNummer, - legeHelsepersonellkategori = - signerendeBehandler?.godkjenninger?.let { - getHelsepersonellKategori( - it, - ) - }, - legekontorOrgNr = legekontorOrgNr, - legekontorOrgName = legekontorOrgName, - legekontorHerId = legekontorHerId, - legekontorReshId = legekontorReshId, - mottattDato = mottatDato, - rulesetVersion = rulesetVersion, - fellesformat = fellesformatText, - tssid = tssIdInfotrygd ?: "", - merknader = null, - partnerreferanse = partnerReferanse, - vedlegg = vedleggListe, - utenlandskSykmelding = null, - ) + if (behandlenedeBehandlerFnr != signaturFnr) { + logUlikBehandler(loggingMeta) + } - if (behandlenedeBehandlerFnr != signaturFnr) { - logUlikBehandler(loggingMeta) - } + countNewDiagnoseCode(receivedSykmelding.sykmelding.medisinskVurdering) - countNewDiagnoseCode(receivedSykmelding.sykmelding.medisinskVurdering) + logger.info( + "Validating against rules, sykmeldingId {}, {}", + StructuredArguments.keyValue("sykmeldingId", sykmelding.id), + StructuredArguments.fields(loggingMeta), + ) + val validationResult = + syfoSykemeldingRuleClient.executeRuleValidation( + receivedSykmelding, + loggingMeta, + ) - logger.info( - "Validating against rules, sykmeldingId {}, {}", - StructuredArguments.keyValue("sykmeldingId", sykmelding.id), - StructuredArguments.fields(loggingMeta), + when (validationResult.status) { + Status.OK -> + handleStatusOK( + fellesformat = fellesformat, + ediLoggId = ediLoggId, + msgId = msgId, + msgHead = msgHead, + apprecTopic = env.apprecTopic, + kafkaproducerApprec = kafkaproducerApprec, + loggingMeta = loggingMeta, + okSykmeldingTopic = env.okSykmeldingTopic, + receivedSykmelding = receivedSykmelding, + kafkaproducerreceivedSykmelding = + kafkaproducerreceivedSykmelding, ) - val validationResult = - syfoSykemeldingRuleClient.executeRuleValidation( - receivedSykmelding, - loggingMeta, - ) - when (validationResult.status) { - Status.OK -> - handleStatusOK( - fellesformat = fellesformat, - ediLoggId = ediLoggId, - msgId = msgId, - msgHead = msgHead, - apprecTopic = env.apprecTopic, - kafkaproducerApprec = kafkaproducerApprec, - loggingMeta = loggingMeta, - okSykmeldingTopic = env.okSykmeldingTopic, - receivedSykmelding = receivedSykmelding, - kafkaproducerreceivedSykmelding = - kafkaproducerreceivedSykmelding, - ) - - Status.MANUAL_PROCESSING -> - handleStatusMANUALPROCESSING( - receivedSykmelding = receivedSykmelding, - loggingMeta = loggingMeta, - fellesformat = fellesformat, - ediLoggId = ediLoggId, - msgId = msgId, - msgHead = msgHead, - apprecTopic = env.apprecTopic, - kafkaproducerApprec = kafkaproducerApprec, - validationResult = validationResult, - kafkaManuelTaskProducer = kafkaManuelTaskProducer, - kafkaproducerreceivedSykmelding = - kafkaproducerreceivedSykmelding, - manuellBehandlingSykmeldingTopic = - env.manuellBehandlingSykmeldingTopic, - kafkaproducervalidationResult = kafkaproducervalidationResult, - behandlingsUtfallTopic = env.behandlingsUtfallTopic, - kafkaproducerManuellOppgave = kafkaproducerManuellOppgave, - syfoSmManuellTopic = env.syfoSmManuellTopic, - produserOppgaveTopic = env.produserOppgaveTopic, - ) - - Status.INVALID -> - handleStatusINVALID( - validationResult = validationResult, - kafkaproducerreceivedSykmelding = - kafkaproducerreceivedSykmelding, - kafkaproducervalidationResult = kafkaproducervalidationResult, - avvistSykmeldingTopic = env.avvistSykmeldingTopic, - receivedSykmelding = receivedSykmelding, - loggingMeta = loggingMeta, - fellesformat = fellesformat, - apprecTopic = env.apprecTopic, - behandlingsUtfallTopic = env.behandlingsUtfallTopic, - kafkaproducerApprec = kafkaproducerApprec, - ediLoggId = ediLoggId, - msgId = msgId, - msgHead = msgHead, - ) - } + Status.MANUAL_PROCESSING -> + handleStatusMANUALPROCESSING( + receivedSykmelding = receivedSykmelding, + loggingMeta = loggingMeta, + fellesformat = fellesformat, + ediLoggId = ediLoggId, + msgId = msgId, + msgHead = msgHead, + apprecTopic = env.apprecTopic, + kafkaproducerApprec = kafkaproducerApprec, + validationResult = validationResult, + kafkaManuelTaskProducer = kafkaManuelTaskProducer, + kafkaproducerreceivedSykmelding = + kafkaproducerreceivedSykmelding, + manuellBehandlingSykmeldingTopic = + env.manuellBehandlingSykmeldingTopic, + kafkaproducervalidationResult = kafkaproducervalidationResult, + behandlingsUtfallTopic = env.behandlingsUtfallTopic, + kafkaproducerManuellOppgave = kafkaproducerManuellOppgave, + syfoSmManuellTopic = env.syfoSmManuellTopic, + produserOppgaveTopic = env.produserOppgaveTopic, + ) - val currentRequestLatency = requestLatency.observeDuration() - - duplicationService.persistDuplicationCheck(duplicateCheck) - logger.info( - "Message got outcome {}, {}, processing took {}s, {}, {}", - StructuredArguments.keyValue("status", validationResult.status), - StructuredArguments.keyValue( - "ruleHits", - validationResult.ruleHits.joinToString(", ", "(", ")") { - it.ruleName - }, - ), - StructuredArguments.keyValue("latency", currentRequestLatency), - StructuredArguments.fields(loggingMeta), - StructuredArguments.keyValue("sykmeldingId", sykmeldingId), + Status.INVALID -> + handleStatusINVALID( + validationResult = validationResult, + kafkaproducerreceivedSykmelding = + kafkaproducerreceivedSykmelding, + kafkaproducervalidationResult = kafkaproducervalidationResult, + avvistSykmeldingTopic = env.avvistSykmeldingTopic, + receivedSykmelding = receivedSykmelding, + loggingMeta = loggingMeta, + fellesformat = fellesformat, + apprecTopic = env.apprecTopic, + behandlingsUtfallTopic = env.behandlingsUtfallTopic, + kafkaproducerApprec = kafkaproducerApprec, + ediLoggId = ediLoggId, + msgId = msgId, + msgHead = msgHead, ) - } - } catch (e: Exception) { - logger.error( - "Exception caught while handling message, sending to backout ${ - StructuredArguments.fields( - loggingMeta, - ) - }", - e, - ) - backoutProducer.send(message) - } finally { - message.acknowledge() } + + val currentRequestLatency = requestLatency.observeDuration() + + duplicationService.persistDuplicationCheck(duplicateCheck) + logger.info( + "Message got outcome {}, {}, processing took {}s, {}, {}", + StructuredArguments.keyValue("status", validationResult.status), + StructuredArguments.keyValue( + "ruleHits", + validationResult.ruleHits.joinToString(", ", "(", ")") { + it.ruleName + }, + ), + StructuredArguments.keyValue("latency", currentRequestLatency), + StructuredArguments.fields(loggingMeta), + StructuredArguments.keyValue("sykmeldingId", sykmeldingId), + ) } + } catch (e: Exception) { + logger.error( + "Exception caught while handling message, sending to backout ${ + StructuredArguments.fields( + loggingMeta, + ) + }", + e, + ) + backoutProducer.send(message) + } finally { + message.acknowledge() } }