Skip to content

Commit

Permalink
IS: Move queries to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikdahlen committed Nov 5, 2024
1 parent 73ba875 commit d20c814
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PublishDialogmoteStatusEndringService(
virksomhetsnummer = database.getMoteDeltakerArbeidsgiver(moteId).virksomhetsnummer,
)
dialogmoteStatusEndringProducer.sendDialogmoteStatusEndring(kDialogmoteStatusEndring)

moteStatusEndretRepository.updateMoteStatusEndretPublishedAt(
moteStatusEndretId = dialogmoteStatusEndret.id,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.syfo.dialogmote
import no.nav.syfo.client.oppfolgingstilfelle.OppfolgingstilfelleClient
import no.nav.syfo.dialogmote.database.repository.MoteStatusEndretRepository
import no.nav.syfo.dialogmote.api.domain.DialogmoteStatusEndringDTO
import no.nav.syfo.dialogmote.database.createMoteStatusEndring
import no.nav.syfo.dialogmote.database.updateMoteStatus
import no.nav.syfo.dialogmote.domain.*
import no.nav.syfo.domain.PersonIdent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
}
}
}

fun getMoteStatusEndretNotPublished(): List<PMoteStatusEndret> {
return database.connection.use { connection ->
connection.prepareStatement(GET_MOTE_STATUS_ENDRET_NOT_PUBLISHED).use {
it.executeQuery().toList { toPMoteStatusEndret() }
}
}
}

fun updateMoteStatusEndretPublishedAt(moteStatusEndretId: Int) {
val now = Timestamp.from(Instant.now())
database.connection.use { connection ->
Expand All @@ -55,7 +55,7 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
connection.commit()
}
}

fun createMoteStatusEndring(
connection: Connection,
commit: Boolean = true,
Expand All @@ -67,9 +67,9 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
): Pair<Int, UUID> {
val now = Timestamp.from(Instant.now())
val startDate = tilfelleStart ?: LocalDate.EPOCH

val moteStatusEndringUuid = UUID.randomUUID()

val moteStatusEndringIdList = connection.prepareStatement(CREATE_MOTE_STATUS_ENDRING).use {
it.setString(1, moteStatusEndringUuid.toString())
it.setTimestamp(2, now)
Expand All @@ -81,18 +81,18 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
it.setBoolean(8, isBehandlerMotedeltaker)
it.executeQuery().toList { getInt("id") }
}

if (moteStatusEndringIdList.size != 1) {
throw SQLException("Creating MoteStatusEndring failed, no rows affected.")
}

if (commit) {
connection.commit()
}

return Pair(moteStatusEndringIdList.first(), moteStatusEndringUuid)
}

companion object {
private const val GET_MOTE_STATUS_ENDRINGER =
"""
Expand All @@ -105,7 +105,7 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
WHERE mda.personident = ?
ORDER BY mse.created_at DESC
"""

private const val CREATE_MOTE_STATUS_ENDRING =
"""
INSERT INTO MOTE_STATUS_ENDRET (
Expand All @@ -120,15 +120,15 @@ class MoteStatusEndretRepository(private val database: DatabaseInterface) {
motedeltaker_behandler
) VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id
"""

private const val GET_MOTE_STATUS_ENDRET_NOT_PUBLISHED =
"""
SELECT *
FROM MOTE_STATUS_ENDRET
WHERE published_at IS NULL
ORDER BY created_at ASC LIMIT 100
"""

private const val UPDATE_MOTE_STATUS_ENDRET_PUBLISHED_AT =
"""
UPDATE MOTE_STATUS_ENDRET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PublishDialogmoteStatusEndringCronjobSpek : Spek({

val externalMockEnvironment = ExternalMockEnvironment.getInstance()
val database = externalMockEnvironment.database

val moteStatusEndretRepository = MoteStatusEndretRepository(database)

val behandlerDialogmeldingProducer = mockk<BehandlerDialogmeldingProducer>()
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/no/nav/syfo/janitor/JanitorServiceSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class JanitorServiceSpek : Spek({
)
)
}

moteStatusEndretRepository.getMoteStatusEndretNotPublished().shouldBeEmpty()
verify(exactly = 0) { eventStatusProducerMock.sendEventStatus(any()) }
}
Expand Down

0 comments on commit d20c814

Please sign in to comment.