11package no .nav .dolly .bestilling .instdata .service ;
22
33import lombok .RequiredArgsConstructor ;
4- import no .nav .dolly .bestilling .instdata .InstdataConsumer ;
5- import no .nav .dolly .bestilling .instdata .domain .InstdataKdiDTO ;
6- import no .nav .dolly .bestilling .instdata .domain .InstdataKdiResponse ;
74import no .nav .dolly .domain .resultset .RsDollyUtvidetBestilling ;
85import no .nav .dolly .domain .resultset .inst .RsInstdataKdi ;
96import no .nav .dolly .service .TransactionHelperService ;
7+ import org .apache .commons .lang3 .StringUtils ;
108import org .springframework .stereotype .Service ;
119import reactor .core .publisher .Mono ;
1210
1311import java .time .LocalDateTime ;
14- import java .util .Collection ;
1512import java .util .List ;
16- import java .util .stream .Stream ;
13+ import java .util .UUID ;
14+ import java .util .concurrent .atomic .AtomicInteger ;
1715
1816import static java .util .Objects .isNull ;
17+ import static org .apache .commons .lang3 .StringUtils .isBlank ;
1918
2019@ Service
2120@ RequiredArgsConstructor
@@ -27,43 +26,27 @@ public class InstKdiHendelseService {
2726 private static final String LOESLATELSE = "D" ;
2827 private static final String FORVENTET_LOESLATELSE = "E" ;
2928
30- private final InstdataConsumer instdataConsumer ;
3129 private final TransactionHelperService transactionHelperService ;
3230
33- public Mono <RsInstdataKdi > getOppdaterBestilling (RsDollyUtvidetBestilling bestilling , Long bestillingId , String ident , String miljoe , boolean isOpprettEndre ) {
31+ public Mono <RsInstdataKdi > getOppdaterBestilling (RsDollyUtvidetBestilling bestilling , Long bestillingId , boolean isOpprettEndre ) {
3432
35- if (isOpprettEndre || hasEmptyHendelseId (bestilling .getInstdataKdi ())) {
36-
37- return instdataConsumer .getInstdataKdi (ident , miljoe )
38- .filter (response -> response .getStatus ().is2xxSuccessful ())
39- .map (instdataKdiResponse -> getOppdaterBestilling (instdataKdiResponse , bestilling .getInstdataKdi (), bestillingId ))
33+ if (isOpprettEndre ) {
34+ return Mono .just (getOppdaterBestilling (bestilling .getInstdataKdi (), bestillingId ))
4035 .doOnNext (bestilling ::setInstdataKdi )
4136 .flatMap (oppdaterBestilling -> transactionHelperService .persister (bestillingId , bestilling )
4237 .then (Mono .just (oppdaterBestilling )));
43-
4438 } else {
4539 return Mono .just (bestilling .getInstdataKdi ());
4640 }
4741 }
4842
49- private RsInstdataKdi getOppdaterBestilling (InstdataKdiResponse instdataKdiResponse , RsInstdataKdi bestilling , Long bestillingId ) {
50-
51- var kdidata = instdataKdiResponse .getInstdataKdi ().getData ();
52-
53- var annuleringer = kdidata .getAnnullering ().stream ()
54- .map (InstdataKdiDTO .Annullering ::getHendelseId )
55- .toList ();
43+ private RsInstdataKdi getOppdaterBestilling (RsInstdataKdi bestilling , Long bestillingId ) {
5644
57- oppdaterHendelser (bestilling .getInnsettelse (), getHendelseId (kdidata .getInnsettelse (),
58- INNSETTELSE , bestillingId , annuleringer ));
59- oppdaterHendelser (bestilling .getAvbruddStart (), getHendelseId (kdidata .getAvbruddStart (),
60- AVBRUDD_START , bestillingId , annuleringer ));
61- oppdaterHendelser (bestilling .getAvbruddSlutt (), getHendelseId (kdidata .getAvbruddSlutt (),
62- AVBRUDD_SLUTT , bestillingId , annuleringer ));
63- oppdaterHendelser (bestilling .getLoeslatelse (), getHendelseId (kdidata .getLoeslatelse (),
64- LOESLATELSE , bestillingId , annuleringer ));
65- oppdaterHendelser (bestilling .getForventetLoeslatelse (), getHendelseId (kdidata .getForventetLoeslatelse (),
66- FORVENTET_LOESLATELSE , bestillingId , annuleringer ));
45+ oppdaterHendelser (bestilling .getInnsettelse (), INNSETTELSE , bestillingId );
46+ oppdaterHendelser (bestilling .getAvbruddStart (), AVBRUDD_START , bestillingId );
47+ oppdaterHendelser (bestilling .getAvbruddSlutt (), AVBRUDD_SLUTT , bestillingId );
48+ oppdaterHendelser (bestilling .getLoeslatelse (), LOESLATELSE , bestillingId );
49+ oppdaterHendelser (bestilling .getForventetLoeslatelse (), FORVENTET_LOESLATELSE , bestillingId );
6750
6851 bestilling .getAnnullering ()
6952 .forEach (annulering -> annulering .setPubliseringstidspunkt (
@@ -73,50 +56,26 @@ private RsInstdataKdi getOppdaterBestilling(InstdataKdiResponse instdataKdiRespo
7356 return bestilling ;
7457 }
7558
76- private static boolean hasEmptyHendelseId (RsInstdataKdi instdataKdi ) {
77-
78- return Stream .of (instdataKdi .getInnsettelse (), instdataKdi .getAvbruddStart (),
79- instdataKdi .getAvbruddSlutt (), instdataKdi .getLoeslatelse (),
80- instdataKdi .getForventetLoeslatelse ())
81- .flatMap (Collection ::stream )
82- .anyMatch (hendelse -> isNull (hendelse .getHendelseId ()));
83- }
84-
85- private static String getHendelseId (List <? extends InstdataKdiDTO .Hendelse > hendelser ,
86- String type ,
87- Long bestillingId ,
88- List <String > annuleringer ) {
59+ private static void oppdaterHendelser (List <? extends RsInstdataKdi .Hendelse > hendelser , String type , Long bestillingId ) {
8960
90- var hendelseId = getHendelseId ( hendelser , type , bestillingId );
61+ var loepenummer = new AtomicInteger ( 1 );
9162
92- return !annuleringer .contains (hendelseId ) ? hendelseId : getNextHendelseId (hendelseId );
93- }
94-
95- private static String getNextHendelseId (String hendelseId ) {
63+ hendelser .forEach (hendelse -> {
9664
97- return hendelseId .substring (0 , 21 ) + String .format ("%010d" , Long .parseLong (hendelseId .substring (21 )) + 1 );
65+ if (StringUtils .isBlank (hendelse .getMeldingId ())) {
66+ hendelse .setMeldingId (UUID .randomUUID ().toString ());
67+ }
68+ if (isBlank (hendelse .getHendelseId ())) {
69+ hendelse .setHendelseId (makeHendelseId (bestillingId , type , loepenummer .getAndIncrement ()));
70+ }
71+ if (isNull (hendelse .getPubliseringstidspunkt ())) {
72+ hendelse .setPubliseringstidspunkt (LocalDateTime .now ());
73+ }
74+ });
9875 }
9976
10077 private static String makeHendelseId (Long bestillingId , String type , Integer loepenummer ) {
10178
10279 return "0x%010d00000%s00%010d" .formatted (bestillingId , type , loepenummer );
10380 }
104-
105- private static String getHendelseId (List <? extends InstdataKdiDTO .Hendelse > hendelser , String type , Long bestillingId ) {
106-
107- return hendelser .stream ()
108- .map (InstdataKdiDTO .Hendelse ::getHendelseId )
109- .max (String ::compareTo )
110- .orElse (makeHendelseId (bestillingId , type , 1 ));
111- }
112-
113- private static void oppdaterHendelser (List <? extends RsInstdataKdi .Hendelse > hendelser , String hendelseId ) {
114-
115- hendelser .forEach (hendelse -> {
116- hendelse .setHendelseId (hendelseId );
117- if (isNull (hendelse .getPubliseringstidspunkt ())) {
118- hendelse .setPubliseringstidspunkt (LocalDateTime .now ());
119- }
120- });
121- }
12281}
0 commit comments