@Test public void testCheckIfPersonHasExistingUtkast() { final String personId = PERSON_ID; final Set activeModules = new HashSet<>(Arrays.asList("db", "doi")); final String vardgivareId = "vardgivarid"; Utkast db1 = createUtkast("db1", 1L, "db", UtkastStatus.DRAFT_INCOMPLETE, null, "", null, PERSONNUMMER); db1.setVardgivarId("other"); Utkast db2 = createUtkast("db2", 1L, "db", UtkastStatus.DRAFT_INCOMPLETE, null, "", null, PERSONNUMMER); db2.setVardgivarId(vardgivareId); Utkast doi = createUtkast("doi1", 1L, "doi", UtkastStatus.DRAFT_INCOMPLETE, null, "", null, PERSONNUMMER); doi.setVardgivarId("other"); when(authoritiesHelper.getIntygstyperForFeature(any(), any(), any())).thenReturn(activeModules); when(utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(PERSONNUMMER.getPersonnummerWithDash(), activeModules)) .thenReturn(Arrays.asList(db1, db2, doi)); Map<String, Map<String, PreviousIntyg>> res = utkastService.checkIfPersonHasExistingIntyg(PERSONNUMMER, createUser(), null); assertNotNull(res.get("utkast")); assertTrue(res.get("utkast").get("db").isSameVardgivare()); assertEquals(res.get("utkast").get("db").getLatestIntygsId(), "db2"); assertFalse(res.get("utkast").get("doi").isSameVardgivare()); verify(utkastRepository).findAllByPatientPersonnummerAndIntygsTypIn(eq(PERSONNUMMER.getPersonnummerWithDash()), eq(activeModules)); }
@Override public Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer, final IntygUser user, final String currentDraftId) { List<Utkast> toFilter = utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(personnummer.getPersonnummerWithDash(), authoritiesHelper.getIntygstyperForFeature(user, AuthoritiesConstants.FEATURE_UNIKT_INTYG, AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG)); List<Utkast> signedList = toFilter.stream() .filter(utkast -> utkast.getStatus() == UtkastStatus.SIGNED) .filter(utkast -> utkast.getAterkalladDatum() == null) .sorted(Comparator.comparing(u -> u.getSignatur().getSigneringsDatum())) .collect(Collectors.toList()); Map<String, Map<String, PreviousIntyg>> ret = new HashMap<>(); ret.put(INTYG_INDICATOR, signedList.stream() .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); ret.put(UTKAST_INDICATOR, toFilter.stream() .filter(utkast -> utkast.getStatus() != UtkastStatus.SIGNED && utkast.getStatus() != UtkastStatus.DRAFT_LOCKED && !utkast.getIntygsId().equals(currentDraftId)) .sorted(Comparator.comparing(Utkast::getSkapad, Comparator.nullsFirst(Comparator.naturalOrder()))) .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); return ret; }
UtkastServiceImpl implements UtkastService { @Override public Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer, final IntygUser user, final String currentDraftId) { List<Utkast> toFilter = utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(personnummer.getPersonnummerWithDash(), authoritiesHelper.getIntygstyperForFeature(user, AuthoritiesConstants.FEATURE_UNIKT_INTYG, AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG)); List<Utkast> signedList = toFilter.stream() .filter(utkast -> utkast.getStatus() == UtkastStatus.SIGNED) .filter(utkast -> utkast.getAterkalladDatum() == null) .sorted(Comparator.comparing(u -> u.getSignatur().getSigneringsDatum())) .collect(Collectors.toList()); Map<String, Map<String, PreviousIntyg>> ret = new HashMap<>(); ret.put(INTYG_INDICATOR, signedList.stream() .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); ret.put(UTKAST_INDICATOR, toFilter.stream() .filter(utkast -> utkast.getStatus() != UtkastStatus.SIGNED && utkast.getStatus() != UtkastStatus.DRAFT_LOCKED && !utkast.getIntygsId().equals(currentDraftId)) .sorted(Comparator.comparing(Utkast::getSkapad, Comparator.nullsFirst(Comparator.naturalOrder()))) .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); return ret; } }
UtkastServiceImpl implements UtkastService { @Override public Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer, final IntygUser user, final String currentDraftId) { List<Utkast> toFilter = utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(personnummer.getPersonnummerWithDash(), authoritiesHelper.getIntygstyperForFeature(user, AuthoritiesConstants.FEATURE_UNIKT_INTYG, AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG)); List<Utkast> signedList = toFilter.stream() .filter(utkast -> utkast.getStatus() == UtkastStatus.SIGNED) .filter(utkast -> utkast.getAterkalladDatum() == null) .sorted(Comparator.comparing(u -> u.getSignatur().getSigneringsDatum())) .collect(Collectors.toList()); Map<String, Map<String, PreviousIntyg>> ret = new HashMap<>(); ret.put(INTYG_INDICATOR, signedList.stream() .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); ret.put(UTKAST_INDICATOR, toFilter.stream() .filter(utkast -> utkast.getStatus() != UtkastStatus.SIGNED && utkast.getStatus() != UtkastStatus.DRAFT_LOCKED && !utkast.getIntygsId().equals(currentDraftId)) .sorted(Comparator.comparing(Utkast::getSkapad, Comparator.nullsFirst(Comparator.naturalOrder()))) .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); return ret; }  }
UtkastServiceImpl implements UtkastService { @Override public Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer, final IntygUser user, final String currentDraftId) { List<Utkast> toFilter = utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(personnummer.getPersonnummerWithDash(), authoritiesHelper.getIntygstyperForFeature(user, AuthoritiesConstants.FEATURE_UNIKT_INTYG, AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG)); List<Utkast> signedList = toFilter.stream() .filter(utkast -> utkast.getStatus() == UtkastStatus.SIGNED) .filter(utkast -> utkast.getAterkalladDatum() == null) .sorted(Comparator.comparing(u -> u.getSignatur().getSigneringsDatum())) .collect(Collectors.toList()); Map<String, Map<String, PreviousIntyg>> ret = new HashMap<>(); ret.put(INTYG_INDICATOR, signedList.stream() .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); ret.put(UTKAST_INDICATOR, toFilter.stream() .filter(utkast -> utkast.getStatus() != UtkastStatus.SIGNED && utkast.getStatus() != UtkastStatus.DRAFT_LOCKED && !utkast.getIntygsId().equals(currentDraftId)) .sorted(Comparator.comparing(Utkast::getSkapad, Comparator.nullsFirst(Comparator.naturalOrder()))) .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); return ret; }  static boolean isUtkast(Utkast utkast); static boolean isEditableUtkast(Utkast utkast); @Override @Transactional() // , readOnly=true int countFilterIntyg(UtkastFilter filter); @Override Utkast createNewDraft(CreateNewDraftRequest request); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, String toUtkastId, String toUtkastType); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, Utkast toUtkast); @Override String getQuestions(String intygsTyp, String version); @Override @Transactional void setKlarForSigneraAndSendStatusMessage(String intygsId, String intygType); @Override Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer,
                                                                                 final IntygUser user,
                                                                                 final String currentDraftId); @Override @Transactional void deleteUnsignedDraft(String intygId, long version); @Override @Transactional(readOnly = true) List<Utkast> filterIntyg(UtkastFilter filter); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType, boolean createPdlLogEvent); @Override @Transactional(readOnly = true) List<Lakare> getLakareWithDraftsByEnhet(String enhetsId); @Override @Transactional(readOnly = true) Map<String, Long> getNbrOfUnsignedDraftsByCareUnits(List<String> careUnitIds); @Override @Transactional SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent); @Override void updatePatientOnDraft(UpdatePatientOnDraftRequest request); @Override @Transactional Utkast setNotifiedOnDraft(String intygsId, long version, Boolean notified); @Override DraftValidation validateDraft(String intygId, String intygType, String draftAsJson); @Override @Transactional int lockOldDrafts(int lockedAfterDay, LocalDate today); @Override void revokeLockedDraft(String intygId, String intygTyp, String revokeMessage, String reason); @Override boolean isDraftCreatedFromReplacement(String certificateId); }
UtkastServiceImpl implements UtkastService { @Override public Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer, final IntygUser user, final String currentDraftId) { List<Utkast> toFilter = utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(personnummer.getPersonnummerWithDash(), authoritiesHelper.getIntygstyperForFeature(user, AuthoritiesConstants.FEATURE_UNIKT_INTYG, AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG)); List<Utkast> signedList = toFilter.stream() .filter(utkast -> utkast.getStatus() == UtkastStatus.SIGNED) .filter(utkast -> utkast.getAterkalladDatum() == null) .sorted(Comparator.comparing(u -> u.getSignatur().getSigneringsDatum())) .collect(Collectors.toList()); Map<String, Map<String, PreviousIntyg>> ret = new HashMap<>(); ret.put(INTYG_INDICATOR, signedList.stream() .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); ret.put(UTKAST_INDICATOR, toFilter.stream() .filter(utkast -> utkast.getStatus() != UtkastStatus.SIGNED && utkast.getStatus() != UtkastStatus.DRAFT_LOCKED && !utkast.getIntygsId().equals(currentDraftId)) .sorted(Comparator.comparing(Utkast::getSkapad, Comparator.nullsFirst(Comparator.naturalOrder()))) .collect(Collectors.groupingBy(Utkast::getIntygsTyp, Collectors.mapping(utkast -> PreviousIntyg.of( Objects.equals(user.getValdVardgivare().getId(), utkast.getVardgivarId()), Objects.equals(user.getValdVardenhet().getId(), utkast.getEnhetsId()), utkast.getEnhetsNamn(), utkast.getIntygsId(), utkast.getSkapad()), Collectors.reducing(new PreviousIntyg(), (a, b) -> b.isSameVardgivare() ? b : a))))); return ret; }  static boolean isUtkast(Utkast utkast); static boolean isEditableUtkast(Utkast utkast); @Override @Transactional() // , readOnly=true int countFilterIntyg(UtkastFilter filter); @Override Utkast createNewDraft(CreateNewDraftRequest request); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, String toUtkastId, String toUtkastType); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, Utkast toUtkast); @Override String getQuestions(String intygsTyp, String version); @Override @Transactional void setKlarForSigneraAndSendStatusMessage(String intygsId, String intygType); @Override Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer,
                                                                                 final IntygUser user,
                                                                                 final String currentDraftId); @Override @Transactional void deleteUnsignedDraft(String intygId, long version); @Override @Transactional(readOnly = true) List<Utkast> filterIntyg(UtkastFilter filter); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType, boolean createPdlLogEvent); @Override @Transactional(readOnly = true) List<Lakare> getLakareWithDraftsByEnhet(String enhetsId); @Override @Transactional(readOnly = true) Map<String, Long> getNbrOfUnsignedDraftsByCareUnits(List<String> careUnitIds); @Override @Transactional SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent); @Override void updatePatientOnDraft(UpdatePatientOnDraftRequest request); @Override @Transactional Utkast setNotifiedOnDraft(String intygsId, long version, Boolean notified); @Override DraftValidation validateDraft(String intygId, String intygType, String draftAsJson); @Override @Transactional int lockOldDrafts(int lockedAfterDay, LocalDate today); @Override void revokeLockedDraft(String intygId, String intygTyp, String revokeMessage, String reason); @Override boolean isDraftCreatedFromReplacement(String certificateId);  }