Skip to content

Commit

Permalink
IS: Move motestatusendring queries inside repository
Browse files Browse the repository at this point in the history
Prøver å rydde litt i repoet i kjølevannet av nytt endepunkt for møtestatusendring
  • Loading branch information
eirikdahlen committed Nov 5, 2024
1 parent 73e7b2a commit 73ba875
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 140 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/no/nav/syfo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ fun main() {
dialogmotestatusService = dialogmotestatusService,
dialogmoterelasjonService = dialogmoterelasjonService,
arbeidstakerVarselService = arbeidstakerVarselService,
moteStatusEndretRepository = moteStatusEndretRepository,
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/no/nav/syfo/cronjob/CronjobModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import no.nav.syfo.cronjob.statusendring.kafkaDialogmoteStatusEndringProducerCon
import no.nav.syfo.dialogmote.*
import no.nav.syfo.cronjob.dialogmotesvar.KDialogmotesvar
import no.nav.syfo.dialogmote.avro.KDialogmoteStatusEndring
import no.nav.syfo.dialogmote.database.repository.MoteStatusEndretRepository
import org.apache.kafka.clients.producer.KafkaProducer

fun Application.cronjobModule(
Expand All @@ -36,6 +37,7 @@ fun Application.cronjobModule(
dialogmotestatusService: DialogmotestatusService,
dialogmoterelasjonService: DialogmoterelasjonService,
arbeidstakerVarselService: ArbeidstakerVarselService,
moteStatusEndretRepository: MoteStatusEndretRepository,
) {
val azureAdV2Client = AzureAdV2Client(
aadAppClient = environment.aadAppClient,
Expand Down Expand Up @@ -96,6 +98,7 @@ fun Application.cronjobModule(
val publishDialogmoteStatusEndringService = PublishDialogmoteStatusEndringService(
database = database,
dialogmoteStatusEndringProducer = dialogmoteStatusEndringProducer,
moteStatusEndretRepository = moteStatusEndretRepository,
)
val publishDialogmotesvarService = PublishDialogmotesvarService(
database = database,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ package no.nav.syfo.cronjob.statusendring

import no.nav.syfo.application.database.DatabaseInterface
import no.nav.syfo.dialogmote.avro.KDialogmoteStatusEndring
import no.nav.syfo.dialogmote.database.*
import no.nav.syfo.dialogmote.database.domain.*
import no.nav.syfo.dialogmote.domain.*
import no.nav.syfo.dialogmote.database.domain.PDialogmote
import no.nav.syfo.dialogmote.database.domain.toDialogmoteStatusEndret
import no.nav.syfo.dialogmote.database.domain.toDialogmoteTidSted
import no.nav.syfo.dialogmote.database.getDialogmote
import no.nav.syfo.dialogmote.database.getMoteDeltakerArbeidsgiver
import no.nav.syfo.dialogmote.database.getMoteDeltakerArbeidstaker
import no.nav.syfo.dialogmote.database.getTidSted
import no.nav.syfo.dialogmote.database.repository.MoteStatusEndretRepository
import no.nav.syfo.dialogmote.domain.DialogmoteStatusEndret
import no.nav.syfo.dialogmote.domain.DialogmoteTidSted
import no.nav.syfo.dialogmote.domain.latest
import no.nav.syfo.domain.PersonIdent
import no.nav.syfo.domain.Virksomhetsnummer
import java.time.*
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId

class PublishDialogmoteStatusEndringService(
private val database: DatabaseInterface,
private val dialogmoteStatusEndringProducer: DialogmoteStatusEndringProducer,
private val moteStatusEndretRepository: MoteStatusEndretRepository,
) {
fun getDialogmoteStatuEndretToPublishList(): List<DialogmoteStatusEndret> {
return database.getMoteStatusEndretNotPublished().map {
return moteStatusEndretRepository.getMoteStatusEndretNotPublished().map {
it.toDialogmoteStatusEndret()
}
}
Expand All @@ -34,8 +45,8 @@ class PublishDialogmoteStatusEndringService(
virksomhetsnummer = database.getMoteDeltakerArbeidsgiver(moteId).virksomhetsnummer,
)
dialogmoteStatusEndringProducer.sendDialogmoteStatusEndring(kDialogmoteStatusEndring)

database.updateMoteStatusEndretPublishedAt(
moteStatusEndretRepository.updateMoteStatusEndretPublishedAt(
moteStatusEndretId = dialogmoteStatusEndret.id,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class DialogmotestatusService(
)
}

connection.createMoteStatusEndring(
moteStatusEndretRepository.createMoteStatusEndring(
connection = connection,
commit = false,
moteId = dialogmoteId,
opprettetAv = opprettetAv,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import no.nav.syfo.dialogmote.api.domain.DialogmoteStatusEndringDTO
import no.nav.syfo.dialogmote.database.domain.PMoteStatusEndret
import no.nav.syfo.dialogmote.domain.DialogmoteStatus
import no.nav.syfo.domain.PersonIdent
import java.sql.Connection
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Timestamp
import java.time.Instant
import java.time.LocalDate
import java.util.*

class MoteStatusEndretRepository(private val database: DatabaseInterface) {
Expand All @@ -29,7 +34,65 @@ 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 ->
connection.prepareStatement(UPDATE_MOTE_STATUS_ENDRET_PUBLISHED_AT).use {
it.setTimestamp(1, now)
it.setTimestamp(2, now)
it.setInt(3, moteStatusEndretId)
it.execute()
}
connection.commit()
}
}

fun createMoteStatusEndring(
connection: Connection,
commit: Boolean = true,
moteId: Int,
opprettetAv: String,
status: DialogmoteStatus,
tilfelleStart: LocalDate?,
isBehandlerMotedeltaker: Boolean,
): 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)
it.setTimestamp(3, now)
it.setInt(4, moteId)
it.setString(5, status.name)
it.setString(6, opprettetAv)
it.setTimestamp(7, Timestamp.valueOf(startDate.atStartOfDay()))
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 @@ -42,6 +105,36 @@ 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 (
id,
uuid,
created_at,
updated_at,
mote_id,
status,
opprettet_av,
tilfelle_start,
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
SET published_at = ?, updated_at = ?
WHERE id = ?
"""
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import no.nav.syfo.brev.esyfovarsel.EsyfovarselProducer
import no.nav.syfo.brev.esyfovarsel.NarmesteLederHendelse
import no.nav.syfo.dialogmote.api.domain.DialogmoteDTO
import no.nav.syfo.dialogmote.api.v2.*
import no.nav.syfo.dialogmote.database.repository.MoteStatusEndretRepository
import no.nav.syfo.dialogmote.domain.DialogmoteStatus
import no.nav.syfo.testhelper.*
import no.nav.syfo.testhelper.generator.*
Expand All @@ -44,6 +45,8 @@ class PublishDialogmoteStatusEndringCronjobSpek : Spek({

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

val moteStatusEndretRepository = MoteStatusEndretRepository(database)

val behandlerDialogmeldingProducer = mockk<BehandlerDialogmeldingProducer>()
justRun { behandlerDialogmeldingProducer.sendDialogmelding(dialogmelding = any()) }
Expand Down Expand Up @@ -72,6 +75,7 @@ class PublishDialogmoteStatusEndringCronjobSpek : Spek({
val publishDialogmoteStatusEndringService = PublishDialogmoteStatusEndringService(
database = database,
dialogmoteStatusEndringProducer = dialogmoteStatusEndringProducer,
moteStatusEndretRepository = moteStatusEndretRepository,
)

val publishDialogmoteStatusEndringCronjob = PublishDialogmoteStatusEndringCronjob(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import no.nav.syfo.brev.behandler.kafka.KafkaBehandlerDialogmeldingDTO
import no.nav.syfo.brev.esyfovarsel.EsyfovarselProducer
import no.nav.syfo.client.oppfolgingstilfelle.toLatestOppfolgingstilfelle
import no.nav.syfo.dialogmote.api.domain.DialogmoteDTO
import no.nav.syfo.dialogmote.database.repository.getMoteStatusEndretNotPublished
import no.nav.syfo.dialogmote.database.repository.MoteStatusEndretRepository
import no.nav.syfo.dialogmote.domain.*
import no.nav.syfo.testhelper.ExternalMockEnvironment
import no.nav.syfo.testhelper.UserConstants.ARBEIDSTAKER_FNR
Expand Down Expand Up @@ -55,6 +55,7 @@ class AvlysDialogmoteApiV2Spek : Spek({
behandlerDialogmeldingProducer = behandlerDialogmeldingProducer,
)
val esyfovarselProducerMock = mockk<EsyfovarselProducer>(relaxed = true)
val moteStatusEndretRepository = MoteStatusEndretRepository(database)

val altinnMock = mockk<ICorrespondenceAgencyExternalBasic>()
val altinnResponse = ReceiptExternal()
Expand Down Expand Up @@ -180,7 +181,7 @@ class AvlysDialogmoteApiV2Spek : Spek({

verify(exactly = 0) { behandlerDialogmeldingProducer.sendDialogmelding(any()) }

val moteStatusEndretList = database.getMoteStatusEndretNotPublished()
val moteStatusEndretList = moteStatusEndretRepository.getMoteStatusEndretNotPublished()
moteStatusEndretList.size shouldBeEqualTo 2

moteStatusEndretList.forEach { moteStatusEndret ->
Expand Down Expand Up @@ -467,7 +468,7 @@ class AvlysDialogmoteApiV2Spek : Spek({
LocalDateTime.now().isBefore(newDialogmoteDTO.tidSted.tid)
isTodayBeforeDialogmotetid shouldBeEqualTo false

val moteStatusEndretList = database.getMoteStatusEndretNotPublished()
val moteStatusEndretList = moteStatusEndretRepository.getMoteStatusEndretNotPublished()
moteStatusEndretList.size shouldBeEqualTo 2

moteStatusEndretList.forEach { moteStatusEndret ->
Expand Down
Loading

0 comments on commit 73ba875

Please sign in to comment.