1919import reactor .core .publisher .Mono ;
2020
2121import java .util .List ;
22+ import java .util .Set ;
2223import java .util .concurrent .atomic .AtomicInteger ;
2324import java .util .stream .Collectors ;
2425
2526import static java .util .Collections .emptyList ;
2627import static java .util .Objects .isNull ;
2728import static java .util .Objects .nonNull ;
29+ import static no .nav .dolly .errorhandling .ErrorStatusDecoder .getInfoVenter ;
2830
2931@ Slf4j
3032@ Service
@@ -33,6 +35,7 @@ public class InstdataClient implements ClientRegister {
3335
3436 private static final String INST2_STATUS = "INST2_STATUS#%s" ;
3537 private static final String KDI_STATUS = "KDI_STATUS#%s" ;
38+ private static final String INSTDATA = "Institusjonsopphold" ;
3639
3740 private final MapperFacade mapperFacade ;
3841 private final InstdataConsumer instdataConsumer ;
@@ -48,20 +51,25 @@ public Mono<BestillingProgress> gjenopprett(RsDollyUtvidetBestilling bestilling,
4851 return Mono .empty ();
4952 }
5053
51- return Flux .merge (doInst2Bestilling (bestilling , dollyPerson , isOpprettEndre )
54+ return Flux .merge (doInst2Bestilling (bestilling , dollyPerson , progress , isOpprettEndre )
5255 .map (INST2_STATUS ::formatted ),
53- doInstKdiBestilling (bestilling , dollyPerson , progress . getBestillingId () , isOpprettEndre )
56+ doInstKdiBestilling (bestilling , dollyPerson , progress , isOpprettEndre )
5457 .map (KDI_STATUS ::formatted ))
5558 .collect (Collectors .joining ("|" ))
5659 .filter (resultat -> !resultat .isBlank ())
5760 .flatMap (resultat -> oppdaterStatus (progress , resultat ));
5861 }
5962
60- private Mono <String > doInstKdiBestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson , Long bestillingId , boolean isOpprettEndre ) {
63+ private Mono <String > doInstKdiBestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson ,
64+ BestillingProgress progress , boolean isOpprettEndre ) {
6165
6266 return Mono .just (bestilling )
63- .filter (bestilling1 -> nonNull (bestilling1 .getInstdataKdi ()))
64- .flatMap (bestilling1 -> instKdiHendelseService .getOppdaterBestilling (bestilling1 , bestillingId , isOpprettEndre ))
67+ .filter (_ -> nonNull (bestilling .getInstdataKdi ()) &&
68+ !bestilling .getEnvironments ().isEmpty ())
69+ .flatMap (_ -> oppdaterStatus (progress , prepInitStatus (KDI_STATUS ,
70+ bestilling .getEnvironments ())))
71+ .flatMap (_ -> instKdiHendelseService .getOppdaterBestilling (bestilling ,
72+ progress .getBestillingId (), isOpprettEndre ))
6573 .flatMap (instKdiData -> instdataConsumer .getMiljoer ()
6674 .flatMapMany (miljoer -> Flux .fromIterable (miljoer .getKdiEnvironments ())
6775 .filter (miljoe -> bestilling .getEnvironments ().contains (miljoe ))
@@ -76,14 +84,17 @@ private Mono<String> doInstKdiBestilling(RsDollyUtvidetBestilling bestilling, Do
7684 .filter (resultat -> !resultat .isBlank ());
7785 }
7886
79- private Flux <String > doInst2Bestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson , boolean isOpprettEndre ) {
87+ private Flux <String > doInst2Bestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson ,
88+ BestillingProgress progress , boolean isOpprettEndre ) {
8089
81- var context = MappingContextUtils .getMappingContext ();
82- context .setProperty ("ident" , dollyPerson .getIdent ());
83-
84- return Mono .just (bestilling .getInstdata ())
85- .filter (rsInstdata -> !rsInstdata .isEmpty ())
86- .flatMapMany (rsInstdata -> Mono .just (mapperFacade .mapAsList (rsInstdata , Instdata .class , context )))
90+ return Mono .just (bestilling )
91+ .filter (_ -> !bestilling .getInstdata ().isEmpty () && !bestilling .getEnvironments ().isEmpty ())
92+ .flatMap (_ -> oppdaterStatus (progress , prepInitStatus (INST2_STATUS , bestilling .getEnvironments ())))
93+ .flatMapMany (_ -> {
94+ var context = MappingContextUtils .getMappingContext ();
95+ context .setProperty ("ident" , dollyPerson .getIdent ());
96+ return Mono .just (mapperFacade .mapAsList (bestilling .getInstdata (), Instdata .class , context ));
97+ })
8798 .flatMap (instdata -> instdataConsumer .getMiljoer ()
8899 .flatMapMany (miljoer -> Flux .fromIterable (miljoer .getInstitusjonsoppholdEnvironments ())
89100 .filter (miljoe -> bestilling .getEnvironments ().contains (miljoe ))
@@ -102,13 +113,20 @@ private Mono<BestillingProgress> oppdaterStatus(BestillingProgress progress, Str
102113 public void release (List <String > identer ) {
103114
104115 instdataConsumer .deleteInstdata (identer )
105- .subscribe (_ -> log .info ("Slettet identer fra Instdata (inst 2)" ));
116+ .subscribe (
117+ _ -> log .info ("Slettet identer fra Instdata (inst 2)" ),
118+ throwable -> log .error ("Feil ved sletting av identer fra Instdata (inst 2)" , throwable ));
106119 instdataConsumer .deleteInstKdiData (identer )
107- .subscribe (_ -> log .info ("Slettet identer fra Institusjonsopphold fengsel (KDI)" ));
120+ .subscribe (
121+ _ -> log .info ("Slettet identer fra Institusjonsopphold fengsel (KDI)" ),
122+ throwable -> log .error ("Feil ved sletting av identer fra Institusjonsopphold fengsel (KDI)" , throwable ));
108123 }
109124
110125 private Mono <List <Instdata >> filterInstdata (List <Instdata > instdataRequest , String miljoe ) {
111126
127+ if (instdataRequest .isEmpty ()) {
128+ return Mono .just (emptyList ());
129+ }
112130 return instdataConsumer .getInstdata (instdataRequest .getFirst ().getNorskident (), miljoe )
113131 .map (eksisterende -> {
114132 log .info ("Instdata hentet data fra {}: {}" , miljoe , eksisterende .getInstitusjonsopphold ());
@@ -121,6 +139,14 @@ private Mono<List<Instdata>> filterInstdata(List<Instdata> instdataRequest, Stri
121139 });
122140 }
123141
142+ private String prepInitStatus (String system , Set <String > miljoer ) {
143+
144+ var miljoerString = miljoer .stream ()
145+ .map (miljo -> String .format ("%s:%s" , miljo , getInfoVenter (INSTDATA )))
146+ .collect (Collectors .joining ("," ));
147+ return String .format (system , miljoerString );
148+ }
149+
124150 private String getStatus (InstdataResponse response ) {
125151
126152 return response .getStatus ().is2xxSuccessful () ? "OK" :
@@ -148,11 +174,11 @@ private Mono<String> postInstdata(boolean isNewOpphold, List<Instdata> instdata,
148174 private Mono <String > postInstdataKdi (InstdataKdiDTO instdata , String ident ) {
149175
150176 return instdataConsumer .postInstdataKdi (instdata , ident )
151- .map (response -> String . format ( "%s:%s" .formatted (
177+ .map (response -> "%s:%s" .formatted (
152178 instdata .getEnvironment (),
153179 response .getStatus ().is2xxSuccessful () ? "OK" :
154180 ErrorStatusDecoder .encodeStatus (errorStatusDecoder .getErrorText (response .getStatus (),
155- response .getFeilmelding ()))))) ;
181+ response .getFeilmelding ()))));
156182
157183 }
158184}
0 commit comments