-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add skattekort support to bestilling
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
Showing
16 changed files
with
288 additions
and
1 deletion.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/skattekort/SkattekortClient.java
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,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 | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
apps/dolly-backend/src/main/java/no/nav/dolly/bestilling/skattekort/SkattekortConsumer.java
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,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()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...ckend/src/main/java/no/nav/dolly/bestilling/skattekort/command/SkattekortPostCommand.java
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,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(); | ||
} | ||
} |
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
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
67 changes: 67 additions & 0 deletions
67
apps/dolly-backend/src/main/java/no/nav/dolly/mapper/BestillingSkattekortStatusMapper.java
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,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; | ||
}; | ||
} | ||
} |
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
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
Oops, something went wrong.