Skip to content

Commit

Permalink
Improve status handling in OrderStatusService
Browse files Browse the repository at this point in the history
#deploy-organisasjon-forvalter

Refactored `getStatus` to enhance status retrieval and error handling. Integrated missing organization checks with import service and introduced JSON error parsing for detailed error messages. Removed unused code in `MiljoerServiceConsumer`.
  • Loading branch information
krharum committed Sep 11, 2024
1 parent 704ab15 commit 1999387
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
import no.nav.organisasjonforvalter.consumer.command.MiljoerServiceCommand;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.emptySet;
import static no.nav.organisasjonforvalter.config.CacheConfig.CACHE_MILJOER;

@Slf4j
@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.organisasjonforvalter.dto.responses;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -27,6 +28,6 @@ public static class StatusEnv {
private String environment;
private StatusDTO.Status status;
private String details;
private String error;
private JsonNode error;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package no.nav.organisasjonforvalter.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.organisasjonforvalter.consumer.OrganisasjonBestillingConsumer;
import no.nav.organisasjonforvalter.dto.responses.BestillingStatus;
import no.nav.organisasjonforvalter.dto.responses.OrdreResponse;
import no.nav.organisasjonforvalter.dto.responses.OrdreResponse.StatusEnv;
import no.nav.organisasjonforvalter.dto.responses.RsOrganisasjon;
import no.nav.organisasjonforvalter.dto.responses.StatusDTO;
import no.nav.organisasjonforvalter.jpa.repository.StatusRepository;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static no.nav.organisasjonforvalter.dto.responses.StatusDTO.Status.ADDING_TO_QUEUE;
import static no.nav.organisasjonforvalter.dto.responses.StatusDTO.Status.COMPLETED;
import static no.nav.organisasjonforvalter.dto.responses.StatusDTO.Status.ERROR;
import static no.nav.organisasjonforvalter.dto.responses.StatusDTO.Status.NOT_FOUND;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
Expand All @@ -35,38 +35,31 @@ public class OrdreStatusService {
private final OrganisasjonBestillingConsumer organisasjonBestillingConsumer;
private final StatusRepository statusRepository;
private final ImportService importService;
private final ObjectMapper objectMapper;

public OrdreResponse getStatus(List<String> orgnumre) {

var orgImiljo = orgnumre.stream()
.map(orgnr -> importService.getOrganisasjoner(orgnr, null))
.toList();

var statusMap = statusRepository.findAllByOrganisasjonsnummerIn(orgnumre);

var orgnrSomMangler = orgnumre.stream()
.filter(orgnr -> statusMap.stream()
.noneMatch(status -> orgnr.equals(status.getOrganisasjonsnummer())))
.filter(orgnr -> orgImiljo.stream()
.filter(iMiljoe -> !iMiljoe.isEmpty())
.flatMap(iMiljoe -> iMiljoe.values().stream())
.noneMatch(iMiljo -> orgnr.equals(iMiljo.getOrganisasjonsnummer())))
.collect(Collectors.toSet());

if (!orgnrSomMangler.isEmpty()) {

return OrdreResponse.builder()
.orgStatus(orgnrSomMangler.stream()
.collect(Collectors.toMap(orgnr -> orgnr, orgnr -> {
var orgIMiljo = importService.getOrganisasjoner(orgnr, null);
return !orgIMiljo.isEmpty() ?
orgIMiljo.keySet().stream()
.map(env -> StatusEnv.builder()
.status(COMPLETED)
.environment(env)
.build())
.toList() :
List.of(StatusEnv.builder()
.status(NOT_FOUND)
.build());
})))
.build();
}
var oppdatertStatus = statusMap.stream()
.filter(status -> orgnrSomMangler.stream()
.noneMatch(orgnr -> orgnr.equals(status.getOrganisasjonsnummer())))
.toList();

var oppdatertStatus = statusMap;
if (statusMap.stream().anyMatch(status -> isBlank(status.getBestId()))) {
if (oppdatertStatus.stream().anyMatch(status -> isBlank(status.getBestId()))) {

oppdatertStatus = Flux.fromIterable(statusMap)
.flatMap(organisasjonBestillingConsumer::getBestillingId)
Expand All @@ -82,61 +75,63 @@ public OrdreResponse getStatus(List<String> orgnumre) {
.collectList()
.block();

var firstOrg = nonNull(orgStatus) ? orgStatus.stream().findFirst().orElse(null) : null;

var orgIMiljoe = false;

if (nonNull(firstOrg)) {
Map<String, RsOrganisasjon> organisasjoner = importService.getOrganisasjoner(firstOrg.getOrgnummer(), Set.of(firstOrg.getMiljoe()));
log.info("Leter etter organisasjoner i miljoe: {}", firstOrg.getMiljoe());
if (!organisasjoner.isEmpty()) {
log.info("Fant org: {} i miljoe: {}", firstOrg.getOrgnummer(), firstOrg.getMiljoe());
orgIMiljoe = true;
}
}

if (oppdatertStatus.stream().anyMatch(status -> isBlank(status.getBestId()))) {
orgStatus.addAll(statusMap.stream()
.filter(status -> isBlank(status.getBestId()))
.map(status -> BestillingStatus.builder()
.orgnummer(status.getOrganisasjonsnummer())
.miljoe(status.getMiljoe())
.status(StatusDTO.builder()
.status(ADDING_TO_QUEUE)
.description("Bestilling venter på å starte")
.build())
.build())
.toList());
}

if (orgIMiljoe) {
return OrdreResponse.builder()
.orgStatus(orgStatus
.stream()
.collect(Collectors.groupingBy(BestillingStatus::getOrgnummer,
mapping(status -> StatusEnv.builder()
.status(COMPLETED)
.details("Fant organisasjon i miljø")
.environment(status.getMiljoe())
.error(status.getFeilmelding())
.build(),
toList()))))
.build();
}
orgStatus.addAll(statusMap.stream()
.filter(status -> isBlank(status.getBestId()))
.map(status -> BestillingStatus.builder()
.orgnummer(status.getOrganisasjonsnummer())
.miljoe(status.getMiljoe())
.status(StatusDTO.builder()
.status(ADDING_TO_QUEUE)
.description("Bestilling venter på å starte")
.build())
.build())
.toList());

orgStatus.addAll(orgImiljo.stream()
.filter(iMiljo -> !iMiljo.isEmpty())
.map(iMiljo -> iMiljo.entrySet().stream()
.map(entry -> BestillingStatus.builder()
.orgnummer(entry.getValue().getOrganisasjonsnummer())
.status(StatusDTO.builder()
.status(COMPLETED)
.description("Fant organisasjonen i miljø")
.build())
.miljoe(entry.getKey())
.build())
.toList())
.flatMap(Collection::stream)
.toList());

orgStatus.addAll(orgnrSomMangler.stream()
.map(orgnr -> BestillingStatus.builder()
.orgnummer(orgnr)
.status(StatusDTO.builder()
.status(NOT_FOUND)
.description("Ikke funnet")
.build())
.build())
.toList());

return OrdreResponse.builder()
.orgStatus((nonNull(orgStatus) ? orgStatus : new ArrayList<BestillingStatus>())
.stream()
.collect(Collectors.groupingBy(BestillingStatus::getOrgnummer,
mapping(status -> StatusEnv.builder()
.status(nonNull(status.getStatus()) ?
status.getStatus().getStatus() : null)
.details(nonNull(status.getStatus()) ?
status.getStatus().getDescription() : null)
.environment(status.getMiljoe())
.error(status.getFeilmelding())
.build(),
toList()))))
.orgStatus(orgStatus.stream()
.collect(Collectors.groupingBy(BestillingStatus::getOrgnummer))
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, org -> org.getValue().stream()
.map(value -> {
try {
return StatusEnv.builder()
.status(nonNull(value.getStatus()) ? value.getStatus().getStatus() : ERROR)
.details(nonNull(value.getStatus()) ? value.getStatus().getDescription() : "Se feilbeskrivelse")
.environment(value.getMiljoe())
.error(nonNull(value.getFeilmelding()) ?
objectMapper.readValue(value.getFeilmelding(), JsonNode.class) :
null)
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.toList())))
.build();
}
}

0 comments on commit 1999387

Please sign in to comment.