Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status when inserting new oppgave, and return it in oppgave result #138

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import no.nav.syfo.aksessering.UlosteOppgave
import no.nav.syfo.db.DatabaseInterface
import no.nav.syfo.db.toList
import no.nav.syfo.model.ManuellOppgaveKomplett
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.ReceivedSykmelding
import no.nav.syfo.model.ValidationResult
import no.nav.syfo.objectMapper
Expand Down Expand Up @@ -127,7 +128,7 @@ suspend fun DatabaseInterface.getUlosteOppgaver(): List<UlosteOppgave> =
withContext(Dispatchers.IO) {
connection.use { connection ->
connection.prepareStatement(
"""select receivedsykmelding->>'mottattDato' as dato, oppgaveId FROM MANUELLOPPGAVE
"""select receivedsykmelding->>'mottattDato' as dato, oppgaveId, status FROM MANUELLOPPGAVE
WHERE ferdigstilt is not true
""",
).use {
Expand All @@ -139,6 +140,7 @@ fun ResultSet.toUlostOppgave(): UlosteOppgave =
UlosteOppgave(
oppgaveId = getInt("oppgaveid"),
mottattDato = LocalDateTime.parse(getString("dato")),
status = ManuellOppgaveStatus.valueOf(getString("status")),
)

fun ResultSet.toManuellOppgave(): ManuellOppgaveKomplett =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import no.nav.syfo.model.ReceivedSykmelding
import no.nav.syfo.oppgave.FerdigstillOppgave
import no.nav.syfo.oppgave.OppgaveStatus
import no.nav.syfo.oppgave.OpprettOppgave
import no.nav.syfo.oppgave.OpprettOppgaveResponse
import no.nav.syfo.oppgave.client.OppgaveClient
import no.nav.syfo.oppgave.model.OpprettOppgaveKafkaMessage
import no.nav.syfo.oppgave.model.PrioritetType
Expand All @@ -25,7 +26,7 @@ class OppgaveService(
private val kafkaProduceTaskProducer: KafkaProducers.KafkaProduceTaskProducer,
) {

suspend fun opprettOppgave(manuellOppgave: ManuellOppgave, loggingMeta: LoggingMeta): Int {
suspend fun opprettOppgave(manuellOppgave: ManuellOppgave, loggingMeta: LoggingMeta): OpprettOppgaveResponse {
val opprettOppgave = tilOpprettOppgave(manuellOppgave)
val oppgaveResponse = oppgaveClient.opprettOppgave(opprettOppgave, manuellOppgave.receivedSykmelding.msgId)
OPPRETT_OPPGAVE_COUNTER.inc()
Expand All @@ -34,7 +35,7 @@ class OppgaveService(
StructuredArguments.keyValue("oppgaveId", oppgaveResponse.id),
StructuredArguments.fields(loggingMeta),
)
return oppgaveResponse.id
return oppgaveResponse
}

fun opprettOppfoligingsOppgave(manuellOppgave: ManuellOppgaveKomplett, enhet: String, veileder: String, loggingMeta: LoggingMeta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import no.nav.syfo.log
import no.nav.syfo.metrics.INCOMING_MESSAGE_COUNTER
import no.nav.syfo.metrics.MESSAGE_STORED_IN_DB_COUNTER
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Merknad
import no.nav.syfo.objectMapper
import no.nav.syfo.oppgave.service.OppgaveService
Expand All @@ -22,6 +23,7 @@ import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.errors.AuthorizationException
import org.apache.kafka.common.errors.ClusterAuthorizationException
import java.time.Duration
import java.time.LocalDateTime
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

Expand All @@ -37,6 +39,12 @@ class MottattSykmeldingService(
companion object {
private const val DELAY_ON_ERROR_SECONDS = 60L
private const val POLL_TIME_SECONDS = 10L

private val statusMap = mapOf(
"FERDIGSTILT" to ManuellOppgaveStatus.FERDIGSTILT,
"FEILREGISTRERT" to ManuellOppgaveStatus.FEILREGISTRERT,
null to ManuellOppgaveStatus.DELETED,
)
}

@ExperimentalTime
Expand Down Expand Up @@ -110,16 +118,17 @@ class MottattSykmeldingService(
fields(loggingMeta),
)
} else {
val oppgaveId = oppgaveService.opprettOppgave(manuellOppgave, loggingMeta)
val oppgave = oppgaveService.opprettOppgave(manuellOppgave, loggingMeta)
val oppdatertApprec = manuellOppgaveService.lagOppdatertApprec(manuellOppgave)

database.opprettManuellOppgave(manuellOppgave, oppdatertApprec, oppgaveId)
val status = statusMap[oppgave.status] ?: ManuellOppgaveStatus.APEN
val statusTimestamp = oppgave.endretTidspunkt?.toLocalDateTime() ?: LocalDateTime.now()
database.opprettManuellOppgave(manuellOppgave, oppdatertApprec, oppgave.id, status, statusTimestamp)
log.info(
"Manuell oppgave lagret i databasen, for {}, {}",
StructuredArguments.keyValue("oppgaveId", oppgaveId),
StructuredArguments.keyValue("oppgaveId", oppgave.id),
fields(loggingMeta),
)
manuellOppgaveService.sendApprec(oppgaveId, oppdatertApprec, loggingMeta)
manuellOppgaveService.sendApprec(oppgave.id, oppdatertApprec, loggingMeta)
manuellOppgaveService.sendReceivedSykmelding(manuellOppgave.receivedSykmelding, loggingMeta)
MESSAGE_STORED_IN_DB_COUNTER.inc()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import no.nav.syfo.model.toPGObject
import java.sql.Timestamp
import java.time.LocalDateTime

suspend fun DatabaseInterface.opprettManuellOppgave(manuellOppgave: ManuellOppgave, apprec: Apprec, oppgaveId: Int) {
suspend fun DatabaseInterface.opprettManuellOppgave(
manuellOppgave: ManuellOppgave,
apprec: Apprec,
oppgaveId: Int,
status: ManuellOppgaveStatus,
statusTimestamp: LocalDateTime,
) {
withContext(Dispatchers.IO) {
connection.use { connection ->
connection.prepareStatement(
Expand All @@ -26,9 +32,11 @@ suspend fun DatabaseInterface.opprettManuellOppgave(manuellOppgave: ManuellOppga
ferdigstilt,
oppgaveid,
sendt_apprec,
opprinnelig_validationresult
opprinnelig_validationresult,
status,
status_timestamp
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
).use {
it.setString(1, manuellOppgave.receivedSykmelding.sykmelding.id)
Expand All @@ -40,6 +48,8 @@ suspend fun DatabaseInterface.opprettManuellOppgave(manuellOppgave: ManuellOppga
it.setInt(7, oppgaveId)
it.setBoolean(8, false)
it.setObject(9, manuellOppgave.validationResult.toPGObject())
it.setString(10, status.name)
it.setTimestamp(11, Timestamp.valueOf(statusTimestamp))
it.executeUpdate()
}
connection.commit()
Expand Down
4 changes: 3 additions & 1 deletion src/test/kotlin/no/nav/syfo/api/AuthenticateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import no.nav.syfo.clients.KafkaProducers
import no.nav.syfo.log
import no.nav.syfo.model.Apprec
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Status
import no.nav.syfo.model.ValidationResult
import no.nav.syfo.objectMapper
Expand All @@ -47,6 +48,7 @@ import no.nav.syfo.testutil.generateSykmelding
import no.nav.syfo.testutil.receivedSykmelding
import org.junit.jupiter.api.Assertions.assertEquals
import java.nio.file.Paths
import java.time.LocalDateTime

class AuthenticateTest : FunSpec({
val path = "src/test/resources/jwkset.json"
Expand Down Expand Up @@ -75,7 +77,7 @@ class AuthenticateTest : FunSpec({
beforeTest {
database.connection.dropData()
clearMocks(syfoTilgangsKontrollClient, msGraphClient, kafkaProducers, oppgaveService)
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid)
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid, ManuellOppgaveStatus.APEN, LocalDateTime.now())
coEvery { syfoTilgangsKontrollClient.sjekkVeiledersTilgangTilPersonViaAzure(any(), any()) } returns Tilgang(true)
}

Expand Down
18 changes: 16 additions & 2 deletions src/test/kotlin/no/nav/syfo/api/HenteManuellOppgaverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import no.nav.syfo.clients.KafkaProducers
import no.nav.syfo.log
import no.nav.syfo.model.Apprec
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Status
import no.nav.syfo.model.ValidationResult
import no.nav.syfo.objectMapper
Expand All @@ -45,6 +46,7 @@ import no.nav.syfo.testutil.generateJWT
import no.nav.syfo.testutil.generateSykmelding
import no.nav.syfo.testutil.receivedSykmelding
import org.junit.jupiter.api.Assertions.assertEquals
import java.time.LocalDateTime
import java.util.concurrent.ExecutionException
import kotlin.test.assertFailsWith

Expand Down Expand Up @@ -122,7 +124,13 @@ class HenteManuellOppgaverTest : FunSpec({
throw cause
}
}
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid)
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
oppgaveid,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)
with(
handleRequest(HttpMethod.Get, "/api/v1/manuellOppgave/$oppgaveid") {
addHeader(
Expand Down Expand Up @@ -174,7 +182,13 @@ class HenteManuellOppgaverTest : FunSpec({
throw cause
}
}
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid)
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
oppgaveid,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)
assertFailsWith<NumberFormatException> {
handleRequest(HttpMethod.Get, "/api/v1/manuellOppgave/1h2j32k")
}
Expand Down
18 changes: 16 additions & 2 deletions src/test/kotlin/no/nav/syfo/api/OpprettManuellOppgaveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.kotest.core.spec.style.FunSpec
import no.nav.syfo.aksessering.db.hentKomplettManuellOppgave
import no.nav.syfo.model.Apprec
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Status
import no.nav.syfo.model.ValidationResult
import no.nav.syfo.objectMapper
Expand All @@ -15,6 +16,7 @@ import no.nav.syfo.testutil.dropData
import no.nav.syfo.testutil.generateSykmelding
import no.nav.syfo.testutil.receivedSykmelding
import org.junit.jupiter.api.Assertions.assertEquals
import java.time.LocalDateTime

class OpprettManuellOppgaveTest : FunSpec({
val database = TestDB.database
Expand All @@ -38,7 +40,13 @@ class OpprettManuellOppgaveTest : FunSpec({

context("Test av oppretting av manuelle oppgaver") {
test("Skal lagre manuellOppgave i databasen og kunne hente den opp som forventet") {
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, 123144)
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
123144,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)

val oppgaveliste = database.hentKomplettManuellOppgave(123144)

Expand All @@ -49,7 +57,13 @@ class OpprettManuellOppgaveTest : FunSpec({
assertEquals(false, oppgaveliste[0].sendtApprec)
}
test("Hvis oppgave er lagret for sykmeldingsid skal erOpprettManuellOppgave returnere true") {
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, 123144)
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
123144,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)

assertEquals(true, database.erOpprettManuellOppgave(manuellOppgave.receivedSykmelding.sykmelding.id))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import no.nav.syfo.db.DatabaseInterface
import no.nav.syfo.log
import no.nav.syfo.model.Apprec
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Merknad
import no.nav.syfo.model.Status
import no.nav.syfo.model.ValidationResult
Expand All @@ -50,6 +51,7 @@ import no.nav.syfo.testutil.generateSykmelding
import no.nav.syfo.testutil.receivedSykmelding
import org.apache.kafka.clients.producer.RecordMetadata
import org.junit.jupiter.api.Assertions.assertEquals
import java.time.LocalDateTime
import java.util.concurrent.CompletableFuture
import kotlin.test.assertFailsWith

Expand Down Expand Up @@ -88,7 +90,13 @@ class SendVurderingManuellOppgaveTest : FunSpec({
with(TestApplicationEngine()) {
start()

database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid)
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
oppgaveid,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)

application.routing {
sendVurderingManuellOppgave(
Expand Down Expand Up @@ -256,7 +264,15 @@ fun setUpTest(
coEvery { kafkaProducers.kafkaRecievedSykmeldingProducer.producer.send(any()) } returns CompletableFuture<RecordMetadata>().apply { complete(mockk()) }
coEvery { oppgaveService.ferdigstillOppgave(any(), any(), any(), any()) } returns Unit
coEvery { kafkaProducers.kafkaApprecProducer.producer.send(any()) } returns CompletableFuture<RecordMetadata>().apply { complete(mockk()) }
runBlocking { database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid) }
runBlocking {
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
oppgaveid,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)
}

testApplicationEngine.application.routing {
sendVurderingManuellOppgave(
Expand Down
10 changes: 9 additions & 1 deletion src/test/kotlin/no/nav/syfo/api/SykmeldingsApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import no.nav.syfo.client.SyfoTilgangsKontrollClient
import no.nav.syfo.clients.KafkaProducers
import no.nav.syfo.model.Apprec
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Status
import no.nav.syfo.model.ValidationResult
import no.nav.syfo.objectMapper
Expand All @@ -33,6 +34,7 @@ import no.nav.syfo.testutil.generateJWT
import no.nav.syfo.testutil.generateSykmelding
import no.nav.syfo.testutil.receivedSykmelding
import org.junit.jupiter.api.Assertions.assertEquals
import java.time.LocalDateTime
import java.util.UUID

class SykmeldingsApiTest : FunSpec({
Expand Down Expand Up @@ -76,7 +78,13 @@ class SykmeldingsApiTest : FunSpec({
}

test("Skal få 200 OK hvis sykmelding finnes") {
database.opprettManuellOppgave(manuellOppgave, manuellOppgave.apprec, oppgaveid)
database.opprettManuellOppgave(
manuellOppgave,
manuellOppgave.apprec,
oppgaveid,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)
with(
handleRequest(HttpMethod.Get, "/api/v1/sykmelding/$sykmeldingsId") {
addHeader(HttpHeaders.Authorization, "Bearer ${generateJWT("2", "clientId")}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import io.mockk.mockk
import io.mockk.mockkStatic
import no.nav.syfo.db.DatabaseInterface
import no.nav.syfo.model.ManuellOppgave
import no.nav.syfo.model.ManuellOppgaveStatus
import no.nav.syfo.model.Status
import no.nav.syfo.model.ValidationResult
import no.nav.syfo.oppgave.client.OppgaveClient
import no.nav.syfo.persistering.db.opprettManuellOppgave
import no.nav.syfo.testutil.TestDB
import no.nav.syfo.testutil.okApprec
import no.nav.syfo.testutil.receivedSykmelding
import java.time.LocalDateTime
import java.util.UUID

class UpdateStatusServiceDbTest : FunSpec({
Expand All @@ -30,6 +32,8 @@ class UpdateStatusServiceDbTest : FunSpec({
),
okApprec(),
0,
ManuellOppgaveStatus.APEN,
LocalDateTime.now(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import no.nav.syfo.service.ManuellOppgaveService
import no.nav.syfo.testutil.TestDB
import no.nav.syfo.testutil.dropData
import no.nav.syfo.testutil.generateSykmelding
import no.nav.syfo.testutil.oppgave
import no.nav.syfo.testutil.receivedSykmelding
import no.nav.syfo.util.LoggingMeta
import org.apache.kafka.clients.consumer.KafkaConsumer
Expand Down Expand Up @@ -66,7 +67,7 @@ class MotattSykmeldingServiceTest : FunSpec({

beforeTest {
clearMocks(syfoTilgangsKontrollClient, kafkaProducers, oppgaveService)
coEvery { oppgaveService.opprettOppgave(any(), any()) } returns oppgaveid
coEvery { oppgaveService.opprettOppgave(any(), any()) } returns oppgave(oppgaveid)
coEvery { kafkaProducers.kafkaApprecProducer.producer } returns mockk()
coEvery { kafkaProducers.kafkaApprecProducer.apprecTopic } returns "apprectopic"
coEvery { kafkaProducers.kafkaRecievedSykmeldingProducer.producer.send(any()) } returns CompletableFuture<RecordMetadata>().apply { complete(mockk()) }
Expand Down
Loading