Skip to content

Commit

Permalink
Legg til alternativt endepunkt for å hente forespoersel
Browse files Browse the repository at this point in the history
  • Loading branch information
bjerga committed Jun 19, 2024
1 parent f6ae2eb commit d344498
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 95 deletions.
2 changes: 1 addition & 1 deletion api/http/hentFraSpleis.http
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Hent fra Spleis / Helsebro
POST http://0.0.0.0:8080/api/v1/trenger
POST http://0.0.0.0:8080/api/v1/hent-forespoersel
Content-Type: application/json

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import no.nav.helse.rapids_rivers.RapidsConnection
import no.nav.helsearbeidsgiver.felles.rapidsrivers.registerShutdownLifecycle
import no.nav.helsearbeidsgiver.inntektsmelding.api.aktiveorgnr.aktiveOrgnrRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.auth.Tilgangskontroll
import no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel.hentForespoerselRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.hentselvbestemtim.hentSelvbestemtImRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.innsending.innsendingRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.inntekt.inntektRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.inntektselvbestemt.inntektSelvbestemtRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.kvittering.kvitteringRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.lagreselvbestemtim.lagreSelvbestemtImRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.tilgang.TilgangProducer
import no.nav.helsearbeidsgiver.inntektsmelding.api.trenger.trengerRoute
import no.nav.helsearbeidsgiver.utils.cache.LocalCache
import no.nav.helsearbeidsgiver.utils.json.jsonConfig
import no.nav.helsearbeidsgiver.utils.json.toJsonStr
Expand All @@ -41,6 +41,7 @@ val sikkerLogger = sikkerLogger()
object Routes {
const val PREFIX = "/api/v1"

const val HENT_FORESPOERSEL = "/hent-forespoersel"
const val TRENGER = "/trenger"
const val INNTEKT = "/inntekt"
const val INNTEKT_SELVBESTEMT = "/inntekt-selvbestemt"
Expand Down Expand Up @@ -111,7 +112,7 @@ fun Application.apiModule(

authenticate {
route(Routes.PREFIX) {
trengerRoute(rapid, tilgangskontroll, redisPoller)
hentForespoerselRoute(rapid, tilgangskontroll, redisPoller)
inntektRoute(rapid, tilgangskontroll, redisPoller)
inntektSelvbestemtRoute(rapid, tilgangskontroll, redisPoller)
innsendingRoute(rapid, tilgangskontroll, redisPoller)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.helsearbeidsgiver.inntektsmelding.api.trenger
package no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel

import no.nav.helse.rapids_rivers.RapidsConnection
import no.nav.helsearbeidsgiver.felles.EventName
Expand All @@ -10,11 +10,11 @@ import no.nav.helsearbeidsgiver.utils.json.toJson
import no.nav.helsearbeidsgiver.utils.json.toPretty
import java.util.UUID

class TrengerProducer(
class HentForespoerselProducer(
private val rapid: RapidsConnection
) {
init {
logger.info("Starter ${TrengerProducer::class.simpleName}...")
logger.info("Starter ${HentForespoerselProducer::class.simpleName}...")
}

fun publish(request: HentForespoerselRequest, arbeidsgiverFnr: String): UUID {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:UseSerializers(UuidSerializer::class)

package no.nav.helsearbeidsgiver.inntektsmelding.api.trenger
package no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel

import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:UseSerializers(LocalDateSerializer::class)

package no.nav.helsearbeidsgiver.inntektsmelding.api.trenger
package no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel

import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.helsearbeidsgiver.inntektsmelding.api.trenger
package no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel

import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
Expand Down Expand Up @@ -30,82 +30,84 @@ import no.nav.helsearbeidsgiver.inntektsmelding.api.validation.ValidationRespons
import no.nav.helsearbeidsgiver.utils.json.fromJson
import no.nav.helsearbeidsgiver.utils.json.toJson

fun Route.trengerRoute(
fun Route.hentForespoerselRoute(
rapid: RapidsConnection,
tilgangskontroll: Tilgangskontroll,
redisPoller: RedisPoller
) {
val trengerProducer = TrengerProducer(rapid)
val hentForespoerselProducer = HentForespoerselProducer(rapid)
val requestLatency = Summary.build()
.name("simba_trenger_latency_seconds")
.help("trenger endpoint latency in seconds")
.name("simba_hent_forespoersel_latency_seconds")
.help("hent forespoersel endpoint latency in seconds")
.register()

post(Routes.TRENGER) {
val requestTimer = requestLatency.startTimer()
runCatching {
receive(HentForespoerselRequest.serializer())
}
.onSuccess { request ->
logger.info("Henter data for uuid: ${request.uuid}")
try {
tilgangskontroll.validerTilgangTilForespoersel(call.request, request.uuid)
setOf(Routes.TRENGER, Routes.HENT_FORESPOERSEL).forEach { routeUrl ->
post(routeUrl) {
val requestTimer = requestLatency.startTimer()
runCatching {
receive(HentForespoerselRequest.serializer())
}
.onSuccess { request ->
logger.info("Henter data for uuid: ${request.uuid}")
try {
tilgangskontroll.validerTilgangTilForespoersel(call.request, request.uuid)

val arbeidsgiverFnr = call.request.lesFnrFraAuthToken()
val arbeidsgiverFnr = call.request.lesFnrFraAuthToken()

val transaksjonId = trengerProducer.publish(request, arbeidsgiverFnr)
val transaksjonId = hentForespoerselProducer.publish(request, arbeidsgiverFnr)

val resultatJson = redisPoller.hent(transaksjonId).fromJson(ResultJson.serializer())
val resultatJson = redisPoller.hent(transaksjonId).fromJson(ResultJson.serializer())

sikkerLogger.info("Hentet forespørsel: $resultatJson")
sikkerLogger.info("Hentet forespørsel: $resultatJson")

val resultat = resultatJson.success?.fromJson(HentForespoerselResultat.serializer())
if (resultat != null) {
respond(HttpStatusCode.Created, resultat.toResponse(), HentForespoerselResponse.serializer())
} else {
val feilmelding = resultatJson.failure?.fromJson(String.serializer()) ?: "Teknisk feil, prøv igjen senere."
val resultat = resultatJson.success?.fromJson(HentForespoerselResultat.serializer())
if (resultat != null) {
respond(HttpStatusCode.Created, resultat.toResponse(), HentForespoerselResponse.serializer())
} else {
val feilmelding = resultatJson.failure?.fromJson(String.serializer()) ?: "Teknisk feil, prøv igjen senere."
val response = ResultJson(
failure = feilmelding.toJson()
)
respond(HttpStatusCode.ServiceUnavailable, response, ResultJson.serializer())
}
} catch (e: ManglerAltinnRettigheterException) {
val response = ResultJson(
failure = "Du har ikke rettigheter for organisasjon.".toJson()
)
respondForbidden(response, ResultJson.serializer())
} catch (_: RedisPollerJsonParseException) {
logger.info("Fikk parsefeil for ${request.uuid}")
val response = ResultJson(
failure = feilmelding.toJson()
failure = RedisPermanentErrorResponse(request.uuid).toJson(RedisPermanentErrorResponse.serializer())
)
respond(HttpStatusCode.ServiceUnavailable, response, ResultJson.serializer())
respondInternalServerError(response, ResultJson.serializer())
} catch (_: RedisPollerTimeoutException) {
logger.info("Fikk timeout for ${request.uuid}")
val response = ResultJson(
failure = RedisTimeoutResponse(request.uuid).toJson(RedisTimeoutResponse.serializer())
)
respondInternalServerError(response, ResultJson.serializer())
}
} catch (e: ManglerAltinnRettigheterException) {
val response = ResultJson(
failure = "Du har ikke rettigheter for organisasjon.".toJson()
)
respondForbidden(response, ResultJson.serializer())
} catch (_: RedisPollerJsonParseException) {
logger.info("Fikk parsefeil for ${request.uuid}")
val response = ResultJson(
failure = RedisPermanentErrorResponse(request.uuid).toJson(RedisPermanentErrorResponse.serializer())
)
respondInternalServerError(response, ResultJson.serializer())
} catch (_: RedisPollerTimeoutException) {
logger.info("Fikk timeout for ${request.uuid}")
val response = ResultJson(
failure = RedisTimeoutResponse(request.uuid).toJson(RedisTimeoutResponse.serializer())
)
respondInternalServerError(response, ResultJson.serializer())
}.also {
requestTimer.observeDuration()
}
}.also {
requestTimer.observeDuration()
}
.onFailure {
logger.error("Klarte ikke lese request.", it)
val response = ResultJson(
failure = ValidationResponse(
listOf(
ValidationError(
property = HentForespoerselRequest::uuid.name,
error = it.message.orEmpty(),
value = "<ukjent>"
.onFailure {
logger.error("Klarte ikke lese request.", it)
val response = ResultJson(
failure = ValidationResponse(
listOf(
ValidationError(
property = HentForespoerselRequest::uuid.name,
error = it.message.orEmpty(),
value = "<ukjent>"
)
)
)
.toJson(ValidationResponse.serializer())
)
.toJson(ValidationResponse.serializer())
)
respondBadRequest(response, ResultJson.serializer())
}
respondBadRequest(response, ResultJson.serializer())
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AuthorizationTest : ApiTest() {
@Test
fun `stopp uautoriserte kall mot API`() = testApi {
listOf(
Routes.HENT_FORESPOERSEL to ::postUtenAuth,
Routes.TRENGER to ::postUtenAuth,
Routes.INNTEKT to ::postUtenAuth,
Routes.INNTEKT_SELVBESTEMT to ::postUtenAuth,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.helsearbeidsgiver.inntektsmelding.api.trenger
package no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.ints.shouldBeExactly
Expand All @@ -14,9 +14,9 @@ import no.nav.helsearbeidsgiver.utils.test.wrapper.genererGyldig
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr
import java.util.UUID

class TrengerProducerTest : FunSpec({
class HentForespoerselProducerTest : FunSpec({
val testRapid = TestRapid()
val producer = TrengerProducer(testRapid)
val producer = HentForespoerselProducer(testRapid)

test("publiserer melding på forventet format") {
val forespoerselId = UUID.randomUUID()
Expand Down
Loading

0 comments on commit d344498

Please sign in to comment.