Skip to content

Commit

Permalink
Oppdatering for Sigrunstub
Browse files Browse the repository at this point in the history
  • Loading branch information
krharum committed Sep 18, 2023
1 parent 0cf3e44 commit 462fe2c
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ma.glasnost.orika.MapperFacade;
import no.nav.dolly.bestilling.ClientFuture;
import no.nav.dolly.bestilling.ClientRegister;
import no.nav.dolly.bestilling.sigrunstub.dto.PensjonsgivendeForFolketrygden;
import no.nav.dolly.bestilling.sigrunstub.dto.SigrunstubResponse;
import no.nav.dolly.domain.jpa.BestillingProgress;
import no.nav.dolly.domain.resultset.RsDollyUtvidetBestilling;
Expand All @@ -18,12 +19,18 @@
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;

@Log4j2
@Service
@RequiredArgsConstructor
public class SigrunStubClient implements ClientRegister {

private static final String LIGNET_INNTEKT = "SIGRUN_LIGNET:";
private static final String PENSJONSGIVENDE_INNTEKT = "SIGRUN_PENSJONSGIVENDE:";

private final SigrunStubConsumer sigrunStubConsumer;
private final ErrorStatusDecoder errorStatusDecoder;
private final MapperFacade mapperFacade;
Expand All @@ -32,20 +39,40 @@ public class SigrunStubClient implements ClientRegister {
@Override
public Flux<ClientFuture> gjenopprett(RsDollyUtvidetBestilling bestilling, DollyPerson dollyPerson, BestillingProgress progress, boolean isOpprettEndre) {

if (!bestilling.getSigrunstub().isEmpty()) {

var context = MappingContextUtils.getMappingContext();
context.setProperty("ident", dollyPerson.getIdent());

var skattegrunnlag =
mapperFacade.mapAsList(bestilling.getSigrunstub(), OpprettSkattegrunnlag.class, context);

return Flux.from(deleteSkattegrunnlag(dollyPerson.getIdent(), isOpprettEndre)
.flatMap(deletedStatus -> sigrunStubConsumer.createSkattegrunnlag(skattegrunnlag))
.map(this::getStatus)
.map(resultat -> futurePersist(progress, resultat)));
}
return Flux.empty();
return Flux.from(Flux.merge(
Flux.just(bestilling)
.filter(bestilling1 -> !bestilling1.getSigrunstub().isEmpty())
.map(RsDollyUtvidetBestilling::getSigrunstub)
.flatMap(lignetInntekt -> {

var context = MappingContextUtils.getMappingContext();
context.setProperty("ident", dollyPerson.getIdent());

var skattegrunnlag =
mapperFacade.mapAsList(lignetInntekt, OpprettSkattegrunnlag.class, context);

return Flux.from(deleteSkattegrunnlag(dollyPerson.getIdent(), isOpprettEndre)
.flatMap(deletedStatus -> sigrunStubConsumer.createSkattegrunnlag(skattegrunnlag))
.map(this::getStatus))
.map(status -> LIGNET_INNTEKT + status);
}),
Flux.just(bestilling)
.filter(bestilling2 -> !bestilling2.getSigrunstubPensjonsgivende().isEmpty())
.map(RsDollyUtvidetBestilling::getSigrunstubPensjonsgivende)
.flatMap(pensjonsgivende -> {

var context = MappingContextUtils.getMappingContext();
context.setProperty("ident", dollyPerson.getIdent());

var skattegrunnlag =
mapperFacade.mapAsList(pensjonsgivende, PensjonsgivendeForFolketrygden.class, context);

return sigrunStubConsumer.updatePensjonsgivendeInntekt(skattegrunnlag)
.map(this::getStatus)
.map(status -> PENSJONSGIVENDE_INNTEKT + status);
}))
.collect(Collectors.joining(","))
.map(resultat -> futurePersist(progress, resultat)));
}

private ClientFuture futurePersist(BestillingProgress progress, String status) {
Expand All @@ -72,7 +99,20 @@ private Mono<SigrunstubResponse> deleteSkattegrunnlag(String ident, boolean oppr

private String getStatus(SigrunstubResponse response) {

return response.getStatus().is2xxSuccessful() ? "OK" :
errorStatusDecoder.getErrorText(response.getStatus(), response.getMelding());
return isNull(response.getErrorStatus()) ?
getStatus(response.getOpprettelseTilbakemeldingsListe()) :
ErrorStatusDecoder.encodeStatus(errorStatusDecoder.getErrorText(response.getErrorStatus(), response.getMelding()));
}

private static String getStatus(List<SigrunstubResponse.OpprettelseTilbakemelding> tilbakemeldinger) {

return tilbakemeldinger.stream()
.allMatch(SigrunstubResponse.OpprettelseTilbakemelding::isOK) ? "OK" :
String.format("Feil: %s", ErrorStatusDecoder.encodeStatus(
tilbakemeldinger.stream()
.filter(SigrunstubResponse.OpprettelseTilbakemelding::isError)
.map(SigrunstubResponse.OpprettelseTilbakemelding::getMessage)
.distinct()
.collect(Collectors.joining(", "))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import no.nav.dolly.bestilling.ConsumerStatus;
import no.nav.dolly.bestilling.sigrunstub.command.SigrunstubDeleteCommand;
import no.nav.dolly.bestilling.sigrunstub.command.SigurunstubPostCommand;
import no.nav.dolly.bestilling.sigrunstub.command.SigurunstubPutCommand;
import no.nav.dolly.bestilling.sigrunstub.dto.PensjonsgivendeForFolketrygden;
import no.nav.dolly.bestilling.sigrunstub.dto.SigrunstubResponse;
import no.nav.dolly.config.credentials.SigrunstubProxyProperties;
import no.nav.dolly.domain.resultset.sigrunstub.OpprettSkattegrunnlag;
Expand Down Expand Up @@ -69,6 +71,15 @@ public Mono<SigrunstubResponse> createSkattegrunnlag(List<OpprettSkattegrunnlag>
.flatMap(token -> new SigurunstubPostCommand(webClient, request, token.getTokenValue()).call());
}

@Timed(name = "providers", tags = { "operation", "sigrun_createPensjonsgivendeInntekt" })
public Mono<SigrunstubResponse> updatePensjonsgivendeInntekt(List<PensjonsgivendeForFolketrygden> request) {

log.info("Put til Sigrunstub med data {}", request);

return tokenService.exchange(serviceProperties)
.flatMap(token -> new SigurunstubPutCommand(webClient, request, token.getTokenValue()).call());
}

@Override
public String serviceUrl() {
return serviceProperties.getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public Mono<SigrunstubResponse> call() {
.toBodilessEntity()
.map(resultat -> SigrunstubResponse.builder()
.ident(ident)
.status(HttpStatus.valueOf(resultat.getStatusCode().value()))
.errorStatus(HttpStatus.valueOf(resultat.getStatusCode().value()))
.build())
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(error -> Mono.just(SigrunstubResponse.builder()
.ident(ident)
.status(WebClientFilter.getStatus(error))
.errorStatus(WebClientFilter.getStatus(error))
.melding(WebClientFilter.getMessage(error))
.build()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import no.nav.dolly.util.RequestHeaderUtil;
import no.nav.dolly.util.WebClientFilter;
import no.nav.testnav.libs.securitycore.config.UserConstant;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
Expand Down Expand Up @@ -43,15 +42,11 @@ public Mono<SigrunstubResponse> call() {
.header(HEADER_NAV_CONSUMER_ID, CONSUMER)
.header(AUTHORIZATION, "Bearer " + token)
.header(UserConstant.USER_HEADER_JWT, getUserJwt())
.bodyValue(request)
.retrieve()
.toBodilessEntity()
.map(resultat -> SigrunstubResponse.builder()
.status(HttpStatus.valueOf(resultat.getStatusCode().value()))
.build())
.bodyToMono(SigrunstubResponse.class)
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(error -> Mono.just(SigrunstubResponse.builder()
.status(WebClientFilter.getStatus(error))
.errorStatus(WebClientFilter.getStatus(error))
.melding(WebClientFilter.getMessage(error))
.build()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package no.nav.dolly.bestilling.sigrunstub.command;

import lombok.RequiredArgsConstructor;
import no.nav.dolly.bestilling.sigrunstub.dto.PensjonsgivendeForFolketrygden;
import no.nav.dolly.bestilling.sigrunstub.dto.SigrunstubResponse;
import no.nav.dolly.util.RequestHeaderUtil;
import no.nav.dolly.util.WebClientFilter;
import no.nav.testnav.libs.securitycore.config.UserConstant;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.Callable;

import static no.nav.dolly.domain.CommonKeysAndUtils.HEADER_NAV_CALL_ID;
import static no.nav.dolly.domain.CommonKeysAndUtils.HEADER_NAV_CONSUMER_ID;
import static no.nav.dolly.util.TokenXUtil.getUserJwt;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RequiredArgsConstructor
public class SigurunstubPutCommand implements Callable<Mono<SigrunstubResponse>> {

private static final String CONSUMER = "Dolly";
private static final String SIGRUN_STUB_OPPRETT_GRUNNLAG = "/api/v1/pensjonsgivendeInntektforfolketrygden";

private final WebClient webClient;
private final List<PensjonsgivendeForFolketrygden> request;
private final String token;

@Override
public Mono<SigrunstubResponse> call() {

return webClient.post().uri(uriBuilder -> uriBuilder
.path(SIGRUN_STUB_OPPRETT_GRUNNLAG)
.build())
.header(ACCEPT, APPLICATION_JSON_VALUE)
.header(HEADER_NAV_CALL_ID, RequestHeaderUtil.getNavCallId())
.header(HEADER_NAV_CONSUMER_ID, CONSUMER)
.header(AUTHORIZATION, "Bearer " + token)
.header(UserConstant.USER_HEADER_JWT, getUserJwt())
.bodyValue(request)
.retrieve()
.bodyToMono(SigrunstubResponse.class)
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(error -> Mono.just(SigrunstubResponse.builder()
.errorStatus(WebClientFilter.getStatus(error))
.melding(WebClientFilter.getMessage(error))
.build()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.nav.dolly.bestilling.sigrunstub.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PensjonsgivendeForFolketrygden {

private String inntektsaar;
@Schema(description = "Liste av map som beskrevet i /api/v1/pensjonsgivendeinntektforfolketrygden/kodeverk")
private List<Map<String, Object>> pensjonsgivendeInntekt;
private String personidentifikator;
private String testdataEier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,48 @@
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SigrunstubResponse {

private HttpStatus status;
private HttpStatus errorStatus;
private String melding;
private String ident;

List<OpprettelseTilbakemelding> opprettelseTilbakemeldingsListe;

public List<OpprettelseTilbakemelding> getOpprettelseTilbakemeldingsListe() {

if (isNull(opprettelseTilbakemeldingsListe)) {
opprettelseTilbakemeldingsListe = new ArrayList<>();
}
return opprettelseTilbakemeldingsListe;
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class OpprettelseTilbakemelding {

private String message;
private String personident;
private Integer status;

public boolean isOK() {
return nonNull(status) && status == 200;
}

public boolean isError() {
return nonNull(status) && status != 200;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import no.nav.dolly.domain.resultset.pdldata.PdlPersondata;
import no.nav.dolly.domain.resultset.pensjon.PensjonData;
import no.nav.dolly.domain.resultset.sigrunstub.OpprettSkattegrunnlag;
import no.nav.dolly.domain.resultset.sigrunstub.RsPensjonsgivendeForFolketrygden;
import no.nav.dolly.domain.resultset.skjerming.RsSkjerming;
import no.nav.dolly.domain.resultset.sykemelding.RsSykemelding;
import no.nav.dolly.domain.resultset.tpsmessagingservice.RsTpsMessaging;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class RsDollyBestilling {
private List<RsInstdata> instdata;
private List<RsAareg> aareg;
private List<OpprettSkattegrunnlag> sigrunstub;
private List<RsPensjonsgivendeForFolketrygden> sigrunstubPensjonsgivende;
private InntektMultiplierWrapper inntektstub;
private Arenadata arenaforvalter;
private RsUdiPerson udistub;
Expand Down Expand Up @@ -91,6 +93,13 @@ public List<OpprettSkattegrunnlag> getSigrunstub() {
return sigrunstub;
}

public List<RsPensjonsgivendeForFolketrygden> getSigrunstubPensjonsgivende() {
if (isNull(sigrunstubPensjonsgivende)) {
sigrunstubPensjonsgivende = new ArrayList<>();
}
return sigrunstubPensjonsgivende;
}

public List<RsInstdata> getInstdata() {
if (isNull(instdata)) {
instdata = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public enum SystemTyper {
INST2("Institusjonsopphold (INST2)"),
KRRSTUB("Digital kontaktinformasjon (DKIF)"),
SIGRUNSTUB("Skatteinntekt grunnlag (SIGRUN)"),
SIGRUN_LIGNET("Lignet skatteinntekt (Sigrunstub)"),
SIGRUN_PENSJONSGIVENDE("Pensjonsgivende inntekt (Sigrunstub)"),
ARENA("Arena fagsystem"),
ARENA_BRUKER("Arena bruker"),
ARENA_AAP("Arena AAP ytelse"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.nav.dolly.domain.resultset.sigrunstub;


import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RsPensjonsgivendeForFolketrygden {

private String inntektsaar;
@Schema(description = "Liste av map som beskrevet i /api/v1/pensjonsgivendeinntektforfolketrygden/kodeverk")
private List<Map<String, Object>> pensjonsgivendeInntekt;
private String testdataEier;
}
Loading

0 comments on commit 462fe2c

Please sign in to comment.