Skip to content

Commit

Permalink
♻️ Bytter til QueryRunner i DokumentDao og VarselDao
Browse files Browse the repository at this point in the history
  • Loading branch information
havstein committed Oct 26, 2024
1 parent 93a9a4a commit 4a977da
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal class SpesialistApp(
private val totrinnsvurderingDao = TotrinnsvurderingDao(dataSource)
private val snapshotApiDao = SnapshotApiDao(dataSource)
private val apiVarselRepository = ApiVarselRepository(dataSource)
private val dokumentDao = DokumentDao.NonTransactional(dataSource)
private val dokumentDao = DokumentDao(dataSource)
private valVentApiDao = PåVentApiDao(dataSource)
private val avviksvurderingDao = AvviksvurderingDao(dataSource)
private val stansAutomatiskBehandlingDao = StansAutomatiskBehandlingDao(dataSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal class MeldingMediator(
private val meldingDao: MeldingDao = MeldingDao(dataSource),
private val meldingDuplikatkontrollDao: MeldingDuplikatkontrollDao = MeldingDuplikatkontrollDao(dataSource),
private val kommandofabrikk: Kommandofabrikk,
private val dokumentDao: DokumentDaoInterface = DokumentDao.NonTransactional(dataSource),
private val dokumentDao: DokumentDaoInterface = DokumentDao(dataSource),
avviksvurderingDao: AvviksvurderingDao,
private val varselRepository: VarselRepository = VarselRepository(dataSource),
private val stansAutomatiskBehandlingMediator: StansAutomatiskBehandlingMediator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package no.nav.helse.modell.dokument

import com.fasterxml.jackson.databind.JsonNode
import kotliquery.Session
import kotliquery.sessionOf
import no.nav.helse.HelseDao.Companion.asSQL
import no.nav.helse.HelseDao.Companion.single
import no.nav.helse.HelseDao.Companion.update
import no.nav.helse.db.MedDataSource
import no.nav.helse.db.MedSession
import no.nav.helse.db.QueryRunner
import no.nav.helse.objectMapper
import java.util.UUID
import javax.sql.DataSource
Expand All @@ -25,29 +25,9 @@ internal interface DokumentDaoInterface {
fun slettGamleDokumenter(): Int
}

internal class DokumentDao(private val session: Session) : DokumentDaoInterface {
internal object NonTransactional {
operator fun invoke(dataSource: DataSource): DokumentDaoInterface {
fun <T> inSession(block: (Session) -> T) = sessionOf(dataSource).use { block(it) }

return object : DokumentDaoInterface {
override fun lagre(
dselsnummer: String,
dokumentId: UUID,
dokument: JsonNode,
) {
inSession { DokumentDao(it).lagre(fødselsnummer, dokumentId, dokument) }
}

override fun hent(
dselsnummer: String,
dokumentId: UUID,
) = inSession { DokumentDao(it).hent(fødselsnummer, dokumentId) }

override fun slettGamleDokumenter(): Int = inSession { DokumentDao(it).slettGamleDokumenter() }
}
}
}
internal class DokumentDao(queryRunner: QueryRunner) : DokumentDaoInterface, QueryRunner by queryRunner {
constructor(session: Session) : this(MedSession(session))
constructor(dataSource: DataSource) : this(MedDataSource(dataSource))

override fun lagre(
dselsnummer: String,
Expand All @@ -59,7 +39,7 @@ internal class DokumentDao(private val session: Session) : DokumentDaoInterface
SELECT id FROM person WHERE fodselsnummer=:fodselsnummer
""".trimIndent(),
"fodselsnummer" to fødselsnummer.toLong(),
).single(session) { it.int("id") }?.let { personId ->
).single { it.int("id") }?.let { personId ->
asSQL(
"""
INSERT INTO dokumenter (dokument_id, person_ref, dokument)
Expand All @@ -73,7 +53,7 @@ internal class DokumentDao(private val session: Session) : DokumentDaoInterface
"dokumentId" to dokumentId,
"personRef" to personId,
"dokument" to objectMapper.writeValueAsString(dokument),
).update(session)
).update()
}
}

Expand All @@ -87,7 +67,7 @@ internal class DokumentDao(private val session: Session) : DokumentDaoInterface
""".trimIndent(),
"fodselsnummer" to fødselsnummer.toLong(),
"dokumentId" to dokumentId,
).single(session) { row ->
).single { row ->
row.stringOrNull("dokument")?.let { dokument -> objectMapper.readTree(dokument) }
}

Expand All @@ -96,5 +76,5 @@ internal class DokumentDao(private val session: Session) : DokumentDaoInterface
"""
delete from dokumenter where opprettet < current_date - interval '3 months';
""".trimIndent(),
).update(session)
).update()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package no.nav.helse.modell.varsel

import kotliquery.Session
import kotliquery.queryOf
import kotliquery.sessionOf
import no.nav.helse.HelseDao.Companion.asSQL
import no.nav.helse.db.MedDataSource
import no.nav.helse.db.MedSession
import no.nav.helse.db.QueryRunner
import no.nav.helse.modell.person.vedtaksperiode.Varsel.Status
import org.intellij.lang.annotations.Language
import java.time.LocalDateTime
import java.util.UUID
import javax.sql.DataSource
Expand All @@ -16,49 +17,29 @@ internal interface VarselDaoInterface {
)
}

internal class VarselDao(private val session: Session) : VarselDaoInterface {
internal object NonTransactional {
operator fun invoke(dataSource: DataSource): VarselDaoInterface {
fun inSession(block: (Session) -> Unit) = sessionOf(dataSource).use { block(it) }

return object : VarselDaoInterface {
override fun avvikleVarsel(
varselkode: String,
definisjonId: UUID,
) {
inSession { VarselDao(it).avvikleVarsel(varselkode, definisjonId) }
}
}
}
}
internal class VarselDao(queryRunner: QueryRunner) : VarselDaoInterface, QueryRunner by queryRunner {
constructor(session: Session) : this(MedSession(session))
constructor(dataSource: DataSource) : this(MedDataSource(dataSource))

override fun avvikleVarsel(
varselkode: String,
definisjonId: UUID,
) {
@Language("PostgreSQL")
val query =
asSQL(
"""
UPDATE selve_varsel
SET status = :avvikletStatus,
status_endret_tidspunkt = :endretTidspunkt,
status_endret_ident = :ident,
definisjon_ref = (SELECT id FROM api_varseldefinisjon WHERE unik_id = :definisjonId)
WHERE kode = :varselkode AND status = :aktivStatus;
"""

session.run(
queryOf(
query,
mapOf(
"avvikletStatus" to Status.AVVIKLET.name,
"aktivStatus" to Status.AKTIV.name,
"endretTidspunkt" to LocalDateTime.now(),
"ident" to "avviklet_fra_speaker",
"definisjonId" to definisjonId,
"varselkode" to varselkode,
),
).asUpdate,
)
UPDATE selve_varsel
SET status = :avvikletStatus,
status_endret_tidspunkt = :endretTidspunkt,
status_endret_ident = :ident,
definisjon_ref = (SELECT id FROM api_varseldefinisjon WHERE unik_id = :definisjonId)
WHERE kode = :varselkode AND status = :aktivStatus;
""".trimIndent(),
"avvikletStatus" to Status.AVVIKLET.name,
"aktivStatus" to Status.AKTIV.name,
"endretTidspunkt" to LocalDateTime.now(),
"ident" to "avviklet_fra_speaker",
"definisjonId" to definisjonId,
"varselkode" to varselkode,
).update()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package no.nav.helse.modell.varsel
import javax.sql.DataSource

internal class VarselRepository(dataSource: DataSource) {
private val varselDao = VarselDao.NonTransactional(dataSource)
private val varselDao = VarselDao(dataSource)
private val definisjonDao = DefinisjonDao(dataSource)

internal fun lagreDefinisjon(definisjonDto: VarseldefinisjonDto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ abstract class DatabaseIntegrationTest : AbstractDatabaseTest() {
internal val vergemålDao = VergemålDao(dataSource)
internal val generasjonDao = GenerasjonDao(dataSource)
internal val totrinnsvurderingDao = TotrinnsvurderingDao(session)
internal val dokumentDao = DokumentDao.NonTransactional(dataSource)
internal val dokumentDao = DokumentDao(dataSource)
internal valVentDao = PåVentDao(dataSource)
internal val stansAutomatiskBehandlingDao = StansAutomatiskBehandlingDao(dataSource)
internal val notatDao = NotatDao(dataSource)
Expand Down

0 comments on commit 4a977da

Please sign in to comment.