Skip to content

Commit

Permalink
Henter nå filtere fra databasen, og endepunkter for å administrere fi…
Browse files Browse the repository at this point in the history
…lter.
  • Loading branch information
frodeli committed Oct 21, 2024
1 parent 96a7b08 commit fa07ef0
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 57 deletions.
2 changes: 1 addition & 1 deletion api-kontrakt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val jacksonVersion = "2.18.0"

dependencies {
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")

api("no.nav:ktor-openapi-generator:1.0.46")
}

java {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package no.nav.aap.oppgave.filter

import no.nav.aap.oppgave.verdityper.Behandlingstype
import java.time.LocalDateTime

data class FilterDto(
val id: Long,
val navn: String,
val id: Long? = null,
val beskrivelse: String,
val avklaringsbehovKoder: Set<String> = emptySet(),
val behandlingstyper: Set<Behandlingstype> = emptySet()
val behandlingstyper: Set<Behandlingstype> = emptySet(),
val opprettetAv: String,
val opprettetTidspunkt: LocalDateTime,
val endretAv: String? = null,
val endretTidspunkt: LocalDateTime? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package no.nav.aap.oppgave.filter

import com.papsign.ktor.openapigen.annotations.parameters.PathParam

data class FilterId(@PathParam("filterId") val filterId: Long)
51 changes: 47 additions & 4 deletions app/src/main/kotlin/no/nav/aap/oppgave/filter/FilterAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,62 @@ package no.nav.aap.oppgave.filter

import com.papsign.ktor.openapigen.route.path.normal.NormalOpenAPIRoute
import com.papsign.ktor.openapigen.route.path.normal.get
import com.papsign.ktor.openapigen.route.path.normal.post
import com.papsign.ktor.openapigen.route.response.respond
import com.papsign.ktor.openapigen.route.route
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import no.nav.aap.komponenter.dbconnect.transaction
import no.nav.aap.oppgave.metrikker.httpCallCounter
import no.nav.aap.oppgave.server.authenticate.ident
import java.time.LocalDateTime
import javax.sql.DataSource

fun NormalOpenAPIRoute.filterApi(dataSource: DataSource, prometheus: PrometheusMeterRegistry) =
fun NormalOpenAPIRoute.hentFilterApi(dataSource: DataSource, prometheus: PrometheusMeterRegistry) =

route("/filter").get<Unit, List<FilterDto>> { _ ->
route("/filter").get<Unit, List<FilterDto>> {
prometheus.httpCallCounter("/filter").increment()
val filterListe = dataSource.transaction(readOnly = true) { connection ->
FilterRepository(connection).hentAlleFilter()
FilterRepository(connection).hentAlle()
}
respond(filterListe)
}
}

fun NormalOpenAPIRoute.opprettEllerOppdaterFilterApi(dataSource: DataSource, prometheus: PrometheusMeterRegistry) =

route("/filter").post<Unit, FilterDto, FilterDto> { _, request ->
prometheus.httpCallCounter("/filter").increment()

val filterId = dataSource.transaction { connection ->
val filterRepo = FilterRepository(connection)

if (request.id != null) {
filterRepo.oppdater(OppdaterFilter(
id = request.id!!,
beskrivelse = request.beskrivelse,
avklaringsbehovtyper = request.avklaringsbehovKoder,
behandlingstyper = request.behandlingstyper,
endretAv = ident(),
endretTidspunkt = LocalDateTime.now(),
))
} else {
filterRepo.opprett(OpprettFilter(
beskrivelse = request.beskrivelse,
avklaringsbehovtyper = request.avklaringsbehovKoder,
behandlingstyper = request.behandlingstyper,
opprettetAv = ident(),
opprettetTidspunkt = LocalDateTime.now(),
))
}
}
respond(request.copy(id = filterId))
}

fun NormalOpenAPIRoute.slettFilterApi(dataSource: DataSource, prometheus: PrometheusMeterRegistry) =

route("/filter/{filterId}/slett").post<Unit, Unit, FilterId> { _, req ->
prometheus.httpCallCounter("/filter/{filterId}/slett").increment()
dataSource.transaction { connection ->
FilterRepository(connection).slettFilter(req.filterId)
}
respond(Unit)
}
252 changes: 223 additions & 29 deletions app/src/main/kotlin/no/nav/aap/oppgave/filter/FilterRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,229 @@ package no.nav.aap.oppgave.filter

import no.nav.aap.komponenter.dbconnect.DBConnection
import no.nav.aap.oppgave.verdityper.Behandlingstype
import no.nav.aap.postmottak.kontrakt.avklaringsbehov.Definisjon
import java.time.LocalDateTime

data class OpprettFilter(
val beskrivelse: String,
val avklaringsbehovtyper: Set<String> = emptySet(),
val behandlingstyper: Set<Behandlingstype> = emptySet(),
val opprettetAv: String,
val opprettetTidspunkt: LocalDateTime,
)

data class OppdaterFilter(
val id: Long,
val beskrivelse: String,
val avklaringsbehovtyper: Set<String> = emptySet(),
val behandlingstyper: Set<Behandlingstype> = emptySet(),
val endretAv: String? = null,
val endretTidspunkt: LocalDateTime? = null,
)

// Forløpig mock repository
class FilterRepository(private val connection: DBConnection) {

private val filterDtoMap = mapOf(
1L to FilterDto(1L, "Alle oppgaver"),
2L to FilterDto(2L, "Alle postmottak oppgaver",
avklaringsbehovKoder = Definisjon.entries.map {it.kode}.toSet()),
3L to FilterDto(3L, "Alle behandlingsflyt oppgaver",
avklaringsbehovKoder = no.nav.aap.behandlingsflyt.kontrakt.avklaringsbehov.Definisjon.entries.map {it.kode}.toSet()),
4L to FilterDto(4L, "Alle førstegangsbehandling oppgaver",
behandlingstyper = setOf(Behandlingstype.FØRSTEGANGSBEHANDLING)),
5L to FilterDto(5L, "Alle revurdering oppgaver",
behandlingstyper = setOf(Behandlingstype.REVURDERING)),
6L to FilterDto(6L, "Alle tilbakekreving oppgaver",
behandlingstyper = setOf(Behandlingstype.TILBAKEKREVING)),
7L to FilterDto(7L, "Alle klage oppgaver",
behandlingstyper = setOf(Behandlingstype.KLAGE)),
8L to FilterDto(8L, "Alle dokumenthåndtering oppgaver",
behandlingstyper = setOf(Behandlingstype.DOKUMENT_HÅNDTERING)),
)

fun hentFilter(filterId: Long): FilterDto? {
return filterDtoMap[filterId]
}

fun hentAlleFilter(): List<FilterDto> {
return filterDtoMap.values.toList()
}

}
fun hentAlle(): List<FilterDto> {
return hentFilter(null)
}

fun hent(filterId: Long): FilterDto? {
return hentFilter(filterId).firstOrNull()
}

fun opprett(filter: OpprettFilter): Long {
val filterId = opprettFilter(filter)
opprettFilterAvklaringsbehovtyper(filterId, filter.avklaringsbehovtyper)
opprettFilterBehandlingstyper(filterId, filter.behandlingstyper)
return filterId
}

fun oppdater(filter: OppdaterFilter): Long {
oppdaterFilter(filter)
slettFilterParametre(filter.id)
opprettFilterAvklaringsbehovtyper(filter.id, filter.avklaringsbehovtyper)
opprettFilterBehandlingstyper(filter.id, filter.behandlingstyper)
return filter.id
}

fun slettFilter(id: Long) {
connection.execute("UPDATE FILTER SET SLETTET = TRUE WHERE ID = ?") {
setParams {
setLong(1, id)
}
}
}

private fun oppdaterFilter(filter: OppdaterFilter) {
connection.execute("UPDATE FILTER SET BESKRIVELSE = ?, ENDRET_AV = ?, ENDRET_TIDSPUNKT = ? WHERE ID = ?") {
setParams {
setString(1, filter.beskrivelse)
setString(2, filter.endretAv)
setLocalDateTime(3, filter.endretTidspunkt)
setLong(4, filter.id)
}
}
}

private fun slettFilterParametre(filterId: Long) {
connection.execute("DELETE FROM FILTER_AVKLARINGSBEHOVTYPE WHERE FILTER_ID = ?") {
setParams { setLong(1, filterId) }
}
connection.execute("DELETE FROM FILTER_BEHANDLINGSTYPE WHERE FILTER_ID = ?") {
setParams { setLong(1, filterId) }
}
}

private fun opprettFilter(filter: OpprettFilter): Long {
val insertFilterSql = """
INSERT INTO FILTER (BESKRIVELSE, OPPRETTET_AV, OPPRETTET_TIDSPUNKT) VALUES (?, ?, ?)
""".trimIndent()

return connection.executeReturnKey(insertFilterSql) {
setParams {
setString(1, filter.beskrivelse)
setString(2, filter.opprettetAv)
setLocalDateTime(3, filter.opprettetTidspunkt)
}
}
}

private fun opprettFilterAvklaringsbehovtyper(filterId: Long, avklaringsbehovtyper: Set<String>) {
val insertFilterAvklaringsbehovtypeSql = """
INSERT INTO FILTER_AVKLARINGSBEHOVTYPE (FILTER_ID, AVKLARINGSBEHOVTYPE) VALUES (?, ?)
""".trimIndent()

connection.executeBatch(insertFilterAvklaringsbehovtypeSql, avklaringsbehovtyper) {
setParams {
setLong(1, filterId)
setString(2, it)
}
}
}

private fun opprettFilterBehandlingstyper(filterId: Long, behandlingstyper: Set<Behandlingstype>) {
val insertFilterAvklaringsbehovtypeSql = """
INSERT INTO FILTER_BEHANDLINGSTYPE (FILTER_ID, BEHANDLINGSTYPE) VALUES (?, ?)
""".trimIndent()

connection.executeBatch(insertFilterAvklaringsbehovtypeSql, behandlingstyper) {
setParams {
setLong(1, filterId)
setString(2, it.name)
}
}
}


private fun hentFilter(filterId: Long?): List<FilterDto> {
val filterIdClause = if (filterId != null) " AND ID = ?" else ""
val query = """
SELECT
ID, BESKRIVELSE, OPPRETTET_AV, OPPRETTET_TIDSPUNKT, ENDRET_AV, ENDRET_TIDSPUNKT
FROM
FILTER
WHERE
SLETTET = FALSE $filterIdClause
""".trimIndent()

val alleFilter = connection.queryList(query) {
setParams {
if (filterId != null) {
setLong(1, filterId)
}
}
setRowMapper { row ->
FilterDto(
id = row.getLong("ID"),
beskrivelse = row.getString("BESKRIVELSE"),
opprettetAv = row.getString("OPPRETTET_AV"),
opprettetTidspunkt = row.getLocalDateTime("OPPRETTET_TIDSPUNKT"),
endretAv = row.getStringOrNull("ENDRET_AV"),
endretTidspunkt = row.getLocalDateTimeOrNull("ENDRET_TIDSPUNKT"),
)
}
}
val alleFilterAvklaringsbehovtype = hentAlleFilterAvklaringsbehovtype(filterId)
val alleFilterBehandlingstyper = hentAlleFilterBehandlingstyper(filterId)
val alleFilterMedFelter = alleFilter.map { filter ->
filter.copy(
avklaringsbehovKoder = alleFilterAvklaringsbehovtype[filter.id] ?: emptySet(),
behandlingstyper = alleFilterBehandlingstyper[filter.id] ?: emptySet()
)
}
return alleFilterMedFelter
}

private fun hentAlleFilterAvklaringsbehovtype(filterId: Long?): Map<Long, Set<String>> {
val filterIdClause = if (filterId != null) " AND FILTER_ID = ?" else ""
val query = """
SELECT
F.ID AS FILTER_ID,
FA.AVKLARINGSBEHOVTYPE
FROM
FILTER F,
FILTER_AVKLARINGSBEHOVTYPE FA
WHERE
F.ID = FA.FILTER_ID AND
F.SLETTET = FALSE
$filterIdClause
""".trimIndent()

val alleAvklaringsbehovFiltre = connection.queryList(query) {
setParams {
if (filterId != null) {
setLong(1, filterId)
}
}
setRowMapper { row ->
Pair<Long, String>(row.getLong("FILTER_ID"), row.getString("AVKLARINGSBEHOVTYPE"))
}
}

return alleAvklaringsbehovFiltre
.groupBy { it.first }
.mapValues { entry ->
entry.value.map {
it.second
}.toSet()
}
}

private fun hentAlleFilterBehandlingstyper(filterId: Long?): Map<Long, Set<Behandlingstype>> {
val filterIdClause = if (filterId != null) " AND FILTER_ID = ?" else ""
val query = """
SELECT
F.ID AS FILTER_ID,
FB.BEHANDLINGSTYPE
FROM
FILTER F,
FILTER_BEHANDLINGSTYPE FB
WHERE
F.ID = FB.FILTER_ID AND
F.SLETTET = FALSE
$filterIdClause
""".trimIndent()

val alleBehandlingstypeFiltre = connection.queryList(query) {
setParams {
if (filterId != null) {
setLong(1, filterId)
}
}
setRowMapper { row ->
Pair<Long, Behandlingstype>(row.getLong("FILTER_ID"), Behandlingstype.valueOf(row.getString("BEHANDLINGSTYPE")))
}
}

return alleBehandlingstypeFiltre
.groupBy { it.first }
.mapValues { entry ->
entry.value.map {
it.second
}.toSet()
}
}

}



Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PlukkOppgaveService(val connection: DBConnection) {

fun plukkNesteOppgave(filterId: Long, ident: String, token: OidcToken, maksAntallForsøk: Int = 10): NesteOppgaveDto? {
val filterRepo = FilterRepository(connection)
val filter = filterRepo.hentFilter(filterId)
val filter = filterRepo.hent(filterId)
if (filter == null) {
throw IllegalArgumentException("Finner ikke filter med id: $filterId")
}
Expand Down
Loading

0 comments on commit fa07ef0

Please sign in to comment.