|
| 1 | +package no.nav.dolly.bestilling.skattekort.command; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import no.nav.dolly.errorhandling.ErrorStatusDecoder; |
| 5 | +import no.nav.dolly.util.RequestHeaderUtil; |
| 6 | +import no.nav.testnav.libs.dto.skattekortservice.v1.IdentifikatorForEnhetEllerPerson; |
| 7 | +import no.nav.testnav.libs.dto.skattekortservice.v1.SkattekortRequestDTO; |
| 8 | +import no.nav.testnav.libs.reactivecore.utils.WebClientFilter; |
| 9 | +import no.nav.testnav.libs.securitycore.config.UserConstant; |
| 10 | +import org.springframework.web.reactive.function.client.WebClient; |
| 11 | +import org.springframework.web.reactive.function.client.WebClientResponseException; |
| 12 | +import reactor.core.publisher.Flux; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | +import reactor.util.retry.Retry; |
| 15 | + |
| 16 | +import java.time.Duration; |
| 17 | +import java.util.concurrent.Callable; |
| 18 | + |
| 19 | +import static no.nav.dolly.domain.CommonKeysAndUtils.HEADER_NAV_CALL_ID; |
| 20 | +import static no.nav.dolly.domain.CommonKeysAndUtils.HEADER_NAV_CONSUMER_ID; |
| 21 | +import static no.nav.dolly.util.TokenXUtil.getUserJwt; |
| 22 | +import static org.apache.commons.lang3.StringUtils.isNotBlank; |
| 23 | +import static org.springframework.http.HttpHeaders.ACCEPT; |
| 24 | +import static org.springframework.http.HttpHeaders.AUTHORIZATION; |
| 25 | +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; |
| 26 | + |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class SkattekortPostCommand implements Callable<Flux<String>> { |
| 29 | + |
| 30 | + private static final String SKATTEKORT_URL = "/api/v1/skattekort"; |
| 31 | + private static final String CONSUMER = "Dolly"; |
| 32 | + |
| 33 | + private final WebClient webClient; |
| 34 | + private final SkattekortRequestDTO request; |
| 35 | + private final String token; |
| 36 | + |
| 37 | + @Override |
| 38 | + public Flux<String> call() { |
| 39 | + |
| 40 | + return webClient.post().uri(uriBuilder -> uriBuilder |
| 41 | + .path(SKATTEKORT_URL) |
| 42 | + .build()) |
| 43 | + .header(ACCEPT, APPLICATION_JSON_VALUE) |
| 44 | + .header(HEADER_NAV_CALL_ID, RequestHeaderUtil.getNavCallId()) |
| 45 | + .header(HEADER_NAV_CONSUMER_ID, CONSUMER) |
| 46 | + .header(AUTHORIZATION, "Bearer " + token) |
| 47 | + .header(UserConstant.USER_HEADER_JWT, getUserJwt()) |
| 48 | + .bodyValue(request) |
| 49 | + .retrieve() |
| 50 | + .bodyToFlux(String.class) |
| 51 | + .map(status -> getArbeidsgiverAndYear(request) + ErrorStatusDecoder.encodeStatus(status)) |
| 52 | + .doOnError(WebClientFilter::logErrorMessage) |
| 53 | + .retryWhen(Retry.backoff(3, Duration.ofSeconds(5)) |
| 54 | + .filter(WebClientFilter::is5xxException)) |
| 55 | + .onErrorResume(throwable -> throwable instanceof WebClientResponseException.NotFound, |
| 56 | + throwable -> Mono.empty()); |
| 57 | + } |
| 58 | + |
| 59 | + private static String getArbeidsgiverAndYear(SkattekortRequestDTO skattekort) { |
| 60 | + |
| 61 | + return skattekort.getArbeidsgiver().stream() |
| 62 | + .findFirst() |
| 63 | + .map(arbeidsgiver -> String.format("%s+%s:", |
| 64 | + getArbeidsgiver(arbeidsgiver.getArbeidsgiveridentifikator()), |
| 65 | + arbeidsgiver.getArbeidstaker().stream() |
| 66 | + .findFirst() |
| 67 | + .map(arbeidstaker -> arbeidstaker.getInntektsaar().toString()) |
| 68 | + .orElse("inntektsår"))) |
| 69 | + .orElse("organisasjonsnummer+inntektsår:"); |
| 70 | + } |
| 71 | + |
| 72 | + private static String getArbeidsgiver(IdentifikatorForEnhetEllerPerson identifikator) { |
| 73 | + |
| 74 | + return isNotBlank(identifikator.getOrganisasjonsnummer()) ? |
| 75 | + identifikator.getOrganisasjonsnummer() : |
| 76 | + identifikator.getPersonidentifikator(); |
| 77 | + } |
| 78 | +} |
0 commit comments