Skip to content

Commit

Permalink
Terminate med fail-objekt
Browse files Browse the repository at this point in the history
  • Loading branch information
bjerga committed Oct 19, 2023
1 parent 3354ccc commit fe4800c
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import no.nav.helsearbeidsgiver.felles.BehovType
import no.nav.helsearbeidsgiver.felles.DataFelt
import no.nav.helsearbeidsgiver.felles.EksternInntektsmelding
import no.nav.helsearbeidsgiver.felles.EventName
import no.nav.helsearbeidsgiver.felles.Fail
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.createFail
import no.nav.helsearbeidsgiver.felles.json.les
Expand Down Expand Up @@ -82,7 +83,7 @@ class SpinnService(
Log.forespoerselId(forespoerselId)
) {
sikkerLogger.info("Prosesserer transaksjon $transaction.")
if (transaction == Transaction.NEW) {
if (transaction is Transaction.New) {
rapid.publish(
Key.EVENT_NAME to event.toJson(),
Key.BEHOV to BehovType.HENT_EKSTERN_INNTEKTSMELDING.toJson(),
Expand Down Expand Up @@ -141,12 +142,9 @@ class SpinnService(
}
}

override fun terminate(message: JsonMessage) {
val json = message.toJsonMap()
val transaksjonId = Key.UUID.les(UuidSerializer, json)

override fun terminate(fail: Fail) {
MdcUtils.withLogFields(
Log.transaksjonId(transaksjonId)
Log.transaksjonId(fail.uuid.let(UUID::fromString))
) {
sikkerLogger.error("$event terminert.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ abstract class CompositeEventListener(open val redisStore: IRedisStore) : River.
val transaction: Transaction = determineTransactionState(packet)

when (transaction) {
Transaction.NEW -> {
is Transaction.New -> {
initialTransactionState(packet)
dispatchBehov(packet, transaction)
}
Transaction.IN_PROGRESS -> dispatchBehov(packet, transaction)
Transaction.FINALIZE -> finalize(packet)
Transaction.TERMINATE -> terminate(packet)
Transaction.NOT_ACTIVE -> return
is Transaction.InProgress -> dispatchBehov(packet, transaction)
is Transaction.Finalize -> finalize(packet)
is Transaction.Terminate -> terminate(transaction.fail)
is Transaction.NotActive -> return
}
}

Expand All @@ -53,7 +53,7 @@ abstract class CompositeEventListener(open val redisStore: IRedisStore) : River.
return when {
value.isNullOrEmpty() -> {
if (!isEventMelding(message)) {
Transaction.NOT_ACTIVE
Transaction.NotActive
} else {
val clientId = message[Key.CLIENT_ID.str]
.takeUnless(JsonNode::isMissingOrNull)
Expand All @@ -62,11 +62,11 @@ abstract class CompositeEventListener(open val redisStore: IRedisStore) : River.

redisStore.set(eventKey, clientId)

Transaction.NEW
Transaction.New
}
}
isDataCollected(transactionId) -> Transaction.FINALIZE
else -> Transaction.IN_PROGRESS
isDataCollected(transactionId) -> Transaction.Finalize
else -> Transaction.InProgress
}
}

Expand All @@ -93,11 +93,11 @@ abstract class CompositeEventListener(open val redisStore: IRedisStore) : River.

abstract fun dispatchBehov(message: JsonMessage, transaction: Transaction)
abstract fun finalize(message: JsonMessage)
abstract fun terminate(message: JsonMessage)
abstract fun terminate(fail: Fail)
open fun initialTransactionState(message: JsonMessage) {}

open fun onError(feil: Fail): Transaction {
return Transaction.TERMINATE
return Transaction.Terminate(feil)
}

fun withFailKanal(failKanalSupplier: (t: CompositeEventListener) -> FailKanal): CompositeEventListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package no.nav.helsearbeidsgiver.felles.rapidsrivers.composite

enum class Transaction {
NEW, IN_PROGRESS, FINALIZE, TERMINATE, NOT_ACTIVE
import no.nav.helsearbeidsgiver.felles.Fail

sealed class Transaction {
data object New : Transaction()
data object InProgress : Transaction()
data object Finalize : Transaction()
data object NotActive : Transaction()
data class Terminate(
val fail: Fail
) : Transaction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ class InnsendingService(
if (feil.behov == BehovType.VIRKSOMHET) {
val virksomhetKey = "${feil.uuid}${DataFelt.VIRKSOMHET}"
redisStore.set(virksomhetKey, "Ukjent virksomhet")
return Transaction.IN_PROGRESS
return Transaction.InProgress
} else if (feil.behov == BehovType.FULLT_NAVN) {
val arbeidstakerFulltnavnKey = "${feil.uuid}${DataFelt.ARBEIDSTAKER_INFORMASJON.str}"
val arbeidsgiverFulltnavnKey = "${feil.uuid}${DataFelt.ARBEIDSGIVER_INFORMASJON.str}"
redisStore.set(arbeidstakerFulltnavnKey, personIkkeFunnet().toJsonStr(PersonDato.serializer()))
redisStore.set(arbeidsgiverFulltnavnKey, personIkkeFunnet().toJsonStr(PersonDato.serializer()))
return Transaction.IN_PROGRESS
return Transaction.InProgress
}
return Transaction.TERMINATE
return Transaction.Terminate(feil)
}

override fun terminate(message: JsonMessage) {
redisStore.set(message[Key.UUID.str].asText(), message[Key.FAIL.str].asText())
override fun terminate(fail: Fail) {
redisStore.set(fail.uuid!!, fail.feilmelding)
}

override fun dispatchBehov(message: JsonMessage, transaction: Transaction) {
val uuid: String = message[Key.UUID.str].asText()
when (transaction) {
Transaction.NEW -> {
is Transaction.New -> {
logger.info("InnsendingService: emitting behov Virksomhet")
rapid.publish(
Key.EVENT_NAME to event.toJson(),
Expand All @@ -116,7 +116,7 @@ class InnsendingService(
)
}

Transaction.IN_PROGRESS -> {
is Transaction.InProgress -> {
if (isDataCollected(*step1data(message[Key.UUID.str].asText()))) {
val arbeidstakerRedis = redisStore.get(RedisKey.of(uuid, DataFelt.ARBEIDSTAKER_INFORMASJON), PersonDato::class.java)
val arbeidsgiverRedis = redisStore.get(RedisKey.of(uuid, DataFelt.ARBEIDSGIVER_INFORMASJON), PersonDato::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import no.nav.helsearbeidsgiver.felles.BehovType
import no.nav.helsearbeidsgiver.felles.DataFelt
import no.nav.helsearbeidsgiver.felles.EksternInntektsmelding
import no.nav.helsearbeidsgiver.felles.EventName
import no.nav.helsearbeidsgiver.felles.Fail
import no.nav.helsearbeidsgiver.felles.InnsendtInntektsmelding
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.rapidsrivers.DelegatingFailKanal
Expand Down Expand Up @@ -46,7 +47,7 @@ class KvitteringService(

override fun dispatchBehov(message: JsonMessage, transaction: Transaction) {
val transactionId: String = message[Key.UUID.str].asText()
if (transaction == Transaction.NEW) {
if (transaction is Transaction.New) {
val forespoerselId: String = message[Key.FORESPOERSEL_ID.str].asText()
logger.info("Sender event: ${event.name} for forespørsel $forespoerselId")
val msg = JsonMessage.newMessage(
Expand Down Expand Up @@ -77,10 +78,8 @@ class KvitteringService(
redisStore.set(clientId!!, im)
}

override fun terminate(message: JsonMessage) {
val transaksjonsId = message[Key.UUID.str].asText()
val forespoerselId = message[Key.FORESPOERSEL_ID.str].asText()
logger.info("Terminate kvittering med forespoerselId=$forespoerselId og transaksjonsId $transaksjonsId")
redisStore.set(transaksjonsId, message[Key.FAIL.str].asText())
override fun terminate(fail: Fail) {
logger.info("Terminate kvittering med forespoerselId=${fail.forespørselId} og transaksjonsId ${fail.uuid}")
redisStore.set(fail.uuid!!, fail.feilmelding)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class InntektService(
sikkerLogger.info("Prosesserer transaksjon $transaction.")

when (transaction) {
Transaction.NEW -> {
is Transaction.New -> {
rapid.publish(
Key.EVENT_NAME to event.toJson(),
Key.BEHOV to BehovType.HENT_TRENGER_IM.toJson(),
Expand All @@ -103,7 +103,7 @@ class InntektService(
}
}

Transaction.IN_PROGRESS -> {
is Transaction.InProgress -> {
val forspoerselKey = RedisKey.of(transaksjonId.toString(), DataFelt.FORESPOERSEL_SVAR)

if (isDataCollected(forspoerselKey)) {
Expand Down Expand Up @@ -173,27 +173,23 @@ class InntektService(
}
}

override fun terminate(message: JsonMessage) {
val json = message.toJsonMap()

val transaksjonId = Key.UUID.les(UuidSerializer, json)

val clientId = RedisKey.of(transaksjonId.toString(), event)
override fun terminate(fail: Fail) {
val clientId = RedisKey.of(fail.uuid!!, event)
.read()
?.let(UUID::fromString)
if (clientId == null) {
sikkerLogger.error("Forsøkte å terminere, men fant ikke clientID for transaksjon $transaksjonId i Redis")
logger.error("Forsøkte å terminere, men fant ikke clientID for transaksjon $transaksjonId i Redis")
sikkerLogger.error("Forsøkte å terminere, men fant ikke clientID for transaksjon ${fail.uuid} i Redis")
logger.error("Forsøkte å terminere, men fant ikke clientID for transaksjon ${fail.uuid} i Redis")
}
val feil = RedisKey.of(transaksjonId.toString(), Feilmelding("")).read()
val feil = RedisKey.of(fail.uuid!!, Feilmelding("")).read()

val feilResponse = InntektData(
feil = feil?.fromJson(FeilReport.serializer())
)
.toJson(InntektData.serializer())

RedisKey.of(clientId.toString()).write(feilResponse)
val logFields = loggFelterNotNull(transaksjonId, clientId)
val logFields = loggFelterNotNull(fail.uuid.let(UUID::fromString), clientId)
MdcUtils.withLogFields(
*logFields
) {
Expand All @@ -208,7 +204,7 @@ class InntektService(
BehovType.HENT_TRENGER_IM -> {
val feilmelding = Feilmelding("Teknisk feil, prøv igjen senere.", -1, DataFelt.FORESPOERSEL_SVAR)

feilmelding to Transaction.TERMINATE
feilmelding to Transaction.Terminate(feil)
}
BehovType.INNTEKT -> {
val feilmelding = Feilmelding(
Expand Down Expand Up @@ -239,7 +235,7 @@ class InntektService(
feilKey.write(feilReport)
}

return transaction ?: Transaction.IN_PROGRESS
return transaction ?: Transaction.InProgress
}

private fun publishFail(fail: Fail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class InntektServiceTest {
forespørselId = null
)
val transaction = service.onError(feil)
assertEquals(Transaction.IN_PROGRESS, transaction)
service.terminate(feil.toJsonMessage()) // skal ikke kaste exception..
assertEquals(Transaction.InProgress, transaction)
service.terminate(feil) // skal ikke kaste exception..
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ManuellOpprettSakService(private val rapidsConnection: RapidsConnection, o
override fun dispatchBehov(message: JsonMessage, transaction: Transaction) {
val transaksjonsId = message[Key.UUID.str].asText()
val forespoerselId = redisStore.get(transaksjonsId + Key.FORESPOERSEL_ID.str)!!
if (transaction == Transaction.NEW) {
if (transaction is Transaction.New) {
rapidsConnection.publish(
JsonMessage.newMessage(
mapOf(
Expand All @@ -65,7 +65,7 @@ class ManuellOpprettSakService(private val rapidsConnection: RapidsConnection, o
)
).toJson()
)
} else if (transaction == Transaction.IN_PROGRESS) {
} else if (transaction is Transaction.InProgress) {
val forespoersel = redisStore.get(transaksjonsId + DataFelt.FORESPOERSEL_SVAR.str)?.fromJson(TrengerInntekt.serializer())

if (forespoersel == null) {
Expand Down Expand Up @@ -147,13 +147,13 @@ class ManuellOpprettSakService(private val rapidsConnection: RapidsConnection, o
)
}

override fun terminate(message: JsonMessage) {
sikkerLogger.error("Terminerer flyt med transaksjon-ID '${message[Key.UUID.str].asText()}'")
override fun terminate(fail: Fail) {
sikkerLogger.error("Terminerer flyt med transaksjon-ID '${fail.uuid}'")
}

override fun onError(feil: Fail): Transaction {
sikkerLogger.error("Mottok feil:\n$feil")
return Transaction.TERMINATE
return Transaction.Terminate(feil)
}

private fun steg2(transactionId: String) = arrayOf(RedisKey.of(transactionId, DataFelt.FORESPOERSEL_SVAR))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class OpprettSak(private val rapidsConnection: RapidsConnection, override val re
override fun dispatchBehov(message: JsonMessage, transaction: Transaction) {
val transaksjonsId = message[Key.UUID.str].asText()
val forespørselId = redisStore.get(transaksjonsId + Key.FORESPOERSEL_ID.str)!!
if (transaction == Transaction.NEW) {
if (transaction is Transaction.New) {
rapidsConnection.publish(
JsonMessage.newMessage(
mapOf(
Expand All @@ -57,7 +57,7 @@ class OpprettSak(private val rapidsConnection: RapidsConnection, override val re
)
).toJson()
)
} else if (transaction == Transaction.IN_PROGRESS) {
} else if (transaction is Transaction.InProgress) {
if (isDataCollected(*steg3(transaksjonsId))) {
rapidsConnection.publish(
JsonMessage.newMessage(
Expand Down Expand Up @@ -102,17 +102,17 @@ class OpprettSak(private val rapidsConnection: RapidsConnection, override val re
)
}

override fun terminate(message: JsonMessage) {
redisStore.set(message[Key.UUID.str].asText(), message[Key.FAIL.str].asText())
override fun terminate(fail: Fail) {
redisStore.set(fail.uuid!!, fail.feilmelding)
}

override fun onError(feil: Fail): Transaction {
if (feil.behov == BehovType.FULLT_NAVN) {
val fulltNavnKey = "${feil.uuid}${DataFelt.ARBEIDSTAKER_INFORMASJON.str}"
redisStore.set(fulltNavnKey, PersonDato("Ukjent person", null, "").toJsonStr(PersonDato.serializer()))
return Transaction.IN_PROGRESS
return Transaction.InProgress
}
return Transaction.TERMINATE
return Transaction.Terminate(feil)
}

fun steg2(transactionId: String) = arrayOf(RedisKey.of(transactionId, DataFelt.ARBEIDSTAKER_INFORMASJON))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TilgangService(
sikkerLogger.info("Prosesserer transaksjon $transaction.")

when (transaction) {
Transaction.NEW -> {
is Transaction.New -> {
rapid.publish(
Key.EVENT_NAME to event.toJson(),
Key.BEHOV to BehovType.HENT_IM_ORGNR.toJson(),
Expand All @@ -95,7 +95,7 @@ class TilgangService(
}
}

Transaction.IN_PROGRESS -> {
is Transaction.InProgress -> {
val orgnrKey = RedisKey.of(transaksjonId.toString(), DataFelt.ORGNRUNDERENHET)

if (isDataCollected(orgnrKey)) {
Expand Down Expand Up @@ -168,28 +168,24 @@ class TilgangService(
}
}

override fun terminate(message: JsonMessage) {
val json = message.toJsonMap()

val transaksjonId = Key.UUID.les(UuidSerializer, json)

val clientId = RedisKey.of(transaksjonId.toString(), event)
override fun terminate(fail: Fail) {
val clientId = RedisKey.of(fail.uuid!!, event)
.read()
?.let(UUID::fromString)

val feil = RedisKey.of(transaksjonId.toString(), Feilmelding(""))
val feil = RedisKey.of(fail.uuid!!, Feilmelding(""))
.read()

val feilResponse = TilgangData(
feil = feil?.fromJson(FeilReport.serializer())
)
.toJson(TilgangData.serializer())
if (clientId == null) {
sikkerLogger.error("$event forsøkt terminert, kunne ikke finne $transaksjonId i redis!")
sikkerLogger.error("$event forsøkt terminert, kunne ikke finne ${fail.uuid} i redis!")
}
RedisKey.of(clientId.toString()).write(feilResponse)

val logFields = loggFelterNotNull(transaksjonId, clientId)
val logFields = loggFelterNotNull(fail.uuid!!.let(UUID::fromString), clientId)
MdcUtils.withLogFields(
*logFields
) {
Expand Down Expand Up @@ -224,7 +220,7 @@ class TilgangService(
feilKey.write(feilReport)
}

return Transaction.TERMINATE
return Transaction.Terminate(feil)
}

private fun RedisKey.write(json: JsonElement) {
Expand Down
Loading

0 comments on commit fe4800c

Please sign in to comment.