55import ma .glasnost .orika .MapperFacade ;
66import no .nav .dolly .bestilling .ClientRegister ;
77import no .nav .dolly .bestilling .arbeidsplassencv .dto .PAMCVDTO ;
8+ import no .nav .dolly .bestilling .personservice .PersonServiceConsumer ;
9+ import no .nav .dolly .domain .PdlPerson ;
10+ import no .nav .dolly .domain .PdlPersonBolk ;
811import no .nav .dolly .domain .jpa .BestillingProgress ;
912import no .nav .dolly .domain .resultset .RsDollyUtvidetBestilling ;
1013import no .nav .dolly .domain .resultset .dolly .DollyPerson ;
1417import no .nav .testnav .libs .dto .arbeidsplassencv .v1 .ArbeidsplassenCVDTO ;
1518import org .springframework .http .HttpStatus ;
1619import org .springframework .stereotype .Service ;
20+ import reactor .core .publisher .Flux ;
1721import reactor .core .publisher .Mono ;
1822import reactor .util .retry .Retry ;
1923
2024import java .util .List ;
2125import java .util .function .Consumer ;
2226
2327import static java .util .Objects .isNull ;
28+ import static java .util .Objects .nonNull ;
2429import static no .nav .dolly .errorhandling .ErrorStatusDecoder .getInfoVenter ;
30+ import static no .nav .testnav .libs .dto .pdlforvalter .v1 .AdressebeskyttelseDTO .AdresseBeskyttelse .STRENGT_FORTROLIG ;
31+ import static org .apache .commons .lang3 .BooleanUtils .isTrue ;
2532
2633@ Slf4j
2734@ Service
@@ -32,6 +39,7 @@ public class ArbeidsplassenCVClient implements ClientRegister {
3239 private final MapperFacade mapperFacade ;
3340 private final TransactionHelperService transactionHelperService ;
3441 private final ErrorStatusDecoder errorStatusDecoder ;
42+ private final PersonServiceConsumer personServiceConsumer ;
3543
3644 @ Override
3745 public Mono <BestillingProgress > gjenopprett (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson , BestillingProgress progress , boolean isOpprettEndre ) {
@@ -40,27 +48,56 @@ public Mono<BestillingProgress> gjenopprett(RsDollyUtvidetBestilling bestilling,
4048 return Mono .empty ();
4149 }
4250
43- return oppdaterStatus (progress , getInfoVenter ("Arbeidsplassen" ))
44- .then (Mono .just (bestilling .getArbeidsplassenCV ()))
45- .flatMap (arbeidsplassenCV -> {
46- var oppdatertOrdre = mapperFacade .map (arbeidsplassenCV , ArbeidsplassenCVDTO .class );
47- bestilling .setArbeidsplassenCV (oppdatertOrdre );
48- return transactionHelperService .persister (progress .getBestillingId (), bestilling )
49- .then (Mono .just (arbeidsplassenCV ));
50- })
51- .flatMap (oppdatertOrdre -> Mono .just (CallIdUtil .generateCallId ())
52- .flatMap (uuid -> arbeidsplassenCVConsumer .opprettPerson (dollyPerson .getIdent (), uuid )
53- .flatMap (response ->
54- arbeidsplassenCVConsumer .godtaHjemmel (dollyPerson .getIdent (), uuid )
55- .then (Mono .just (mapperFacade .map (oppdatertOrdre , PAMCVDTO .class ))
56- .flatMap (request -> arbeidsplassenCVConsumer .oppdaterCV (dollyPerson .getIdent (),
57- request , uuid , logRetries (progress )))))
58-
59- .map (status -> status .getStatus ().is2xxSuccessful () ? "OK" :
60- errorStatusDecoder .getErrorText (HttpStatus .valueOf (status .getStatus ().value ()),
61- status .getFeilmelding ()))
62-
63- .flatMap (resultat -> oppdaterStatus (progress , resultat ))));
51+ return getPerson (dollyPerson )
52+ .flatMap (person -> Mono .zip (
53+ Mono .just (person .getAdressebeskyttelse ().stream ()
54+ .anyMatch (ab ->
55+ STRENGT_FORTROLIG .equals (ab .getGradering ()))),
56+ Mono .just (person .getDoedsfall ().stream ()
57+ .anyMatch (doed -> nonNull (doed .getDoedsdato ())))))
58+ .flatMap (harAdressebeskyttelseEllerDoed -> {
59+ if (isTrue (harAdressebeskyttelseEllerDoed .getT1 ())) {
60+ return oppdaterStatus (progress , "Avvik: CV kan ikke opprettes - person har adressebeskyttelse STRENGT FORTROLIG" );
61+ } else if (isTrue (harAdressebeskyttelseEllerDoed .getT2 ())) {
62+ return oppdaterStatus (progress , "Avvik: CV kan ikke opprettes - person er død" );
63+ } else {
64+
65+ return oppdaterStatus (progress , getInfoVenter ("Arbeidsplassen" ))
66+ .then (Mono .just (bestilling .getArbeidsplassenCV ()))
67+ .flatMap (arbeidsplassenCV -> {
68+ var oppdatertOrdre = mapperFacade .map (arbeidsplassenCV , ArbeidsplassenCVDTO .class );
69+ bestilling .setArbeidsplassenCV (oppdatertOrdre );
70+ return transactionHelperService .persister (progress .getBestillingId (), bestilling )
71+ .then (Mono .just (arbeidsplassenCV ));
72+ })
73+ .flatMap (oppdatertOrdre -> Mono .just (CallIdUtil .generateCallId ())
74+ .flatMap (uuid -> arbeidsplassenCVConsumer .opprettPerson (dollyPerson .getIdent (), uuid )
75+ .flatMap (response ->
76+ arbeidsplassenCVConsumer .godtaHjemmel (dollyPerson .getIdent (), uuid )
77+ .then (Mono .just (mapperFacade .map (oppdatertOrdre , PAMCVDTO .class ))
78+ .flatMap (request -> arbeidsplassenCVConsumer .oppdaterCV (dollyPerson .getIdent (),
79+ request , uuid , logRetries (progress )))))
80+
81+ .map (status -> status .getStatus ().is2xxSuccessful () ? "OK" :
82+ errorStatusDecoder .getErrorText (HttpStatus .valueOf (status .getStatus ().value ()),
83+ status .getFeilmelding ()))
84+
85+ .flatMap (resultat -> oppdaterStatus (progress , resultat ))));
86+ }
87+ });
88+ }
89+
90+ private Mono <PdlPerson .Person > getPerson (DollyPerson dollyPerson ) {
91+
92+ return personServiceConsumer .getPdlPersoner (List .of (dollyPerson .getIdent ()))
93+ .filter (pdlPersonBolk -> nonNull (pdlPersonBolk .getData ()))
94+ .map (PdlPersonBolk ::getData )
95+ .map (PdlPersonBolk .Data ::getHentPersonBolk )
96+ .flatMap (Flux ::fromIterable )
97+ .filter (personBolk -> nonNull (personBolk .getPerson ()))
98+ .map (PdlPersonBolk .PersonBolk ::getPerson )
99+ .next ()
100+ .switchIfEmpty (Mono .just (new PdlPerson .Person ()));
64101 }
65102
66103 private Consumer <Retry .RetrySignal > logRetries (BestillingProgress progress ) {
0 commit comments