Skip to content

Commit

Permalink
Merge pull request #1257 from navikt/doed-brevmottaker-svarbrev
Browse files Browse the repository at this point in the history
Quick fix for handling failures when sending automatic svarbrev.
  • Loading branch information
flexable777 authored Nov 7, 2024
2 parents fe0c779 + a990a61 commit dbdbe40
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.klage.dokument.service
import com.fasterxml.jackson.databind.ObjectMapper
import io.micrometer.core.instrument.MeterRegistry
import jakarta.servlet.http.HttpServletRequest
import jakarta.transaction.Transactional
import no.nav.klage.dokument.api.mapper.DokumentMapper
import no.nav.klage.dokument.api.view.*
import no.nav.klage.dokument.api.view.JournalfoertDokumentReference
Expand All @@ -15,10 +14,7 @@ import no.nav.klage.dokument.exceptions.DokumentValidationException
import no.nav.klage.dokument.exceptions.SmartDocumentValidationException
import no.nav.klage.dokument.gateway.DefaultKabalSmartEditorApiGateway
import no.nav.klage.dokument.repositories.*
import no.nav.klage.kodeverk.DokumentType
import no.nav.klage.kodeverk.FlowState
import no.nav.klage.kodeverk.PartIdType
import no.nav.klage.kodeverk.Template
import no.nav.klage.kodeverk.*
import no.nav.klage.oppgave.api.view.BehandlingDetaljerView
import no.nav.klage.oppgave.clients.ereg.EregClient
import no.nav.klage.oppgave.clients.kabaldocument.KabalDocumentGateway
Expand Down Expand Up @@ -47,6 +43,7 @@ import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.io.BufferedReader
import java.io.File
import java.nio.file.Files
Expand Down Expand Up @@ -86,6 +83,7 @@ class DokumentUnderArbeidService(
private val dokDistKanalService: DokDistKanalService,
private val kabalJsonToPdfService: KabalJsonToPdfService,
private val tokenUtil: TokenUtil,
private val svarbrevSettingsService: SvarbrevSettingsService,
) {
companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
Expand Down Expand Up @@ -2036,6 +2034,65 @@ class DokumentUnderArbeidService(
)
}

fun sendSvarbrev(
behandling: Behandling,
hindreAutomatiskSvarbrev: Boolean,
) {
if (hindreAutomatiskSvarbrev) {
logger.debug("hindreAutomatiskSvarbrev set to true, returning without sending svarbrev")
return
}
if (behandling.type == Type.KLAGE) {
val svarbrevSettings = svarbrevSettingsService.getSvarbrevSettingsForYtelseAndType(
ytelse = behandling.ytelse,
type = behandling.type
)

if (svarbrevSettings.shouldSend) {
logger.debug("Sender svarbrev for behandling {}", behandling.id)
val receiverId = if (behandling.klager.prosessfullmektig != null) {
behandling.klager.prosessfullmektig!!.partId.value
} else {
behandling.sakenGjelder.partId.value
}

createAndFinalizeDokumentUnderArbeidFromSvarbrev(
behandling = behandling,
svarbrev = Svarbrev(
title = "NAV orienterer om saksbehandlingen",
receivers = listOf(
Svarbrev.Receiver(
id = receiverId,
handling = Svarbrev.Receiver.HandlingEnum.AUTO,
overriddenAddress = null
)
),
fullmektigFritekst = null,
varsletBehandlingstidUnits = svarbrevSettings.behandlingstidUnits,
varsletBehandlingstidUnitType = svarbrevSettings.behandlingstidUnitType,
type = behandling.type,
customText = svarbrevSettings.customText,
),
//Hardcode KA Oslo
avsenderEnhetId = Enhet.E4291.navn,
systemContext = true
)

behandlingService.setVarsletFrist(
behandlingstidUnitType = svarbrevSettings.behandlingstidUnitType,
behandlingstidUnits = svarbrevSettings.behandlingstidUnits,
behandling = behandling,
systemUserContext = true,
mottakere = listOf(behandling.sakenGjelder.partId)
)

logger.debug("Svarbrev klargjort for utsending for behandling {}", behandling.id)
} else {
logger.debug("Svarbrev skal ikke sendes for behandling {}", behandling.id)
}
}
}

fun createAndFinalizeDokumentUnderArbeidFromSvarbrev(
svarbrev: Svarbrev,
behandling: Behandling,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.klage.oppgave.api.controller

import no.nav.klage.oppgave.config.SecurityConfiguration
import no.nav.klage.oppgave.domain.klage.TaskListMerkantil
import no.nav.klage.oppgave.exceptions.MissingTilgangException
import no.nav.klage.oppgave.gateway.AzureGateway
import no.nav.klage.oppgave.service.AdminService
Expand Down Expand Up @@ -151,6 +152,14 @@ class AdminController(
}
}

@GetMapping("/internal/merkantil-tasks")
fun getTaskListMerkantil(): List<TaskListMerkantil> {
logger.debug("getTaskListMerkantil is called")
krevAdminTilgang()

return adminService.getTaskListMerkantil()
}

data class Fnr(val fnr: String)

private fun krevAdminTilgang() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import no.nav.klage.oppgave.domain.klage.AnkeITrygderettenbehandlingInput
import no.nav.klage.oppgave.domain.klage.MottakDokumentType
import no.nav.klage.oppgave.domain.klage.utfallToTrygderetten
import no.nav.klage.oppgave.service.AnkeITrygderettenbehandlingService
import no.nav.klage.oppgave.service.MottakService
import no.nav.klage.oppgave.service.ExternalMottakFacade
import no.nav.klage.oppgave.util.getLogger
import no.nav.security.token.support.core.api.Unprotected
import org.springframework.beans.factory.annotation.Value
Expand All @@ -33,7 +33,7 @@ import kotlin.random.Random
@RestController
@RequestMapping("mockdata")
class MockDataController(
private val mottakService: MottakService,
private val mottakFacade: ExternalMottakFacade,
private val ankeITrygderettenbehandlingService: AnkeITrygderettenbehandlingService,
@Value("#{T(java.time.LocalDate).parse('\${KAKA_VERSION_2_DATE}')}")
private val kakaVersion2Date: LocalDate,
Expand All @@ -45,6 +45,12 @@ class MockDataController(
private val logger = getLogger(javaClass.enclosingClass)
}

@Unprotected
@PostMapping("/doed")
fun createDoedPerson() {
createKlagebehandlingForASpecificPerson("14506507686")
}

//https://dolly.ekstern.dev.nav.no/gruppe/6336
@Unprotected
@PostMapping("/kode6")
Expand All @@ -67,32 +73,18 @@ class MockDataController(
}

fun createKlagebehandlingForASpecificPerson(fnr: String) {
val journalpostId = "510534809"
val journalpost = safFacade.getJournalposter(
journalpostIdSet = setOf(journalpostId),
fnr = null,
saksbehandlerContext = false,
).first()

val dato = LocalDate.of(2022, 1, 13)

mottakService.createMottakForKlageAnkeV3(
mottakFacade.createMottakForKlageAnkeV3(
OversendtKlageAnkeV3(
ytelse = Ytelse.OMS_OMP,
type = Type.KLAGE,
klager = OversendtKlager(
id = OversendtPartId(OversendtPartIdType.PERSON, fnr)
),
fagsak = OversendtSak(
fagsakId = journalpost.sak?.fagsakId ?: "UKJENT",
fagsystem = journalpost.sak?.fagsaksystem?.let {
try {
Fagsystem.valueOf(it)
} catch (e: Exception) {
Fagsystem.AO01
}
}
?: Fagsystem.AO01
fagsakId = UUID.randomUUID().toString(),
fagsystem = Fagsystem.AO01
),
kildeReferanse = UUID.randomUUID().toString(),
innsynUrl = "https://nav.no",
Expand All @@ -102,12 +94,7 @@ class MockDataController(
).shuffled().first()
),
forrigeBehandlendeEnhet = "0104", //NAV Moss
tilknyttedeJournalposter = listOf(
OversendtDokumentReferanse(
randomMottakDokumentType(),
journalpostId
)
),
tilknyttedeJournalposter = listOf(),
brukersHenvendelseMottattNavDato = dato,
innsendtTilNav = dato.minusDays(3),
kilde = Fagsystem.AO01,
Expand Down Expand Up @@ -137,7 +124,7 @@ class MockDataController(

val dato = LocalDate.of(2020, 1, 13)

mottakService.createMottakForKlageAnkeV3(
mottakFacade.createMottakForKlageAnkeV3(
OversendtKlageAnkeV3(
ytelse = Ytelse.OMS_OMP,
type = Type.KLAGE,
Expand Down Expand Up @@ -285,7 +272,7 @@ class MockDataController(
logger.debug("Will create mottak/behandling for klage/anke of type {} for ytelse {}", type, ytelse)
val behandling = when (type) {
Type.KLAGE, Type.ANKE, Type.BEHANDLING_ETTER_TRYGDERETTEN_OPPHEVET -> {
mottakService.createMottakForKlageAnkeV3ForE2ETests(
mottakFacade.createMottakForKlageAnkeV3ForE2ETests(
OversendtKlageAnkeV3(
ytelse = ytelse,
type = type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import no.nav.klage.oppgave.api.view.OversendtKlageAnkeV3
import no.nav.klage.oppgave.api.view.OversendtKlageV2
import no.nav.klage.oppgave.config.SecurityConfiguration
import no.nav.klage.oppgave.service.BehandlingService
import no.nav.klage.oppgave.service.MottakService
import no.nav.klage.oppgave.service.ExternalMottakFacade
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.security.token.support.core.api.ProtectedWithClaims
Expand All @@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RestController
@ProtectedWithClaims(issuer = SecurityConfiguration.ISSUER_AAD)
@RequestMapping("api")
class ExternalApiController(
private val mottakService: MottakService,
private val externalMottakFacade: ExternalMottakFacade,
private val behandlingService: BehandlingService,
) {

Expand All @@ -45,7 +45,7 @@ class ExternalApiController(
@Parameter(description = "Oversendt klage")
@Valid @RequestBody oversendtKlage: OversendtKlageV2
) {
mottakService.createMottakForKlageV2(oversendtKlage)
externalMottakFacade.createMottakForKlageV2(oversendtKlage)
}

@Operation(
Expand All @@ -57,7 +57,7 @@ class ExternalApiController(
@Parameter(description = "Oversendt sak")
@Valid @RequestBody oversendtKlageAnke: OversendtKlageAnkeV3
) {
mottakService.createMottakForKlageAnkeV3(oversendtKlageAnke)
externalMottakFacade.createMottakForKlageAnkeV3(oversendtKlageAnke)
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package no.nav.klage.oppgave.domain.klage

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import java.time.LocalDateTime
import java.util.*

@Entity
@Table(name = "task_list_merkantil", schema = "klage")
class TaskListMerkantil(
@Id
val id: UUID = UUID.randomUUID(),
@Column(name = "behandling_id")
val behandlingId: UUID,
@Column(name = "reason")
val reason: String,
@Column(name = "created")
val created: LocalDateTime,
@Column(name = "date_handled")
var dateHandled: LocalDateTime?,
@Column(name = "handled_by")
var handledBy: String?,
@Column(name = "handled_by_name")
var handledByName: String?,
@Column(name = "comment")
var comment: String?
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as TaskListMerkantil

return id == other.id
}

override fun hashCode(): Int {
return id.hashCode()
}

override fun toString(): String {
return "TaskListMerkantil(id=$id, behandlingId=$behandlingId, reason=$reason, created=$created, dateHandled=$dateHandled, handledBy=$handledBy, handledByName=$handledByName, comment=$comment)"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.nav.klage.oppgave.repositories

import no.nav.klage.oppgave.domain.klage.TaskListMerkantil
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.util.*

@Repository
interface TaskListMerkantilRepository : JpaRepository<TaskListMerkantil, UUID>
5 changes: 5 additions & 0 deletions src/main/kotlin/no/nav/klage/oppgave/service/AdminService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class AdminService(
private val klageFssProxyClient: KlageFssProxyClient,
private val tokenUtil: TokenUtil,
@Value("\${SYSTEMBRUKER_IDENT}") private val systembrukerIdent: String,
private val taskListMerkantilRepository: TaskListMerkantilRepository,
) {

companion object {
Expand Down Expand Up @@ -460,6 +461,10 @@ class AdminService(
Registreringshjemmel.BTRL_13,
Registreringshjemmel.KONTSL_11,
)

fun getTaskListMerkantil(): List<TaskListMerkantil> {
return taskListMerkantilRepository.findAll().sortedByDescending { it.created }
}
}

fun migrateTables(fromJsonString: String?, secureLogger: Logger?): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package no.nav.klage.oppgave.service

import no.nav.klage.dokument.service.DokumentUnderArbeidService
import no.nav.klage.oppgave.api.view.OversendtKlageAnkeV3
import no.nav.klage.oppgave.api.view.OversendtKlageV2
import no.nav.klage.oppgave.domain.klage.Behandling
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import org.springframework.stereotype.Service

@Service
class ExternalMottakFacade(
private val mottakService: MottakService,
private val dokumentUnderArbeidService: DokumentUnderArbeidService,
) {

companion object {
@Suppress("JAVA_CLASS_ON_COMPANION")
private val logger = getLogger(javaClass.enclosingClass)
private val secureLogger = getSecureLogger()
}

fun createMottakForKlageAnkeV3(oversendtKlageAnke: OversendtKlageAnkeV3) {
val behandling = mottakService.createMottakForKlageAnkeV3(oversendtKlageAnke)

tryToSendSvarbrev(behandling, hindreAutomatiskSvarbrev = oversendtKlageAnke.hindreAutomatiskSvarbrev == true)
}

fun createMottakForKlageV2(oversendtKlage: OversendtKlageV2) {
val behandling = mottakService.createMottakForKlageV2(oversendtKlage)

tryToSendSvarbrev(behandling, hindreAutomatiskSvarbrev = oversendtKlage.hindreAutomatiskSvarbrev == true)
}

fun createMottakForKlageAnkeV3ForE2ETests(oversendtKlageAnke: OversendtKlageAnkeV3): Behandling {
val behandling = mottakService.createMottakForKlageAnkeV3(oversendtKlageAnke)

tryToSendSvarbrev(behandling, hindreAutomatiskSvarbrev = oversendtKlageAnke.hindreAutomatiskSvarbrev == true)

return behandling
}

private fun tryToSendSvarbrev(
behandling: Behandling,
hindreAutomatiskSvarbrev: Boolean
) {
try {
dokumentUnderArbeidService.sendSvarbrev(
behandling = behandling,
hindreAutomatiskSvarbrev = hindreAutomatiskSvarbrev,
)
} catch (e: Exception) {
mottakService.createTaskForMerkantil(
behandlingId = behandling.id,
reason = e.message ?: "Ukjent feil"
)
}
}

}
Loading

0 comments on commit dbdbe40

Please sign in to comment.