Skip to content

Commit

Permalink
♻️ Noen justeringer i HelseDao og relatert kode
Browse files Browse the repository at this point in the history
  • Loading branch information
havstein committed Oct 23, 2024
1 parent 284a2f9 commit 5fbc194
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
13 changes: 6 additions & 7 deletions spesialist-felles/src/main/kotlin/no/nav/helse/HelseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class HelseDao(private val dataSource: DataSource) {
) = queryOf(sql, params.toMap())

// Plis bare bruk denne til ting det ikke går an å gjøre med navngitte parametere - eks. ".. AND orgnummer = ANY(?)"
fun asSQLForQuestionMarks(
fun asSQLWithQuestionMarks(
@Language("SQL") sql: String,
vararg params: Any?,
) = queryOf(sql, *params)
Expand Down Expand Up @@ -45,22 +45,21 @@ abstract class HelseDao(private val dataSource: DataSource) {
vararg params: Any?,
) = queryOf(sql, *params)

fun <T> Query.single(mapping: (Row) -> T?) =
sessionOf(dataSource, strict = true).use { session -> session.run(this.map { mapping(it) }.asSingle) }
fun <T> Query.single(mapping: (Row) -> T?) = sessionOf(dataSource, strict = true).use { single(it, mapping) }

fun <T> Query.single(
session: TransactionalSession,
mapping: (Row) -> T?,
) = session.run(this.map { mapping(it) }.asSingle)
) = with(Companion) { single(session, mapping) }

fun <T> Query.list(mapping: (Row) -> T?) = sessionOf(dataSource).use { session -> session.run(this.map { mapping(it) }.asList) }
fun <T> Query.list(mapping: (Row) -> T?) = sessionOf(dataSource).use { list(it, mapping) }

fun <T> Query.list(
session: TransactionalSession,
mapping: (Row) -> T?,
) = session.run(this.map { mapping(it) }.asList)
) = with(Companion) { list(session, mapping) }

fun Query.update() = sessionOf(dataSource).use { session -> session.run(this.asUpdate) }
fun Query.update() = sessionOf(dataSource).use { update(it) }

fun Query.updateAndReturnGeneratedKey() =
sessionOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ class AnnulleringDao(
return finnAnnullering(annulleringDto.arbeidsgiverFagsystemId, annulleringDto.personFagsystemId)
}
}

fun <T> Iterable<T>.somDbArray() = joinToString(prefix = "{", postfix = "}")
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,3 @@ class StansAutomatiskBehandlingDao(private val dataSource: DataSource) :
TransactionalStansAutomatiskBehandlingDao(session).hentFor(fødselsnummer)
}
}

fun <T> Iterable<T>.somDbArray() = joinToString(prefix = "{", postfix = "}")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.nav.helse.db

import kotliquery.Session
import no.nav.helse.HelseDao.Companion.asSQLForQuestionMarks
import no.nav.helse.HelseDao.Companion.asSQLWithQuestionMarks
import no.nav.helse.HelseDao.Companion.list
import no.nav.helse.modell.InntektskildeDto
import no.nav.helse.modell.InntektskildetypeDto
Expand Down Expand Up @@ -48,7 +48,7 @@ internal class TransactionalInntektskilderDao(

private fun eksisterendeInntektskilder(organisasjonsnumre: List<String>): List<InntektskildeDto> {
if (organisasjonsnumre.isEmpty()) return emptyList()
return asSQLForQuestionMarks(
return asSQLWithQuestionMarks(
"""
SELECT orgnummer, navn, bransjer, an.navn_oppdatert FROM arbeidsgiver ag
INNER JOIN arbeidsgiver_navn an on an.id = ag.navn_ref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package no.nav.helse.db
import kotliquery.Session
import kotliquery.queryOf
import no.nav.helse.HelseDao.Companion.asSQL
import no.nav.helse.HelseDao.Companion.asSQLForQuestionMarks
import no.nav.helse.HelseDao.Companion.asSQLWithQuestionMarks
import no.nav.helse.HelseDao.Companion.list
import no.nav.helse.spesialist.api.overstyring.OverstyringType
import org.intellij.lang.annotations.Language
Expand All @@ -13,7 +13,7 @@ class TransactionalOverstyringDao(
private val session: Session,
) : OverstyringRepository {
override fun finnOverstyringerMedTypeForVedtaksperioder(vedtaksperiodeIder: List<UUID>) =
asSQLForQuestionMarks(
asSQLWithQuestionMarks(
"""
SELECT DISTINCT o.id,
CASE
Expand All @@ -29,10 +29,10 @@ class TransactionalOverstyringDao(
LEFT JOIN overstyring_tidslinje ot on o.id = ot.overstyring_ref
LEFT JOIN skjonnsfastsetting_sykepengegrunnlag ss on o.id = ss.overstyring_ref
LEFT JOIN overstyring_minimum_sykdomsgrad oms on o.id = oms.overstyring_ref
WHERE o.vedtaksperiode_id IN (${vedtaksperiodeIder.joinToString { "?" }})
WHERE o.vedtaksperiode_id = ANY (?)
AND o.ferdigstilt = false
""".trimIndent(),
*vedtaksperiodeIder.toTypedArray(),
vedtaksperiodeIder.toTypedArray(),
).list(session) { OverstyringType.valueOf(it.string("type")) }

override fun finnOverstyringerMedTypeForVedtaksperiode(vedtaksperiodeId: UUID): List<OverstyringType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class TransactionalStansAutomatiskBehandlingDao(private val session: Session) :
val statement =
"""
insert into stans_automatisering (fødselsnummer, status, årsaker, opprettet, kilde, original_melding)
values (:fnr, :status, :arsaker::varchar[], :opprettet, :kilde, cast(:originalMelding as json))
values (:fnr, :status, :arsaker, :opprettet, :kilde, cast(:originalMelding as json))
""".trimIndent()
session.run(
queryOf(
statement,
mapOf(
"fnr" to fødselsnummer,
"status" to status,
"arsaker" to årsaker.map(StoppknappÅrsak::name).somDbArray(),
"arsaker" to session.createArrayOf("varchar", årsaker.map(StoppknappÅrsak::name)),
"opprettet" to opprettet,
"kilde" to kilde,
"originalMelding" to originalMelding,
Expand Down

0 comments on commit 5fbc194

Please sign in to comment.