-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1930 from navikt/annullering-kravgrunnlag
kan annullere et kravgrunnlag
- Loading branch information
Showing
33 changed files
with
841 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...ain/kotlin/tilbakekreving/application/service/kravgrunnlag/AnnullerKravgrunnlagService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package tilbakekreving.application.service.kravgrunnlag | ||
|
||
import arrow.core.Either | ||
import arrow.core.getOrElse | ||
import arrow.core.left | ||
import no.nav.su.se.bakover.common.persistence.SessionFactory | ||
import no.nav.su.se.bakover.common.tid.Tidspunkt | ||
import no.nav.su.se.bakover.domain.sak.SakService | ||
import no.nav.su.se.bakover.hendelse.domain.HendelseId | ||
import org.slf4j.LoggerFactory | ||
import tilbakekreving.domain.AvbruttTilbakekrevingsbehandling | ||
import tilbakekreving.domain.KanAnnullere | ||
import tilbakekreving.domain.TilbakekrevingsbehandlingRepo | ||
import tilbakekreving.domain.kravgrunnlag.AnnullerKravgrunnlagCommand | ||
import tilbakekreving.domain.kravgrunnlag.Kravgrunnlagstatus | ||
import tilbakekreving.domain.kravgrunnlag.påsak.KravgrunnlagStatusendringPåSakHendelse | ||
import tilbakekreving.domain.kravgrunnlag.repo.AnnullerKravgrunnlagStatusEndringMeta | ||
import tilbakekreving.domain.kravgrunnlag.repo.KravgrunnlagRepo | ||
import tilbakekreving.domain.vedtak.Tilbakekrevingsklient | ||
import tilgangstyring.application.TilgangstyringService | ||
import java.time.Clock | ||
|
||
class AnnullerKravgrunnlagService( | ||
private val tilgangstyring: TilgangstyringService, | ||
private val tilbakekrevingsbehandlingRepo: TilbakekrevingsbehandlingRepo, | ||
private val sakService: SakService, | ||
private val kravgrunnlagRepo: KravgrunnlagRepo, | ||
private val tilbakekrevingsklient: Tilbakekrevingsklient, | ||
private val sessionFactory: SessionFactory, | ||
private val clock: Clock, | ||
) { | ||
private val log = LoggerFactory.getLogger(this::class.java) | ||
|
||
fun annuller(command: AnnullerKravgrunnlagCommand): Either<KunneIkkeAnnullereKravgrunnlag, AvbruttTilbakekrevingsbehandling?> { | ||
tilgangstyring.assertHarTilgangTilSak(command.sakId).onLeft { | ||
return KunneIkkeAnnullereKravgrunnlag.IkkeTilgang(it).left() | ||
} | ||
val sak = sakService.hentSak(command.sakId).getOrElse { | ||
throw IllegalStateException("Kunne ikke oppdatere kravgrunnlag for tilbakekrevingsbehandling, fant ikke sak. Command: $command") | ||
} | ||
if (sak.versjon != command.klientensSisteSaksversjon) { | ||
log.info("Oppdater kravgrunnlag - Sakens versjon (${sak.versjon}) er ulik saksbehandlers versjon. Command: $command") | ||
} | ||
val tilbakekrevingsbehandlingHendelser = tilbakekrevingsbehandlingRepo.hentForSak(command.sakId) | ||
val uteståendeKravgrunnlagPåSak = tilbakekrevingsbehandlingHendelser.hentUteståendeKravgrunnlag() | ||
?: return KunneIkkeAnnullereKravgrunnlag.SakenHarIkkeKravgrunnlagSomKanAnnulleres.left() | ||
val kravgrunnlag = tilbakekrevingsbehandlingHendelser.hentKravrunnlag(command.kravgrunnlagHendelseId) | ||
?: return KunneIkkeAnnullereKravgrunnlag.FantIkkeKravgrunnlag.left() | ||
|
||
if (uteståendeKravgrunnlagPåSak.hendelseId != kravgrunnlag.hendelseId) { | ||
return KunneIkkeAnnullereKravgrunnlag.InnsendtHendelseIdErIkkeDenSistePåSaken.left() | ||
} | ||
|
||
val behandling = | ||
tilbakekrevingsbehandlingHendelser.hentBehandlingForKravgrunnlag(uteståendeKravgrunnlagPåSak.hendelseId) | ||
|
||
val (avbruttHendelse, avbruttBehandling) = behandling?.let { | ||
(it as? KanAnnullere)?.annuller( | ||
annulleringstidspunkt = Tidspunkt.now(clock), | ||
annullertAv = command.annullertAv, | ||
versjon = command.klientensSisteSaksversjon.inc(), | ||
) ?: return KunneIkkeAnnullereKravgrunnlag.BehandlingenErIFeilTilstandForÅAnnullere.left() | ||
} ?: (null to null) | ||
|
||
return tilbakekrevingsklient.annullerKravgrunnlag(command.annullertAv, kravgrunnlag).mapLeft { | ||
KunneIkkeAnnullereKravgrunnlag.FeilMotTilbakekrevingskomponenten(it) | ||
}.map { råTilbakekrevingsvedtakForsendelse -> | ||
sessionFactory.withTransactionContext { | ||
kravgrunnlagRepo.lagreKravgrunnlagPåSakHendelse( | ||
KravgrunnlagStatusendringPåSakHendelse( | ||
hendelseId = HendelseId.generer(), | ||
versjon = if (avbruttHendelse == null) command.klientensSisteSaksversjon.inc() else command.klientensSisteSaksversjon.inc(2), | ||
sakId = sak.id, | ||
hendelsestidspunkt = Tidspunkt.now(clock), | ||
tidligereHendelseId = uteståendeKravgrunnlagPåSak.hendelseId, | ||
saksnummer = sak.saksnummer, | ||
eksternVedtakId = uteståendeKravgrunnlagPåSak.eksternVedtakId, | ||
status = Kravgrunnlagstatus.Annullert, | ||
eksternTidspunkt = råTilbakekrevingsvedtakForsendelse.tidspunkt, | ||
), | ||
AnnullerKravgrunnlagStatusEndringMeta( | ||
correlationId = command.correlationId, | ||
ident = command.annullertAv, | ||
brukerroller = command.brukerroller, | ||
tilbakekrevingsvedtakForsendelse = råTilbakekrevingsvedtakForsendelse, | ||
), | ||
it, | ||
) | ||
if (avbruttHendelse != null) { | ||
tilbakekrevingsbehandlingRepo.lagre( | ||
hendelse = avbruttHendelse, | ||
meta = command.toDefaultHendelsesMetadata(), | ||
sessionContext = it, | ||
) | ||
} | ||
} | ||
avbruttBehandling | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../kotlin/tilbakekreving/application/service/kravgrunnlag/KunneIkkeAnnullereKravgrunnlag.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package tilbakekreving.application.service.kravgrunnlag | ||
|
||
import tilbakekreving.domain.vedtak.KunneIkkeAnnullerePåbegynteVedtak | ||
import tilgangstyring.domain.IkkeTilgangTilSak | ||
|
||
sealed interface KunneIkkeAnnullereKravgrunnlag { | ||
data class IkkeTilgang(val underliggende: IkkeTilgangTilSak) : KunneIkkeAnnullereKravgrunnlag | ||
data object InnsendtHendelseIdErIkkeDenSistePåSaken : KunneIkkeAnnullereKravgrunnlag | ||
data object SakenHarIkkeKravgrunnlagSomKanAnnulleres : KunneIkkeAnnullereKravgrunnlag | ||
data object FantIkkeKravgrunnlag : KunneIkkeAnnullereKravgrunnlag | ||
data object BehandlingenErIFeilTilstandForÅAnnullere : KunneIkkeAnnullereKravgrunnlag | ||
data class FeilMotTilbakekrevingskomponenten(val underliggende: KunneIkkeAnnullerePåbegynteVedtak) : | ||
KunneIkkeAnnullereKravgrunnlag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tilbakekreving/domain/src/main/kotlin/tilbakekreving/domain/annuller/KanAnnullere.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@file:Suppress("PackageDirectoryMismatch") | ||
// Må ligge i samme pakke som Tilbakekrevingsbehandling (siden det er et sealed interface), men trenger ikke ligge i samme mappe. | ||
|
||
package tilbakekreving.domain | ||
|
||
import no.nav.su.se.bakover.common.ident.NavIdentBruker | ||
import no.nav.su.se.bakover.common.tid.Tidspunkt | ||
import no.nav.su.se.bakover.hendelse.domain.HendelseId | ||
import no.nav.su.se.bakover.hendelse.domain.Hendelsesversjon | ||
|
||
sealed interface KanAnnullere : KanEndres { | ||
fun annuller( | ||
annulleringstidspunkt: Tidspunkt, | ||
annullertAv: NavIdentBruker.Saksbehandler, | ||
versjon: Hendelsesversjon, | ||
): Pair<AvbruttHendelse, AvbruttTilbakekrevingsbehandling> { | ||
val hendelse = AvbruttHendelse( | ||
hendelseId = HendelseId.generer(), | ||
id = this.id, | ||
utførtAv = annullertAv, | ||
tidligereHendelseId = this.hendelseId, | ||
hendelsestidspunkt = annulleringstidspunkt, | ||
sakId = this.sakId, | ||
versjon = versjon, | ||
begrunnelse = "Behandling er blitt avbrutt fordi kravgrunnlaget skal annulleres.", | ||
) | ||
return hendelse to hendelse.applyToState(this) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../domain/src/main/kotlin/tilbakekreving/domain/kravgrunnlag/AnnullerKravgrunnlagCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package tilbakekreving.domain.kravgrunnlag | ||
|
||
import no.nav.su.se.bakover.common.CorrelationId | ||
import no.nav.su.se.bakover.common.brukerrolle.Brukerrolle | ||
import no.nav.su.se.bakover.common.ident.NavIdentBruker | ||
import no.nav.su.se.bakover.hendelse.domain.DefaultHendelseMetadata | ||
import no.nav.su.se.bakover.hendelse.domain.HendelseId | ||
import no.nav.su.se.bakover.hendelse.domain.Hendelsesversjon | ||
import no.nav.su.se.bakover.hendelse.domain.SakshendelseCommand | ||
import java.util.UUID | ||
|
||
data class AnnullerKravgrunnlagCommand( | ||
override val sakId: UUID, | ||
override val correlationId: CorrelationId?, | ||
override val brukerroller: List<Brukerrolle>, | ||
val annullertAv: NavIdentBruker.Saksbehandler, | ||
val kravgrunnlagHendelseId: HendelseId, | ||
val klientensSisteSaksversjon: Hendelsesversjon, | ||
) : SakshendelseCommand { | ||
override val utførtAv: NavIdentBruker = annullertAv | ||
|
||
fun toDefaultHendelsesMetadata() = DefaultHendelseMetadata( | ||
correlationId = correlationId, | ||
ident = this.utførtAv, | ||
brukerroller = brukerroller, | ||
) | ||
} |
Oops, something went wrong.