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

EY-5031 Tilbakestille mottakere fra oppdatert grunnlag #7008

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -207,6 +207,8 @@ internal class ApplicationContext {
brevdistribuerer,
dokdistKanalKlient,
oppgaveService,
brevdataFacade,
adresseService,
)

val clamAvClient = ClamAvClient(httpClient(), env.requireEnvValue(CLAMAV_ENDPOINT_URL))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fun Route.brevRoute(
route("mottaker") {
post {
withSakId(tilgangssjekker, skrivetilgang = true) {
val mottaker = service.opprettMottaker(brevId)
val mottaker = service.opprettMottaker(brevId, brukerTokenInfo)

call.respond(mottaker)
}
Expand All @@ -109,6 +109,12 @@ fun Route.brevRoute(
}
}

post("tilbakestill") {
withSakId(tilgangssjekker, skrivetilgang = true) {
call.respond(service.tilbakestillMottakere(brevId, brukerTokenInfo))
}
}

put("{mottakerId}/hoved") {
withSakId(tilgangssjekker, skrivetilgang = true) {
val mottakerId =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package no.nav.etterlatte.brev

import no.nav.etterlatte.brev.adresse.AdresseService
import no.nav.etterlatte.brev.behandling.opprettAvsenderRequest
import no.nav.etterlatte.brev.db.BrevRepository
import no.nav.etterlatte.brev.distribusjon.BestemDistribusjonskanalRequest
import no.nav.etterlatte.brev.distribusjon.BestemDistribusjonskanalResponse
import no.nav.etterlatte.brev.distribusjon.Brevdistribuerer
import no.nav.etterlatte.brev.distribusjon.DokDistKanalKlient
import no.nav.etterlatte.brev.hentinformasjon.BrevdataFacade
import no.nav.etterlatte.brev.model.Brev
import no.nav.etterlatte.brev.model.BrevDistribusjonResponse
import no.nav.etterlatte.brev.model.BrevID
Expand Down Expand Up @@ -45,6 +47,8 @@ class BrevService(
private val distribuerer: Brevdistribuerer,
private val dokDistKanalKlient: DokDistKanalKlient,
private val oppgaveService: OppgaveService,
private val brevdataFacade: BrevdataFacade,
private val adresseService: AdresseService,
) {
private val logger = LoggerFactory.getLogger(this::class.java)
private val sikkerlogger = sikkerlogger()
Expand Down Expand Up @@ -258,7 +262,10 @@ class BrevService(
.also { logger.info("Vedlegg payload for brev (id=$id) oppdatert") }
}

fun opprettMottaker(brevId: BrevID): Mottaker {
fun opprettMottaker(
brevId: BrevID,
brukerTokenInfo: BrukerTokenInfo,
): Mottaker {
val brev = sjekkOmBrevKanEndres(brevId)

if (brev.mottakere.size > 1) {
Expand All @@ -270,7 +277,7 @@ class BrevService(
logger.info("Oppretter ny mottaker på brev=$brevId")

db
.opprettMottaker(brevId, nyMottaker)
.opprettMottaker(brevId, nyMottaker, brukerTokenInfo)
.also { logger.info("Ny mottaker opprettet på brev id=$brevId") }

return nyMottaker
Expand All @@ -297,6 +304,31 @@ class BrevService(
}
}

suspend fun tilbakestillMottakere(
brevId: BrevID,
bruker: BrukerTokenInfo,
): List<Mottaker> {
val brev = sjekkOmBrevKanEndres(brevId)
logger.info("Tilbakestiller mottakere for brev=$brevId")
val personerISakOgSak = brevdataFacade.hentPersonerISakforBrev(brev.sakId, brev.behandlingId, bruker)
val nyeMottakere = adresseService.hentMottakere(personerISakOgSak.sak.sakType, personerISakOgSak.personerISak, bruker)
if (nyeMottakere.isEmpty()) {
sebassonav marked this conversation as resolved.
Show resolved Hide resolved
throw KanIkkeTilbakestilleUtenNyeMottakere()
}
if (!nyeMottakere.any { it.type == MottakerType.HOVED }) {
throw KanIkkeSletteHovedmottaker()
}
// bare slett hvis testene går gjennom
brev.mottakere.forEach { mottaker ->
db.slettMottaker(brev.id, mottaker.id, bruker)
}
nyeMottakere.forEach { mottaker ->
db.opprettMottaker(brev.id, mottaker, bruker)
}

return db.hentBrev(brevId).mottakere
}

fun oppdaterMottaker(
brevId: BrevID,
mottaker: Mottaker,
Expand Down Expand Up @@ -487,6 +519,12 @@ class BrevKanIkkeEndres(

class MaksAntallMottakere : UgyldigForespoerselException("MAKS_ANTALL_MOTTAKERE", "Maks 2 mottakere tillatt")

class KanIkkeTilbakestilleUtenNyeMottakere :
UgyldigForespoerselException(
code = "KAN_IKKE_SLETTE_MOTTAKERE_UTEN_NY",
detail = "Kan ikke tilbakestille mottakere hvis det ikke er nye",
)

class KanIkkeSletteHovedmottaker :
UgyldigForespoerselException(
code = "KAN_IKKE_SLETTE_HOVEDMOTTAKER",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ class BrevRepository(
fun opprettMottaker(
id: BrevID,
mottaker: Mottaker,
bruker: BrukerTokenInfo,
) = using(sessionOf(ds)) {
it.opprettMottaker(id, mottaker)
it.lagreHendelse(id, Status.OPPRETTET, mottaker.toJson(), bruker)
}

fun oppdaterMottaker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,46 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.UUID

data class PersonerISakOgSak(
val personerISak: PersonerISak,
val sak: Sak,
)

class BrevdataFacade(
private val vedtaksvurderingService: VedtaksvurderingService,
private val grunnlagService: GrunnlagService,
private val behandlingService: BehandlingService,
) {
private val logger: Logger = LoggerFactory.getLogger(BrevdataFacade::class.java)

suspend fun hentPersonerISakforBrev(
sakId: SakId,
behandlingId: UUID?,
brukerTokenInfo: BrukerTokenInfo,
): PersonerISakOgSak =
coroutineScope {
val sakDeferred = async { behandlingService.hentSak(sakId, brukerTokenInfo) }
val vedtakDeferred = behandlingId?.let { async { vedtaksvurderingService.hentVedtak(it, brukerTokenInfo) } }
val vedtakType = vedtakDeferred?.await()?.type
val grunnlag = grunnlagService.hentGrunnlag(vedtakType, sakId, brukerTokenInfo, behandlingId)
val sak = sakDeferred.await()
val brevutfallDeferred = behandlingId?.let { async { behandlingService.hentBrevutfall(it, brukerTokenInfo) } }

val brevutfallDto = brevutfallDeferred?.await()
val verge = grunnlagService.hentVergeForSak(sak.sakType, brevutfallDto, grunnlag)
val personerISak =
PersonerISak(
innsender = grunnlag.mapInnsender(),
soeker = grunnlag.mapSoeker(brevutfallDto?.aldersgruppe),
avdoede = grunnlag.mapAvdoede(),
verge = verge,
)
PersonerISakOgSak(
personerISak = personerISak,
sak = sak,
)
}

suspend fun hentGenerellBrevData(
sakId: SakId,
behandlingId: UUID?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ internal class BrevServiceTest {
mockk(),
mockk(),
mockk(),
mockk(),
mockk(),
)
private val bruker = simpleSaksbehandler("Z123456")

Expand Down Expand Up @@ -266,13 +268,13 @@ internal class BrevServiceTest {

every { db.hentBrev(any()) } returns brev

val faktiskMottaker = brevService.opprettMottaker(brev.id)
val faktiskMottaker = brevService.opprettMottaker(brev.id, bruker)

faktiskMottaker.type shouldBe MottakerType.KOPI

verify {
db.hentBrev(brev.id)
db.opprettMottaker(brev.id, match { it.type == MottakerType.KOPI })
db.opprettMottaker(brev.id, match { it.type == MottakerType.KOPI }, bruker)
}
}

Expand All @@ -288,7 +290,7 @@ internal class BrevServiceTest {

every { db.hentBrev(any()) } returns brev

assertThrows<MaksAntallMottakere> { brevService.opprettMottaker(brev.id) }
assertThrows<MaksAntallMottakere> { brevService.opprettMottaker(brev.id, bruker) }

verify {
db.hentBrev(brev.id)
Expand All @@ -303,7 +305,7 @@ internal class BrevServiceTest {
every { db.hentBrev(any()) } returns brev

assertThrows<BrevKanIkkeEndres> {
brevService.opprettMottaker(brev.id)
brevService.opprettMottaker(brev.id, bruker)
}

verify {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { BrevMottakerPanel } from '~components/person/brev/mottaker/BrevMottaker
import { Alert, Button, HStack, VStack } from '@navikt/ds-react'
import React, { useState } from 'react'
import { useApiCall } from '~shared/hooks/useApiCall'
import { opprettMottaker } from '~shared/api/brev'
import { opprettMottaker, tilbakestillMottakere } from '~shared/api/brev'
import { isPending } from '~shared/api/apiUtils'
import { PlusIcon } from '@navikt/aksel-icons'

export const BrevMottakerWrapper = ({ brev, kanRedigeres }: { brev: IBrev; kanRedigeres: boolean }) => {
const [mottakere, setMottakere] = useState(brev.mottakere)

const [opprettMottakerResult, apiOpprettMottaker] = useApiCall(opprettMottaker)
const [tilbakestillMottakereResult, apiTilbakestillMottaker] = useApiCall(tilbakestillMottakere)

const tilbakestillMottakereWrapper = () => {
apiTilbakestillMottaker({ brevId: brev.id, sakId: brev.sakId }, (mottakereres) => {
setMottakere([...mottakereres])
})
}

const opprettNyMottaker = () => {
apiOpprettMottaker({ brevId: brev.id, sakId: brev.sakId }, (mottaker) => {
Expand Down Expand Up @@ -53,6 +60,18 @@ export const BrevMottakerWrapper = ({ brev, kanRedigeres }: { brev: IBrev; kanRe
</Button>
</HStack>
)}
{kanRedigeres && (
<HStack justify="center">
<Button
variant="secondary"
onClick={tilbakestillMottakereWrapper}
loading={isPending(tilbakestillMottakereResult)}
icon={<PlusIcon aria-hidden />}
>
Tilbakestill mottakere
</Button>
</HStack>
)}
</VStack>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export const opprettVedtaksbrev = async (args: {
export const opprettMottaker = async (props: { brevId: number; sakId: number }): Promise<ApiResponse<Mottaker>> =>
apiClient.post(`/brev/${props.brevId}/mottaker?sakId=${props.sakId}`, {})

export const tilbakestillMottakere = async (props: {
brevId: number
sakId: number
}): Promise<ApiResponse<Mottaker[]>> =>
apiClient.post(`/brev/${props.brevId}/mottaker/tilbakestill?sakId=${props.sakId}`, {})

export const oppdaterMottaker = async (props: {
brevId: number
sakId: number
Expand Down
2 changes: 1 addition & 1 deletion libs/etterlatte-ktor/src/main/kotlin/route/RouteUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ suspend inline fun PipelineContext<*, ApplicationCall>.withSakId(
val sakId =
try {
call.parameters[SAKID_CALL_PARAMETER]?.tilSakId()
} catch (e: Exception) {
} catch (_: Exception) {
throw UgyldigForespoerselException("SAKID_IKKE_TALL", "Kunne ikke lese ut sakId-parameter")
}
if (sakId == null) {
Expand Down
Loading