Skip to content

Commit

Permalink
IS-2752: Add endpoint for historikk (#182)
Browse files Browse the repository at this point in the history
* IS-2752: Add endpoint for historikk

* Rename fields
  • Loading branch information
andersrognstad authored Oct 28, 2024
1 parent 1a2fd2d commit a8ef807
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/main/kotlin/no/nav/syfo/application/api/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import no.nav.syfo.dialogmotekandidat.DialogmotekandidatService
import no.nav.syfo.dialogmotekandidat.api.registerDialogmotekandidatApi
import no.nav.syfo.ikkeaktuell.IkkeAktuellService
import no.nav.syfo.ikkeaktuell.api.registerIkkeAktuellApi
import no.nav.syfo.ikkeaktuell.database.IkkeAktuellRepository
import no.nav.syfo.oppfolgingstilfelle.OppfolgingstilfelleService
import no.nav.syfo.unntak.UnntakService
import no.nav.syfo.unntak.api.registerUnntakApi
Expand Down Expand Up @@ -46,6 +47,12 @@ fun Application.apiModule(
dialogmotekandidatService = dialogmotekandidatService,
oppfolgingstilfelleService = oppfolgingstilfelleService,
)
val ikkeAktuellService = IkkeAktuellService(
database = database,
dialogmotekandidatService = dialogmotekandidatService,
ikkeAktuellRepository = IkkeAktuellRepository(database),
oppfolgingstilfelleService = oppfolgingstilfelleService,
)

routing {
registerPodApi(
Expand All @@ -57,19 +64,17 @@ fun Application.apiModule(
registerDialogmotekandidatApi(
veilederTilgangskontrollClient = veilederTilgangskontrollClient,
oppfolgingstilfelleService = oppfolgingstilfelleService,
dialogmotekandidatService = dialogmotekandidatService
dialogmotekandidatService = dialogmotekandidatService,
unntakService = unntakService,
ikkeAktuellService = ikkeAktuellService,
)
registerUnntakApi(
veilederTilgangskontrollClient = veilederTilgangskontrollClient,
unntakService = unntakService,
)
registerIkkeAktuellApi(
veilederTilgangskontrollClient = veilederTilgangskontrollClient,
ikkeAktuellService = IkkeAktuellService(
database = database,
dialogmotekandidatService = dialogmotekandidatService,
oppfolgingstilfelleService = oppfolgingstilfelleService,
),
ikkeAktuellService = ikkeAktuellService,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ class DialogmotekandidatService(
) {
fun getLatestDialogmotekandidatEndring(
personIdent: PersonIdentNumber,
) = getDialogmotekandidatEndringer(personIdent = personIdent).firstOrNull()

fun getDialogmotekandidatEndringer(
personIdent: PersonIdentNumber,
) = database.connection.use { connection ->
connection.getDialogmotekandidatEndringListForPerson(personIdent = personIdent)
}.toDialogmotekandidatEndringList().firstOrNull()
}.toDialogmotekandidatEndringList()

fun getDialogmotekandidaterWithStoppunktPlanlagtTodayOrYesterday() =
database.getDialogmotekandidaterWithStoppunktTodayOrYesterday().toDialogmotekandidatStoppunktList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import no.nav.syfo.client.veiledertilgang.VeilederTilgangskontrollClient
import no.nav.syfo.dialogmotekandidat.DialogmotekandidatService
import no.nav.syfo.dialogmotekandidat.domain.toDialogmotekandidatDTO
import no.nav.syfo.domain.PersonIdentNumber
import no.nav.syfo.ikkeaktuell.IkkeAktuellService
import no.nav.syfo.oppfolgingstilfelle.OppfolgingstilfelleService
import no.nav.syfo.unntak.UnntakService
import no.nav.syfo.util.*
import java.time.LocalDate

const val kandidatApiBasePath = "/api/internad/v1/kandidat"
const val kandidatApiPersonidentPath = "/personident"
const val kandidatApiHistorikkPath = "/historikk"

fun Route.registerDialogmotekandidatApi(
dialogmotekandidatService: DialogmotekandidatService,
oppfolgingstilfelleService: OppfolgingstilfelleService,
unntakService: UnntakService,
ikkeAktuellService: IkkeAktuellService,
veilederTilgangskontrollClient: VeilederTilgangskontrollClient,
) {
route(kandidatApiBasePath) {
Expand Down Expand Up @@ -54,5 +59,32 @@ fun Route.registerDialogmotekandidatApi(
call.respond(kandidatDTO)
}
}
get(kandidatApiHistorikkPath) {
val personident = personIdentHeader()?.let { personIdent ->
PersonIdentNumber(personIdent)
}
?: throw IllegalArgumentException("Failed to get historikk for person: No $NAV_PERSONIDENT_HEADER supplied in request header")
validateVeilederAccess(
action = "Get historikk for person",
personIdentToAccess = personident,
veilederTilgangskontrollClient = veilederTilgangskontrollClient,
) {
val dialogmotekandidatEndringer = dialogmotekandidatService.getDialogmotekandidatEndringer(
personIdent = personident,
)
val unntak = unntakService.getUnntakList(
personIdent = personident,
)
val ikkeAktuell = ikkeAktuellService.getIkkeAktuellList(
personIdent = personident,
)
val historikkDTOs = HistorikkDTO.createHistorikkDTOs(
dialogmotekandidatEndringer = dialogmotekandidatEndringer,
unntak = unntak,
ikkeAktuell = ikkeAktuell,
)
call.respond(historikkDTOs)
}
}
}
}
72 changes: 72 additions & 0 deletions src/main/kotlin/no/nav/syfo/dialogmotekandidat/api/HistorikkDTO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package no.nav.syfo.dialogmotekandidat.api

import no.nav.syfo.dialogmotekandidat.domain.DialogmotekandidatEndring
import no.nav.syfo.dialogmotekandidat.domain.DialogmotekandidatEndringArsak
import no.nav.syfo.ikkeaktuell.domain.IkkeAktuell
import no.nav.syfo.unntak.domain.Unntak
import java.time.LocalDateTime

data class HistorikkDTO(
val tidspunkt: LocalDateTime,
val type: HistorikkType,
val arsak: String,
val vurdertAv: String?,
) {
companion object {
fun createHistorikkDTOs(
dialogmotekandidatEndringer: List<DialogmotekandidatEndring>,
unntak: List<Unntak>,
ikkeAktuell: List<IkkeAktuell>,
): List<HistorikkDTO> {
val historikk = dialogmotekandidatEndringer.toKandidatHistorikk() + unntak.toUnntakHistorikk() + ikkeAktuell.toIkkeAktuellHistorikk()
return historikk.sortedByDescending { it.tidspunkt }
}
}
}

fun List<DialogmotekandidatEndring>.toKandidatHistorikk(): List<HistorikkDTO> = this.mapNotNull {
val tidspunkt = it.createdAt.toLocalDateTime()
when (it.arsak) {
DialogmotekandidatEndringArsak.STOPPUNKT -> HistorikkDTO(
tidspunkt = tidspunkt,
type = HistorikkType.KANDIDAT,
arsak = it.arsak.name,
vurdertAv = null,
)

DialogmotekandidatEndringArsak.LUKKET -> HistorikkDTO(
tidspunkt = tidspunkt,
type = HistorikkType.LUKKET,
arsak = it.arsak.name,
vurdertAv = null,
)

// Disse dekkes av dialogmote-historikk og unntak/ikke-aktuell-historikk
DialogmotekandidatEndringArsak.DIALOGMOTE_FERDIGSTILT, DialogmotekandidatEndringArsak.DIALOGMOTE_LUKKET, DialogmotekandidatEndringArsak.UNNTAK, DialogmotekandidatEndringArsak.IKKE_AKTUELL -> null
}
}

fun List<Unntak>.toUnntakHistorikk(): List<HistorikkDTO> = this.map {
HistorikkDTO(
tidspunkt = it.createdAt.toLocalDateTime(),
type = HistorikkType.UNNTAK,
arsak = it.arsak.name,
vurdertAv = it.createdBy,
)
}

fun List<IkkeAktuell>.toIkkeAktuellHistorikk(): List<HistorikkDTO> = this.map {
HistorikkDTO(
tidspunkt = it.createdAt.toLocalDateTime(),
type = HistorikkType.IKKE_AKTUELL,
arsak = it.arsak.name,
vurdertAv = it.createdBy,
)
}

enum class HistorikkType {
KANDIDAT,
UNNTAK,
IKKE_AKTUELL,
LUKKET,
}
7 changes: 7 additions & 0 deletions src/main/kotlin/no/nav/syfo/ikkeaktuell/IkkeAktuellService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import no.nav.syfo.dialogmotekandidat.DialogmotekandidatService
import no.nav.syfo.dialogmotekandidat.database.getDialogmotekandidatEndringListForPerson
import no.nav.syfo.dialogmotekandidat.database.toDialogmotekandidatEndringList
import no.nav.syfo.dialogmotekandidat.domain.*
import no.nav.syfo.domain.PersonIdentNumber
import no.nav.syfo.ikkeaktuell.database.IkkeAktuellRepository
import no.nav.syfo.ikkeaktuell.database.createIkkeAktuell
import no.nav.syfo.ikkeaktuell.database.toIkkeAktuellList
import no.nav.syfo.ikkeaktuell.domain.IkkeAktuell
import no.nav.syfo.oppfolgingstilfelle.OppfolgingstilfelleService

class IkkeAktuellService(
private val database: DatabaseInterface,
private val dialogmotekandidatService: DialogmotekandidatService,
private val ikkeAktuellRepository: IkkeAktuellRepository,
private val oppfolgingstilfelleService: OppfolgingstilfelleService,
) {
suspend fun createIkkeAktuell(
Expand Down Expand Up @@ -47,4 +51,7 @@ class IkkeAktuellService(
connection.commit()
}
}

fun getIkkeAktuellList(personIdent: PersonIdentNumber): List<IkkeAktuell> =
ikkeAktuellRepository.getIkkeAktuellListForPerson(personIdent = personIdent).toIkkeAktuellList()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package no.nav.syfo.ikkeaktuell.database

import no.nav.syfo.application.database.DatabaseInterface
import no.nav.syfo.application.database.toList
import no.nav.syfo.domain.PersonIdentNumber
import java.sql.ResultSet
import java.time.OffsetDateTime
import java.util.*

class IkkeAktuellRepository(private val database: DatabaseInterface) {

fun getIkkeAktuellListForPerson(personIdent: PersonIdentNumber): List<PIkkeAktuell> =
database.connection.use { connection ->
connection.prepareStatement(GET_IKKE_AKTUELL_FOR_PERSON).use {
it.setString(1, personIdent.value)
it.executeQuery().toList { toPIkkeAktuell() }
}
}

companion object {
private const val GET_IKKE_AKTUELL_FOR_PERSON =
"""
SELECT *
FROM IKKE_AKTUELL
WHERE personident = ?
"""
}
}

fun ResultSet.toPIkkeAktuell() = PIkkeAktuell(
id = getInt("id"),
uuid = UUID.fromString(getString("uuid")),
createdAt = getObject("created_at", OffsetDateTime::class.java),
createdBy = getString("created_by"),
personIdent = getString("personident"),
arsak = getString("arsak"),
beskrivelse = getString("beskrivelse"),
)
28 changes: 28 additions & 0 deletions src/main/kotlin/no/nav/syfo/ikkeaktuell/database/PIkkeAktuell.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package no.nav.syfo.ikkeaktuell.database

import no.nav.syfo.domain.PersonIdentNumber
import no.nav.syfo.ikkeaktuell.domain.IkkeAktuell
import no.nav.syfo.ikkeaktuell.domain.IkkeAktuellArsak
import java.time.OffsetDateTime
import java.util.*

data class PIkkeAktuell(
val id: Int,
val uuid: UUID,
val createdAt: OffsetDateTime,
val createdBy: String,
val personIdent: String,
val arsak: String,
val beskrivelse: String?,
)

fun List<PIkkeAktuell>.toIkkeAktuellList() = this.map {
IkkeAktuell(
uuid = it.uuid,
createdAt = it.createdAt,
createdBy = it.createdBy,
personIdent = PersonIdentNumber(it.personIdent),
arsak = IkkeAktuellArsak.valueOf(it.arsak),
beskrivelse = it.beskrivelse,
)
}
Loading

0 comments on commit a8ef807

Please sign in to comment.