-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Erstatt river for å markere forespørsel som besvart (#658)
- Loading branch information
Showing
4 changed files
with
87 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 50 additions & 66 deletions
116
...earbeidsgiver/inntektsmelding/forespoerselmarkerbesvart/MarkerForespoerselBesvartRiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,79 @@ | ||
package no.nav.helsearbeidsgiver.inntektsmelding.forespoerselmarkerbesvart | ||
|
||
import no.nav.helse.rapids_rivers.JsonMessage | ||
import no.nav.helse.rapids_rivers.MessageContext | ||
import no.nav.helse.rapids_rivers.RapidsConnection | ||
import no.nav.helse.rapids_rivers.River | ||
import kotlinx.serialization.json.JsonElement | ||
import no.nav.helsearbeidsgiver.felles.EventName | ||
import no.nav.helsearbeidsgiver.felles.Key | ||
import no.nav.helsearbeidsgiver.felles.json.krev | ||
import no.nav.helsearbeidsgiver.felles.json.les | ||
import no.nav.helsearbeidsgiver.felles.json.toMap | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.demandValues | ||
import no.nav.helsearbeidsgiver.felles.json.toPretty | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.pritopic.Pri | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.pritopic.PriProducer | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.rejectKeys | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.require | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.river.ObjectRiver | ||
import no.nav.helsearbeidsgiver.felles.utils.Log | ||
import no.nav.helsearbeidsgiver.utils.json.fromJson | ||
import no.nav.helsearbeidsgiver.utils.json.parseJson | ||
import no.nav.helsearbeidsgiver.utils.json.serializer.UuidSerializer | ||
import no.nav.helsearbeidsgiver.utils.json.toJson | ||
import no.nav.helsearbeidsgiver.utils.json.toPretty | ||
import no.nav.helsearbeidsgiver.utils.log.MdcUtils | ||
import no.nav.helsearbeidsgiver.utils.log.logger | ||
import no.nav.helsearbeidsgiver.utils.log.sikkerLogger | ||
import java.util.UUID | ||
|
||
data class Melding( | ||
val eventName: EventName, | ||
val transaksjonId: UUID, | ||
val forespoerselId: UUID, | ||
) | ||
|
||
class MarkerForespoerselBesvartRiver( | ||
rapid: RapidsConnection, | ||
private val priProducer: PriProducer, | ||
) : River.PacketListener { | ||
) : ObjectRiver<Melding>() { | ||
private val logger = logger() | ||
private val sikkerLogger = sikkerLogger() | ||
|
||
init { | ||
River(rapid) | ||
.validate { msg -> | ||
msg.demandValues( | ||
Key.EVENT_NAME to EventName.INNTEKTSMELDING_MOTTATT.name, | ||
) | ||
msg.require( | ||
Key.UUID to { it.fromJson(UuidSerializer) }, | ||
Key.FORESPOERSEL_ID to { it.fromJson(UuidSerializer) }, | ||
) | ||
msg.rejectKeys( | ||
Key.BEHOV, | ||
Key.DATA, | ||
Key.FAIL, | ||
) | ||
}.register(this) | ||
} | ||
|
||
override fun onPacket( | ||
packet: JsonMessage, | ||
context: MessageContext, | ||
) { | ||
if (packet[Key.FORESPOERSEL_ID.str].asText().isEmpty()) { | ||
logger.warn("Mangler forespørselId!") | ||
sikkerLogger.warn("Mangler forespørselId!") | ||
override fun les(json: Map<Key, JsonElement>): Melding? = | ||
if (setOf(Key.BEHOV, Key.DATA, Key.FAIL).any(json::containsKey)) { | ||
null | ||
} else { | ||
Melding( | ||
eventName = Key.EVENT_NAME.krev(EventName.INNTEKTSMELDING_MOTTATT, EventName.serializer(), json), | ||
transaksjonId = Key.UUID.les(UuidSerializer, json), | ||
forespoerselId = Key.FORESPOERSEL_ID.les(UuidSerializer, json), | ||
) | ||
} | ||
val json = packet.toJson().parseJson() | ||
|
||
MdcUtils.withLogFields( | ||
Log.klasse(this), | ||
Log.event(EventName.INNTEKTSMELDING_MOTTATT), | ||
) { | ||
logger.info("Mottok melding om ${EventName.INNTEKTSMELDING_MOTTATT}.") | ||
sikkerLogger.info("Mottok melding:\n${json.toPretty()}.") | ||
override fun Melding.haandter(json: Map<Key, JsonElement>): Map<Key, JsonElement>? { | ||
logger.info("Mottok melding om ${EventName.INNTEKTSMELDING_MOTTATT}.") | ||
sikkerLogger.info("Mottok melding:\n${json.toPretty()}.") | ||
|
||
val jsonMap = json.toMap() | ||
val publisert = | ||
priProducer | ||
.send( | ||
Pri.Key.NOTIS to Pri.NotisType.FORESPOERSEL_BESVART_SIMBA.toJson(Pri.NotisType.serializer()), | ||
Pri.Key.FORESPOERSEL_ID to forespoerselId.toJson(), | ||
).getOrThrow() | ||
|
||
val transaksjonId = Key.UUID.les(UuidSerializer, jsonMap) | ||
val forespoerselId = Key.FORESPOERSEL_ID.les(UuidSerializer, jsonMap) | ||
logger.info("Publiserte melding på pri-topic om ${Pri.NotisType.FORESPOERSEL_BESVART_SIMBA}.") | ||
sikkerLogger.info("Publiserte melding på pri-topic:\n${publisert.toPretty()}") | ||
|
||
MdcUtils.withLogFields( | ||
Log.transaksjonId(transaksjonId), | ||
Log.forespoerselId(forespoerselId), | ||
) { | ||
sendMeldingOmBesvarelse(forespoerselId) | ||
} | ||
} | ||
return null | ||
} | ||
|
||
private fun sendMeldingOmBesvarelse(forespoerselId: UUID) { | ||
priProducer | ||
.send( | ||
Pri.Key.NOTIS to Pri.NotisType.FORESPOERSEL_BESVART_SIMBA.toJson(Pri.NotisType.serializer()), | ||
Pri.Key.FORESPOERSEL_ID to forespoerselId.toJson(), | ||
).onSuccess { | ||
logger.info("Publiserte melding på pri-topic om ${Pri.NotisType.FORESPOERSEL_BESVART_SIMBA}.") | ||
sikkerLogger.info("Publiserte melding på pri-topic:\n${it.toPretty()}") | ||
}.onFailure { | ||
logger.error("Klarte ikke publiserte melding på pri-topic om ${Pri.NotisType.FORESPOERSEL_BESVART_SIMBA}.") | ||
} | ||
override fun Melding.haandterFeil( | ||
json: Map<Key, JsonElement>, | ||
error: Throwable, | ||
): Map<Key, JsonElement>? { | ||
"Klarte ikke publiserte melding på pri-topic om ${Pri.NotisType.FORESPOERSEL_BESVART_SIMBA}.".also { | ||
logger.error(it) | ||
sikkerLogger().error(it) | ||
} | ||
|
||
return null | ||
} | ||
|
||
override fun Melding.loggfelt(): Map<String, String> = | ||
mapOf( | ||
Log.klasse(this@MarkerForespoerselBesvartRiver), | ||
Log.event(eventName), | ||
Log.transaksjonId(transaksjonId), | ||
Log.forespoerselId(forespoerselId), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters