Skip to content

Commit

Permalink
Add skattekort support to bestilling
Browse files Browse the repository at this point in the history
This update introduces skattekort processing in the bestilling service. New functionalities include mapping skattekort status, handling skattekort requests, and updating related configurations.
  • Loading branch information
krharum committed Aug 2, 2024
1 parent ba5f08e commit a5fa5bd
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package no.nav.dolly.bestilling.skattekort;

import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import no.nav.dolly.bestilling.ClientFuture;
import no.nav.dolly.bestilling.ClientRegister;
import no.nav.dolly.domain.jpa.BestillingProgress;
import no.nav.dolly.domain.resultset.RsDollyUtvidetBestilling;
import no.nav.dolly.domain.resultset.dolly.DollyPerson;
import no.nav.dolly.util.TransactionHelperService;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

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

@Service
@RequiredArgsConstructor
public class SkattekortClient implements ClientRegister {

private final SkattekortConsumer skattekortConsumer;
private final MapperFacade mapperFacade;
private final TransactionHelperService transactionHelperService;

@Override
public Flux<ClientFuture> gjenopprett(RsDollyUtvidetBestilling bestilling, DollyPerson dollyPerson,
BestillingProgress progress, boolean isOpprettEndre) {

return Flux.just(bestilling)
.map(bestilling1 -> mapperFacade.map(bestilling1.getSkattekort(), SkattekortRequestDTO.class))
.doOnNext(skattekort ->
skattekort.getArbeidsgiver()
.forEach(arbeidsgiver -> arbeidsgiver.getArbeidstaker()
.forEach(arbeidstaker -> arbeidstaker.setArbeidstakeridentifikator(dollyPerson.getIdent()))))
.flatMap(skattkort -> Flux.fromIterable(skattkort.getArbeidsgiver())
.map(arbeidsgiver -> SkattekortRequestDTO.builder()
.arbeidsgiver(List.of(arbeidsgiver))
.build())
.flatMap(skattekortConsumer::sendSkattekort)
.collect(Collectors.joining(",")))
.map(status -> futurePersist(progress, status));
}

private ClientFuture futurePersist(BestillingProgress progress, String status) {

return () -> {
transactionHelperService.persister(progress, BestillingProgress::setSkattekortStatus, status);
return progress;
};
}

@Override
public void release(List<String> identer) {
// Deletion is not yet supported
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package no.nav.dolly.bestilling.skattekort;

import no.nav.dolly.bestilling.skattekort.command.SkattekortPostCommand;
import no.nav.dolly.config.Consumers;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.standalone.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@Service
public class SkattekortConsumer {

private final WebClient webClient;
private final ServerProperties serverProperties;
private final TokenExchange tokenExchange;

public SkattekortConsumer(WebClient.Builder webClientBuilder, Consumers consumers, TokenExchange tokenExchange) {

this.serverProperties = consumers.getTestnavSkattekortService();
this.webClient = webClientBuilder
.baseUrl(serverProperties.getUrl())
.build();
this.tokenExchange = tokenExchange;
}

public Flux<String> sendSkattekort(SkattekortRequestDTO skattekortRequestDTO) {

return tokenExchange.exchange(serverProperties)
.flatMapMany(token -> new SkattekortPostCommand(webClient, skattekortRequestDTO, token.getTokenValue()).call());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package no.nav.dolly.bestilling.skattekort.command;

import lombok.RequiredArgsConstructor;
import no.nav.dolly.errorhandling.ErrorStatusDecoder;
import no.nav.dolly.util.RequestHeaderUtil;
import no.nav.testnav.libs.dto.skattekortservice.v1.IdentifikatorForEnhetEllerPerson;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;
import no.nav.testnav.libs.reactivecore.utils.WebClientFilter;
import no.nav.testnav.libs.securitycore.config.UserConstant;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;
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.apache.commons.lang3.StringUtils.isNotBlank;
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 SkattekortPostCommand implements Callable<Flux<String>> {

private static final String SKATTEKORT_URL = "/api/v1/skattekort";
private static final String CONSUMER = "Dolly";

private final WebClient webClient;
private final SkattekortRequestDTO request;
private final String token;

@Override
public Flux<String> call() {

return webClient.post().uri(uriBuilder -> uriBuilder
.path(SKATTEKORT_URL)
.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()
.bodyToFlux(String.class)
.map(status -> getArbeidsgiverAndYear(request) + ErrorStatusDecoder.encodeStatus(status))
.doOnError(WebClientFilter::logErrorMessage)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException))
.onErrorResume(throwable -> throwable instanceof WebClientResponseException.NotFound,
throwable -> Mono.empty());
}

private static String getArbeidsgiverAndYear(SkattekortRequestDTO skattekort) {

return skattekort.getArbeidsgiver().stream()
.findFirst()
.map(arbeidsgiver -> String.format("%s+%s:",
getArbeidsgiver(arbeidsgiver.getArbeidsgiveridentifikator()),
arbeidsgiver.getArbeidstaker().stream()
.findFirst()
.map(arbeidstaker -> arbeidstaker.getInntektsaar().toString())
.orElse("inntektsår")))
.orElse("organisasjonsnummer+inntektsår:");
}

private static String getArbeidsgiver(IdentifikatorForEnhetEllerPerson identifikator) {

return isNotBlank(identifikator.getOrganisasjonsnummer()) ?
identifikator.getOrganisasjonsnummer() :
identifikator.getPersonidentifikator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ public class Consumers {
private ServerProperties testnavSyntSykemeldingApi;
private ServerProperties testnavTpsMessagingService;
private ServerProperties testnavUdistubProxy;
private ServerProperties testnavSkattekortService;
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public class BestillingProgress implements Serializable {
@Column(name = "ARBEIDSPLASSENCV_STATUS")
private String arbeidsplassenCVStatus;

@Column(name = "SKATTEKORT_STATUS")
private String skattekortStatus;

@Column(name = "master")
@Enumerated(EnumType.STRING)
private Master master;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import no.nav.dolly.domain.resultset.tpsmessagingservice.RsTpsMessaging;
import no.nav.dolly.domain.resultset.udistub.model.RsUdiPerson;
import no.nav.testnav.libs.data.arbeidsplassencv.v1.ArbeidsplassenCVDTO;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;

import java.util.List;

Expand Down Expand Up @@ -59,4 +60,5 @@ public class BestilteKriterier {
private RsSkjerming skjerming;
private RsSykemelding sykemelding;
private ArbeidsplassenCVDTO arbeidsplassenCV;
private SkattekortRequestDTO skattekort;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import no.nav.dolly.domain.resultset.tpsmessagingservice.RsTpsMessaging;
import no.nav.dolly.domain.resultset.udistub.model.RsUdiPerson;
import no.nav.testnav.libs.data.arbeidsplassencv.v1.ArbeidsplassenCVDTO;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class RsDollyBestilling {
private BankkontoData bankkonto;
private RsSkjerming skjerming;
private ArbeidsplassenCVDTO arbeidsplassenCV;
private SkattekortRequestDTO skattekort;

public List<RsAareg> getAareg() {
if (isNull(aareg)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum SystemTyper {
SIGRUNSTUB("Skatteinntekt grunnlag (SIGRUN)"),
SIGRUN_LIGNET("Lignet skatteinntekt (Sigrunstub)"),
SIGRUN_PENSJONSGIVENDE("Pensjonsgivende inntekt (Sigrunstub)"),
SKATTEKORT("Skattekort (SOKOS)"),
SKJERMINGSREGISTER("Skjermingsregisteret"),
SYKEMELDING("NAV sykemelding"),
TPS_MESSAGING("Meldinger til TPS"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import no.nav.dolly.domain.resultset.sykemelding.RsSykemelding;
import no.nav.dolly.domain.resultset.udistub.model.RsUdiPerson;
import no.nav.testnav.libs.data.arbeidsplassencv.v1.ArbeidsplassenCVDTO;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Persistable;
Expand Down Expand Up @@ -94,6 +95,8 @@ public boolean isNew() {
@Field
private ArbeidsplassenCVDTO arbeidsplassenCV;
@Field
private SkattekortRequestDTO skattekort;
@Field
private List<String> identer;

@Transient
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package no.nav.dolly.mapper;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import no.nav.dolly.domain.jpa.BestillingProgress;
import no.nav.dolly.domain.resultset.RsStatusRapport;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.emptyList;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BestillingSkattekortStatusMapper {

public static List<RsStatusRapport> buildSkattekortStatusMap(List<BestillingProgress> progressList) {

// status org+year ident
Map<String, Map<String, Set<String>>> errorEnvIdents = new HashMap<>();

progressList.forEach(progress -> {
if (isNotBlank(progress.getSkattekortStatus()) && isNotBlank(progress.getIdent())) {
var element = progress.getSkattekortStatus().split(",");
for (String s : element) {
var orgYear = s.split(":")[0];
var status = s.split(":")[1];
if (errorEnvIdents.containsKey(status)) {
if (errorEnvIdents.get(status).containsKey(orgYear)) {
errorEnvIdents.get(status).get(orgYear).add(progress.getIdent());
} else {
errorEnvIdents.get(status).put(orgYear, new HashSet<>(Set.of(progress.getIdent())));
}
} else {
errorEnvIdents.put(status, new HashMap<>(Map.of(orgYear, new HashSet<>(Set.of(progress.getIdent())))));
}
}
}
});

// return errorEnvIdents.isEmpty() ? emptyList() :
// errorEnvIdents.entrySet().stream()
//
//
// singletonList(RsStatusRapport.builder().id(SKATTEKORT).navn(SKATTEKORT.getBeskrivelse())
// .statuser(statusMap.entrySet().stream()
// .map(entry -> RsStatusRapport.Status.builder()
// .melding(getStatus(decodeMsg(entry.getKey())))
// .identer(entry.getValue())
// .build())
// .toList())
// .build());
return emptyList();
}

private static String getStatus(String melding) {

return switch (melding) {
case "Skattekort lagret" -> "OK";
case null -> "FEIL: ingen status mottatt";
default -> "FEIL: " + melding;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static no.nav.dolly.mapper.BestillingPdlPersonStatusMapper.buildPdlPersonStatusMap;
import static no.nav.dolly.mapper.BestillingPensjonforvalterStatusMapper.buildPensjonforvalterStatusMap;
import static no.nav.dolly.mapper.BestillingSigrunStubStatusMapper.buildSigrunStubStatusMap;
import static no.nav.dolly.mapper.BestillingSkattekortStatusMapper.buildSkattekortStatusMap;
import static no.nav.dolly.mapper.BestillingSkjermingsRegisterStatusMapper.buildSkjermingsRegisterStatusMap;
import static no.nav.dolly.mapper.BestillingSykemeldingStatusMapper.buildSykemeldingStatusMap;
import static no.nav.dolly.mapper.BestillingTpsMessagingStatusMapper.buildTpsMessagingStatusMap;
Expand Down Expand Up @@ -103,6 +104,7 @@ public void mapAtoB(Bestilling bestilling, RsBestillingStatus bestillingStatus,
bestillingStatus.getStatus().addAll(buildSkjermingsRegisterStatusMap(progresser));
bestillingStatus.getStatus().addAll(buildKontoregisterStatusMap(progresser));
bestillingStatus.getStatus().addAll(buildArbeidsplassenCVStatusMap(progresser));
bestillingStatus.getStatus().addAll(buildSkattekortStatusMap(progresser));
bestillingStatus.getStatus().addAll(buildAnnenFeilStatusMap(progresser));
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import no.nav.dolly.mapper.MappingStrategy;
import no.nav.testnav.libs.data.arbeidsplassencv.v1.ArbeidsplassenCVDTO;
import no.nav.testnav.libs.data.pdlforvalter.v1.PersonDTO;
import no.nav.testnav.libs.dto.skattekortservice.v1.Arbeidsgiver;
import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -39,6 +41,7 @@ public void register(MapperFactory factory) {
.field("pensjonforvalter", "pensjonforvalter")
.field("sigrunstub", "sigrunstub")
.field("sigrunstubPensjonsgivende", "sigrunstubPensjonsgivende")
.field("skattekort", "skattekort")
.field("skjerming", "skjerming")
.field("sykemelding", "sykemelding")
.field("tpsMessaging", "tpsMessaging")
Expand All @@ -57,6 +60,27 @@ public void mapAtoB(RsDollyUtvidetBestilling request, RsDollyUtvidetBestilling a
})
.register();

factory.classMap(Arbeidsgiver.class, Arbeidsgiver.class)
.mapNulls(false)
.field("arbeidsgiveridentifikator", "arbeidsgiveridentifikator")
.customize(new CustomMapper<>() {
@Override
public void mapAtoB(Arbeidsgiver arbeidsgiver, Arbeidsgiver akkumulert, MappingContext context) {
akkumulert.getArbeidstaker().addAll(arbeidsgiver.getArbeidstaker());
}
})
.register();

factory.classMap(SkattekortRequestDTO.class, SkattekortRequestDTO.class)
.mapNulls(false)
.customize(new CustomMapper<>() {
@Override
public void mapAtoB(SkattekortRequestDTO skattekort, SkattekortRequestDTO akkumulert, MappingContext context) {
akkumulert.getArbeidsgiver().addAll(skattekort.getArbeidsgiver());
}
})
.register();

factory.classMap(ArbeidsplassenCVDTO.Jobboensker.class, ArbeidsplassenCVDTO.Jobboensker.class)
.mapNulls(false)
.field("active", "active")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public String getBestKriterier(RsDollyBestilling request) {
.skjerming(request.getSkjerming())
.sykemelding(request.getSykemelding())
.arbeidsplassenCV(request.getArbeidsplassenCV())
.skattekort(request.getSkattekort())
.build());
}

Expand Down
4 changes: 3 additions & 1 deletion apps/dolly-backend/src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ consumers:
testnav-arbeidsplassencv-proxy:
url: https://testnav-arbeidsplassencv-proxy.intern.dev.nav.no
testnav-kodeverk-service:
url: https://testnav-kodeverk-service.intern.dev.nav.no
url: https://testnav-kodeverk-service.intern.dev.nav.no
testnav-skattekort-service:
url: https://testnav-skattekort-service.intern.dev.nav.no
5 changes: 5 additions & 0 deletions apps/dolly-backend/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,9 @@ consumers:
namespace: dolly
url: http://testnav-arbeidsplassencv-proxy.dolly.svc.cluster.local
cluster: dev-gcp
testnavSkattekortService:
name: testnav-skattekort-service
namespace: dolly
url: http://testnav-skattekort-service.dolly.svc.cluster.local
cluster: dev-gcp

Loading

0 comments on commit a5fa5bd

Please sign in to comment.