Skip to content

Commit

Permalink
EY-4680 Sjekker konsistens med forrige overstyrt grunnlag (#6904)
Browse files Browse the repository at this point in the history
* Sjekker konsistens med forrige overstyrt grunnlag

Kopierer også inn grunnlaget fra forrige iverksatte behandling i revurderinger, hvis det finnes.

* Fikser tester

* Tester for konsistenssjekking

* Flipper isEmpty -> isNotEmpty

* Tester at vi lagrer ned forrige grunnlag på ny behandling

* Faktisk sjekke at de er like
  • Loading branch information
oyvindsh authored Jan 30, 2025
1 parent 05fcbf3 commit 5899a09
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ class BeregnOverstyrBeregningService(

val beregningsGrunnlag =
krevIkkeNull(
beregningsGrunnlagService.hentOverstyrBeregningGrunnlag(behandling.id),
beregningsGrunnlagService.hentOverstyrBeregningGrunnlag(behandling.id, brukerTokenInfo),
) { "Behandling ${behandling.id} mangler overstyr beregningsgrunnlag" }

beregningsGrunnlagService.sjekkOmOverstyrtGrunnlagErLiktFoerVirk(
behandling.id,
virkningstidspunkt,
brukerTokenInfo,
)

val beregningsType =
when (behandling.sakType) {
SakType.BARNEPENSJON -> Beregningstype.BP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fun Route.beregningsGrunnlag(
beregningsGrunnlagService
.hentOverstyrBeregningGrunnlag(
behandlingId,
brukerTokenInfo,
).perioder,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import no.nav.etterlatte.libs.common.grunnlag.Grunnlagsopplysning.Companion.auto
import no.nav.etterlatte.libs.common.grunnlag.hentAvdoedesbarn
import no.nav.etterlatte.libs.common.logging.sikkerlogger
import no.nav.etterlatte.libs.common.toJson
import no.nav.etterlatte.libs.common.vedtak.VedtakSammendragDto
import no.nav.etterlatte.libs.common.vedtak.VedtakType
import no.nav.etterlatte.libs.ktor.token.BrukerTokenInfo
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -235,23 +236,17 @@ class BeregningsGrunnlagService(
return grunnlag
}

val forrigeIverksatte = forrigeIverksatteBehandling(behandlingId, brukerTokenInfo)
// Det kan hende behandlingen er en revurdering, og da må vi finne forrige grunnlag for saken
val behandling = behandlingKlient.hentBehandling(behandlingId, brukerTokenInfo)
return if (behandling.behandlingType == BehandlingType.REVURDERING) {
val sisteIverksatteBehandling =
vedtaksvurderingKlient
.hentIverksatteVedtak(behandling.sak, brukerTokenInfo)
.sortedByDescending { it.datoFattet }
.first { it.vedtakType != VedtakType.OPPHOER } // Opphør har ikke beregningsgrunnlag

return if (forrigeIverksatte != null) {
beregningsGrunnlagRepository
.finnBeregningsGrunnlag(sisteIverksatteBehandling.behandlingId)
.finnBeregningsGrunnlag(forrigeIverksatte.behandlingId)
?.also {
logger.info(
"Ga ut forrige beregningsgrunnlag for $behandlingId, funnet i " +
"${sisteIverksatteBehandling.id}. Dette grunnlaget er kopiert inn til $behandlingId.",
"${forrigeIverksatte.id}. Dette grunnlaget er kopiert inn til $behandlingId.",
)
beregningsGrunnlagRepository.lagreBeregningsGrunnlag(it.copy(behandlingId = behandling.id))
beregningsGrunnlagRepository.lagreBeregningsGrunnlag(it.copy(behandlingId = behandlingId))
}
} else {
null
Expand Down Expand Up @@ -308,35 +303,70 @@ class BeregningsGrunnlagService(
}
}

fun hentOverstyrBeregningGrunnlag(behandlingId: UUID): OverstyrBeregningGrunnlag {
suspend fun hentOverstyrBeregningGrunnlag(
behandlingId: UUID,
brukerTokenInfo: BrukerTokenInfo,
): OverstyrBeregningGrunnlag {
logger.info("Henter overstyr beregning grunnlag $behandlingId")

return beregningsGrunnlagRepository
.finnOverstyrBeregningGrunnlagForBehandling(
behandlingId,
).let { overstyrBeregningGrunnlagDaoListe ->
OverstyrBeregningGrunnlag(
perioder =
overstyrBeregningGrunnlagDaoListe.map { periode ->
GrunnlagMedPeriode(
data =
OverstyrBeregningGrunnlagData(
utbetaltBeloep = periode.utbetaltBeloep,
foreldreloessats = periode.foreldreloessats,
trygdetid = periode.trygdetid,
trygdetidForIdent = periode.trygdetidForIdent,
prorataBroekTeller = periode.prorataBroekTeller,
prorataBroekNevner = periode.prorataBroekNevner,
beskrivelse = periode.beskrivelse,
aarsak = periode.aarsak,
),
fom = periode.datoFOM,
tom = periode.datoTOM,
)
},
kilde = overstyrBeregningGrunnlagDaoListe.firstOrNull()?.kilde ?: automatiskSaksbehandler,
val overstyrtePerioder =
beregningsGrunnlagRepository
.finnOverstyrBeregningGrunnlagForBehandling(
behandlingId,
).let { overstyrBeregningGrunnlagDaoListe ->
OverstyrBeregningGrunnlag(
perioder =
overstyrBeregningGrunnlagDaoListe.map(OverstyrBeregningGrunnlagDao::tilGrunnlagMedPeriode),
kilde = overstyrBeregningGrunnlagDaoListe.firstOrNull()?.kilde ?: automatiskSaksbehandler,
)
}
return if (overstyrtePerioder.perioder.isEmpty()) {
// Det kan hende behandlingen er en revurdering, og da må vi finne forrige grunnlag for saken
val forrigeIverksatte = forrigeIverksatteBehandling(behandlingId, brukerTokenInfo)
if (forrigeIverksatte != null) {
logger.info(
"Gir ut det forrige overstyrte beregningsgrunnlaget i behandling ${forrigeIverksatte.behandlingId} for " +
"nåværende behandling under arbeid $behandlingId",
)
val overstyrtePerioderForrigeBehandling =
beregningsGrunnlagRepository.finnOverstyrBeregningGrunnlagForBehandling(forrigeIverksatte.behandlingId)
OverstyrBeregningGrunnlag(
perioder = overstyrtePerioderForrigeBehandling.map(OverstyrBeregningGrunnlagDao::tilGrunnlagMedPeriode),
kilde = overstyrtePerioderForrigeBehandling.firstOrNull()?.kilde ?: automatiskSaksbehandler,
).also {
// Lagre ned det grunnlaget vi gir ut fra forrige iverksatte også på behandlingen vi er i
logger.info(
"Kopierte overstyrt beregningsgrunnlag fra ${forrigeIverksatte.behandlingId} til " +
"$behandlingId, med ${overstyrtePerioderForrigeBehandling.size} perioder.",
)
if (overstyrtePerioderForrigeBehandling.isNotEmpty()) {
beregningsGrunnlagRepository.lagreOverstyrBeregningGrunnlagForBehandling(
behandlingId,
overstyrtePerioderForrigeBehandling,
)
}
}
} else {
overstyrtePerioder
}
} else {
overstyrtePerioder
}
}

private suspend fun forrigeIverksatteBehandling(
behandlingId: UUID,
brukerTokenInfo: BrukerTokenInfo,
): VedtakSammendragDto? {
val behandling = behandlingKlient.hentBehandling(behandlingId, brukerTokenInfo)
return if (behandling.behandlingType == BehandlingType.REVURDERING) {
vedtaksvurderingKlient
.hentIverksatteVedtak(behandling.sak, brukerTokenInfo)
.sortedByDescending { it.datoFattet }
.first { it.vedtakType != VedtakType.OPPHOER } // Opphør har ikke beregningsgrunnlag
} else {
null
}
}

suspend fun lagreOverstyrBeregningGrunnlag(
Expand Down Expand Up @@ -378,7 +408,7 @@ class BeregningsGrunnlagService(
},
)
behandlingKlient.statusTrygdetidOppdatert(behandlingId, brukerTokenInfo, commit = true)
return hentOverstyrBeregningGrunnlag(behandlingId)
return hentOverstyrBeregningGrunnlag(behandlingId, brukerTokenInfo)
} else {
throw OverstyrtBeregningFeilBehandlingStatusException(behandlingId, behandling.status)
}
Expand Down Expand Up @@ -444,6 +474,39 @@ class BeregningsGrunnlagService(
}
}
}

fun sjekkOmOverstyrtGrunnlagErLiktFoerVirk(
behandlingId: UUID,
virkningstidspunkt: YearMonth,
brukerTokenInfo: BrukerTokenInfo,
) {
val forrigeIverksatteBehandling = runBlocking { forrigeIverksatteBehandling(behandlingId, brukerTokenInfo) }
if (forrigeIverksatteBehandling != null) {
// har vi overstyrt beregning for denne behandlingen?
val forrigeOverstyrBeregningGrunnlagPerioder =
runBlocking { hentOverstyrBeregningGrunnlag(forrigeIverksatteBehandling.behandlingId, brukerTokenInfo) }
.perioder
.mapVerdier(OverstyrBeregningGrunnlagData::tilSammenligningsperiode)
if (forrigeOverstyrBeregningGrunnlagPerioder.isEmpty()) {
return
}
val naavaerendeGrunnlagPerioder =
runBlocking { hentOverstyrBeregningGrunnlag(behandlingId, brukerTokenInfo) }
.perioder
.mapVerdier(OverstyrBeregningGrunnlagData::tilSammenligningsperiode)
if (!erGrunnlagLiktFoerEnDato(
naavaerendeGrunnlagPerioder.sortedBy { it.fom },
forrigeOverstyrBeregningGrunnlagPerioder.sortedBy { it.fom },
virkningstidspunkt.atDay(1),
)
) {
throw OverstyrtBeregningsgrunnlagEndresFoerVirkException(
behandlingId = behandlingId,
forrigeBehandlingId = forrigeIverksatteBehandling.behandlingId,
)
}
}
}
}

class OverstyrtBeregningUgyldigTrygdetid(
Expand Down Expand Up @@ -478,3 +541,14 @@ class BPBeregningsgrunnlagSoeskenMarkertDoedException(
detail = "Barnpensjon beregningsgrunnlag bruker søsken som er døde i beregningen",
meta = mapOf("behandlingId" to behandlingId),
)

class OverstyrtBeregningsgrunnlagEndresFoerVirkException(
behandlingId: UUID,
forrigeBehandlingId: UUID,
) : UgyldigForespoerselException(
code = "OVERSTYRT_GRUNNLAG_ENDRET_FOER_VIRK",
detail =
"De overstyrte beregningsperiodene er forskjellige fra forrige " +
"vedtak (id=$forrigeBehandlingId) før virkningstidpunktet i denne behandlingen (id=$behandlingId). " +
"Endringer skal kun skje etter virkningstidspunktet.",
)
Loading

0 comments on commit 5899a09

Please sign in to comment.