-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from navikt/v2api
Meldekort API v2
- Loading branch information
Showing
13 changed files
with
453 additions
and
21 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
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
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
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
156 changes: 156 additions & 0 deletions
156
src/main/kotlin/no/nav/meldeplikt/meldekortservice/api/MeldekortV2.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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package no.nav.meldeplikt.meldekortservice.api | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.locations.* | ||
import io.ktor.server.routing.* | ||
import no.nav.meldeplikt.meldekortservice.model.feil.NoContentException | ||
import no.nav.meldeplikt.meldekortservice.model.meldekort.Person | ||
import no.nav.meldeplikt.meldekortservice.model.meldekortdetaljer.Meldekortdetaljer | ||
import no.nav.meldeplikt.meldekortservice.service.ArenaOrdsService | ||
import no.nav.meldeplikt.meldekortservice.utils.* | ||
import no.nav.meldeplikt.meldekortservice.utils.swagger.* | ||
|
||
/** | ||
* REST-controller som henter FNR fra ident-header og tilbyr operasjoner for å hente: | ||
* - Meldekort | ||
* - Historiske meldekort | ||
* - Meldekortdetaljer | ||
* - Korrigert meldekort (ber Arena å opprette et nytt meldekort basert på meldekort med angitt ID og returnerer ny ID) | ||
*/ | ||
|
||
class Headers(val ident: String) | ||
|
||
@KtorExperimentalLocationsAPI | ||
fun Routing.meldekortApiV2(arenaOrdsService: ArenaOrdsService) { | ||
hentMeldekort(arenaOrdsService) | ||
hentHistoriskeMeldekort(arenaOrdsService) | ||
hentMeldekortdetaljer(arenaOrdsService) | ||
hentKorrigertMeldekort(arenaOrdsService) | ||
} | ||
|
||
private const val group = "Meldekort v2" | ||
|
||
@Group(group) | ||
@Location("$API_PATH/v2/meldekort") | ||
@KtorExperimentalLocationsAPI | ||
class HentMeldekortInput | ||
|
||
@KtorExperimentalLocationsAPI | ||
fun Routing.hentMeldekort(arenaOrdsService: ArenaOrdsService) = get<HentMeldekortInput>( | ||
"Hent meldekort".securityAndResponse( | ||
BearerTokenSecurity(), | ||
ok<Person>(), | ||
badRequest<ErrorMessage>(), | ||
serviceUnavailable<ErrorMessage>(), | ||
unAuthorized<Error>() | ||
).header<Headers>() | ||
) { | ||
respondOrError { | ||
val ident = getIdent(call) | ||
|
||
val response = arenaOrdsService.hentMeldekort(ident) | ||
if (response.status == HttpStatusCode.OK) { | ||
mapFraXml(response.content, Person::class.java) | ||
} else { | ||
throw NoContentException() | ||
} | ||
} | ||
} | ||
|
||
@Group(group) | ||
@Location("$API_PATH/v2/historiskemeldekort") | ||
@KtorExperimentalLocationsAPI | ||
class HentHistoriskeMeldekortInput(val antallMeldeperioder: Int) | ||
|
||
@KtorExperimentalLocationsAPI | ||
fun Routing.hentHistoriskeMeldekort(arenaOrdsService: ArenaOrdsService) = get<HentHistoriskeMeldekortInput>( | ||
"Hent tidligere/historiske meldekort".securityAndResponse( | ||
BearerTokenSecurity(), | ||
ok<Person>(), | ||
badRequest<ErrorMessage>(), | ||
serviceUnavailable<ErrorMessage>(), | ||
unAuthorized<Error>() | ||
).header<Headers>() | ||
) { hentHistoriskeMeldekortInput -> | ||
respondOrError { | ||
val ident = getIdent(call) | ||
|
||
arenaOrdsService.hentHistoriskeMeldekort( | ||
ident, | ||
hentHistoriskeMeldekortInput.antallMeldeperioder | ||
) | ||
} | ||
} | ||
|
||
@Group(group) | ||
@Location("$API_PATH/v2/meldekortdetaljer") | ||
@KtorExperimentalLocationsAPI | ||
data class HentMeldekortdetaljerInput(val meldekortId: Long) | ||
|
||
@KtorExperimentalLocationsAPI | ||
fun Routing.hentMeldekortdetaljer(arenaOrdsService: ArenaOrdsService) = | ||
get<HentMeldekortdetaljerInput>( | ||
"Hent meldekortdetaljer".securityAndResponse( | ||
BearerTokenSecurity(), | ||
ok<Meldekortdetaljer>(), | ||
badRequest<ErrorMessage>(), | ||
serviceUnavailable<ErrorMessage>(), | ||
unAuthorized<Error>() | ||
).header<Headers>() | ||
) { hentMeldekortdetaljerInput -> | ||
respondOrError { | ||
val ident = getIdent(call) | ||
|
||
val meldekortdetaljer = arenaOrdsService.hentMeldekortdetaljer(hentMeldekortdetaljerInput.meldekortId) | ||
if (meldekortdetaljer.meldegruppe == "") meldekortdetaljer.meldegruppe = "NULL" | ||
if (meldekortdetaljer.fodselsnr == ident) { | ||
meldekortdetaljer | ||
} else { | ||
val msg = "Personidentifikator matcher ikke. Bruker kan derfor ikke hente ut meldekortdetaljer." | ||
defaultLog.warn(msg) | ||
throw SecurityException(msg) | ||
} | ||
} | ||
} | ||
|
||
@Group(group) | ||
@Location("$API_PATH/v2/korrigertMeldekort") | ||
@KtorExperimentalLocationsAPI | ||
data class HentKorrigertMeldekortInput( | ||
val meldekortId: Long | ||
) | ||
|
||
@KtorExperimentalLocationsAPI | ||
fun Routing.hentKorrigertMeldekort(arenaOrdsService: ArenaOrdsService) = | ||
get<HentKorrigertMeldekortInput>( | ||
"Hent korrigert meldekortid".securityAndResponse( | ||
BearerTokenSecurity(), | ||
ok<String>(), | ||
badRequest<ErrorMessage>(), | ||
serviceUnavailable<ErrorMessage>(), | ||
unAuthorized<Error>() | ||
).header<Headers>() | ||
) { hentKorrigertMeldekortInput -> | ||
respondOrError { | ||
val ident = getIdent(call) | ||
|
||
val meldekortdetaljer = arenaOrdsService.hentMeldekortdetaljer(hentKorrigertMeldekortInput.meldekortId) | ||
if (meldekortdetaljer.fodselsnr != ident) { | ||
val msg = "Personidentifikator matcher ikke. Bruker kan derfor ikke korrigere meldekort." | ||
defaultLog.warn(msg) | ||
throw SecurityException() | ||
} | ||
|
||
arenaOrdsService.kopierMeldekort(hentKorrigertMeldekortInput.meldekortId) | ||
} | ||
} | ||
|
||
private fun getIdent(call: ApplicationCall): String { | ||
val ident = call.request.headers["ident"] | ||
if (ident.isNullOrBlank() || ident.length != 11) { | ||
throw SecurityException("Personidentifikator finnes ikke") | ||
} | ||
|
||
return ident | ||
} |
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
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
Oops, something went wrong.