Skip to content

Commit

Permalink
feat. håndtere dupletter fra Arena (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikv authored Dec 6, 2024
1 parent eeffb73 commit 636a75c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class ArenaAgreementAggregate {
private String antallDagerPrUke;
private LocalDateTime tiltakStartdato;
private LocalDateTime tiltakSluttdato;
private String eksternId;
private String eksternIdTiltak;
private String eksternIdDeltaker;
private Integer prosentDeltid;
private Tiltakstatuskode tiltakstatuskode;
private Deltakerstatuskode deltakerstatuskode;
Expand All @@ -72,12 +73,29 @@ public Optional<LocalDate> findSluttdato() {
.findFirst();
}

public boolean isTiltakDublett() {
return isDublett(eksternIdTiltak);
}

public boolean isDeltakerDublett() {
return isDublett(eksternIdDeltaker);
}

public Optional<UUID> getEksternIdAsUuid() throws IllegalArgumentException {
Optional<String> eksternId = Stream.of(eksternIdTiltak, eksternIdDeltaker)
.filter(id -> !Strings.isNullOrEmpty(id) && !isDublett(id))
.findFirst();
try {
return Strings.isNullOrEmpty(eksternId) ? Optional.empty() : Optional.of(UUID.fromString(eksternId));
return eksternId.map(UUID::fromString);
} catch (IllegalArgumentException e) {
log.error("Arena-avtale har ugyldig UUID som ekstern id: {}. Fortsetter uten.", eksternId);
log.error("Arena-avtale har ugyldig UUID som ekstern id: {}. Fortsetter uten.", eksternId.orElse(null));
return Optional.empty();
}
}

private static boolean isDublett(String eksternId) {
return Optional.ofNullable(eksternId)
.map(id -> id.toLowerCase().contains("dublett"))
.orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.LocalDate;

public enum ArenaMigrationAction {
HOPP_OVER,
GJENOPPRETT,
OPPRETT,
OPPDATER,
Expand All @@ -20,6 +21,14 @@ public static ArenaMigrationAction map(ArenaAgreementAggregate agreementAggregat
boolean isSluttdatoInTheFuture = agreementAggregate.findSluttdato()
.map(sluttdato -> sluttdato.isAfter(LocalDate.now())).orElse(false);

if (agreementAggregate.isDeltakerDublett()) {
return HOPP_OVER;
}

if (agreementAggregate.isTiltakDublett()) {
return IGNORER;
}

return switch (deltakerstatuskode) {
case GJENN, TILBUD -> isSluttdatoInTheFuture ? OPPRETT : IGNORER;
case null, default -> IGNORER;
Expand All @@ -37,6 +46,14 @@ public static ArenaMigrationAction map(
boolean isSluttdatoIFremtiden = agreementAggregate.findSluttdato()
.map(sluttdato -> sluttdato.isAfter(LocalDate.now())).orElse(false);

if (agreementAggregate.isDeltakerDublett()) {
return HOPP_OVER;
}

if (agreementAggregate.isTiltakDublett()) {
return IGNORER;
}

return switch (avtalestatus) {
case ANNULLERT, AVBRUTT -> switch (deltakerstatuskode) {
case GJENN, TILBUD -> isSluttdatoIFremtiden ? (isFeilregistrert ? OPPRETT : GJENOPPRETT) : IGNORER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import no.nav.tag.tiltaksgjennomforing.avtale.Avtale;

public sealed interface ArenaMigrationProcessResult {
record Skip(ArenaMigrationAction action) implements ArenaMigrationProcessResult {}
record Ignored(ArenaMigrationAction action) implements ArenaMigrationProcessResult {}
record Completed(ArenaMigrationAction action, Avtale avtale) implements ArenaMigrationProcessResult {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface ArenaAgreementMigrationRepository extends JpaRepository<ArenaAg
atg.datoFra,
atg.datoTil,
atg.eksternId,
atd.eksternId,
atg.prosentDeltid,
atg.tiltakstatuskode,
atd.deltakerstatuskode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void process(ArenaAgreementAggregate agreementAggregate) {
completed.avtale().getId(),
completed.avtale().getStatus()
);
updateMigrationStatus(
saveMigrationStatus(
tiltaksgjennomforingId,
ArenaAgreementMigrationStatus.COMPLETED,
completed.action(),
Expand All @@ -106,17 +106,20 @@ public void process(ArenaAgreementAggregate agreementAggregate) {
);
}
case ArenaMigrationProcessResult.Ignored ignored ->
updateMigrationStatus(
saveMigrationStatus(
tiltaksgjennomforingId,
ArenaAgreementMigrationStatus.COMPLETED,
ignored.action(),
eksternId,
null
);
case ArenaMigrationProcessResult.Skip skip -> {
// Gjør ingenting
}
}
} catch(Exception e) {
log.error("Feil ved prossesering av avtale fra Arena", e);
updateMigrationStatus(
saveMigrationStatus(
tiltaksgjennomforingId,
ArenaAgreementMigrationStatus.FAILED,
null,
Expand All @@ -126,7 +129,7 @@ public void process(ArenaAgreementAggregate agreementAggregate) {
}
}

private void updateMigrationStatus(
private void saveMigrationStatus(
Integer id,
ArenaAgreementMigrationStatus status,
ArenaMigrationAction action,
Expand Down Expand Up @@ -169,13 +172,20 @@ private ArenaMigrationProcessResult updateAvtale(Avtale avtale, ArenaAgreementAg

ArenaMigrationAction action = ArenaMigrationAction.map(avtale, agreementAggregate);
switch (action) {
case HOPP_OVER -> {
log.info(
"Avtale har deltaker som er duplett i Arena. Hopper over avtalen. Den vil sannsynligvis bli oppdatert senere."
);
return new ArenaMigrationProcessResult.Skip(action);
}
case IGNORER -> {
log.info(
"Avtale med id {} og status {} har tiltakstatus {} og deltakerstatus {} i Arena. Ignorerer avtalen.",
"Avtale med id {} og status {} har tiltakstatus {}, deltakerstatus {} og tiltaket er {} i Arena. Ignorerer avtalen.",
avtale.getId(),
avtale.getStatus(),
agreementAggregate.getTiltakstatuskode(),
agreementAggregate.getDeltakerstatuskode()
agreementAggregate.getDeltakerstatuskode(),
agreementAggregate.isTiltakDublett() ? "dublett" : "ikke dublett"
);
return new ArenaMigrationProcessResult.Ignored(action);
}
Expand Down Expand Up @@ -224,12 +234,20 @@ private ArenaMigrationProcessResult updateAvtale(Avtale avtale, ArenaAgreementAg
private ArenaMigrationProcessResult createAvtale(ArenaAgreementAggregate agreementAggregate) {
ArenaMigrationAction action = ArenaMigrationAction.map(agreementAggregate);

if (ArenaMigrationAction.HOPP_OVER == action) {
log.info(
"Avtale har deltaker som er duplett i Arena. Hopper over avtalen. Den vil sannsynligvis bli oppdatert senere."
);
return new ArenaMigrationProcessResult.Skip(action);
}

if (ArenaMigrationAction.IGNORER == action) {
log.info(
"Avtale har tiltaksstatus {}, deltakerstatus {} og sluttdato {} i Arena. Ignorerer avtalen.",
"Avtale har tiltaksstatus {}, deltakerstatus {}, sluttdato {} og er {} i Arena. Ignorerer avtalen.",
agreementAggregate.getTiltakstatuskode(),
agreementAggregate.getDeltakerstatuskode(),
agreementAggregate.findSluttdato().orElse(null)
agreementAggregate.findSluttdato().orElse(null),
agreementAggregate.isTiltakDublett() ? "dublett" : "ikke dublett"
);
return new ArenaMigrationProcessResult.Ignored(action);
}
Expand Down

0 comments on commit 636a75c

Please sign in to comment.