Skip to content

Commit

Permalink
Refactor error handling and repository methods #deploy-ereg-batch-sta…
Browse files Browse the repository at this point in the history
…tus-service

Removed retry logic from GetBatchStatusCommand to simplify error handling. Modified StatusRepository and OrdreStatusService to eliminate Optional usage, ensuring robust handling of missing organization numbers. Cleaned up configuration by adjusting token settings in application YAML files.
  • Loading branch information
krharum committed Sep 3, 2024
1 parent a3f301d commit 8fd5e71
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.registre.testnorge.eregbatchstatusservice.util.WebClientFilter;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

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

import static org.springframework.http.HttpHeaders.AUTHORIZATION;
Expand Down Expand Up @@ -38,8 +35,6 @@ public Mono<Long> call() {
log.error("Client error occurred when calling modapp-ereg-proxy");
return Mono.error(new ResponseStatusException(clientResponse.statusCode(), "Client error occurred"));
})
.bodyToMono(Long.class)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException));
.bodyToMono(Long.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface StatusRepository extends CrudRepository<Status, Long> {

@Query(value = "select s from Status s " +
"where s.id in (select max(u.id) from Status u " +
"where u.organisasjonsnummer in (:orgnr) " +
"group by u.organisasjonsnummer, u.miljoe)")
Optional<List<Status>> findAllByOrganisasjonsnummer(@Param(value = "orgnr") List<String> organisasjonsnummer);
List<Status> findAllByOrganisasjonsnummerIn(@Param("orgnr") List<String> organisasjonsnummer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,21 @@ public class OrdreStatusService {

public OrdreResponse getStatus(List<String> orgnumre) {

var statusMap = statusRepository.findAllByOrganisasjonsnummer(orgnumre)
.orElseThrow(() -> new HttpClientErrorException(HttpStatus.NOT_FOUND, "Ingen status funnet for gitte orgnumre"));
var statusMap = statusRepository.findAllByOrganisasjonsnummerIn(orgnumre);

if (orgnumre.stream()
.anyMatch(orgnr -> statusMap.stream()
.noneMatch(status -> orgnr.equals(status.getOrganisasjonsnummer())))) {

return OrdreResponse.builder()
.orgStatus(orgnumre.stream()
.filter(orgnr -> statusMap.stream()
.noneMatch(status -> orgnr.equals(status.getOrganisasjonsnummer())))
.collect(Collectors.toMap(orgnr -> orgnr, grgnr -> List.of(StatusEnv.builder()
.status(StatusDTO.Status.NOT_FOUND)
.build()))))
.build();
}

if (statusMap.stream().anyMatch(status -> isBlank(status.getBestId()))) {
statusMap.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ spring:
default-schema: public
cloud:
vault:
enabled: false
enabled: false
security:
oauth2:
resourceserver:
tokenx:
issuer-uri: ${TOKEN_X_ISSUER}
jwk-set-uri: ${TOKEN_X_JWKS_URI}
accepted-audience: ${TOKEN_X_CLIENT_ID}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ spring:
issuer-uri: ${AAD_ISSUER_URI}/v2.0
jwk-set-uri: ${AAD_ISSUER_URI}/discovery/v2.0/keys
accepted-audience: ${azure.app.client.id}, api://${azure.app.client.id}
tokenx:
issuer-uri: ${TOKEN_X_ISSUER}
jwk-set-uri: ${TOKEN_X_JWKS_URI}
accepted-audience: ${TOKEN_X_CLIENT_ID}

springdoc:
swagger-ui:
Expand Down

0 comments on commit 8fd5e71

Please sign in to comment.