@Test public void getDbCandidateMetaDataShouldReturnCorrectCandidate() { String intygTypeCandidate = "db"; String intygTypeVersion = "1.0"; String intygType = "doi"; when(webCertUser.getValdVardgivare()).thenReturn(createSelectableVardenhet("same-care-giver-hsaid")); when(webCertUser.getValdVardenhet()).thenReturn(createSelectableVardenhet("same-care-unit-hsaid")); when(webCertUser.getHsaId()).thenReturn("correct-user-hsaid"); when(webCertUserService.isUserAllowedAccessToUnit("other-care-unit-hsaid")).thenReturn(false); when(draftAccessService.allowToCopyFromCandidate(anyString(), any(Personnummer.class))). thenReturn(AccessResult.create(AccessResultCode.NO_PROBLEM, "")); Optional<GetCopyFromCriteria> copyFromCriteria = Optional.of(new GetCopyFromCriteria(intygTypeCandidate, "1", -1)); when(doiModuleApiV1Mock.getCopyFromCriteria()).thenReturn(copyFromCriteria); when(logRequestFactory.createLogRequestFromUtkast(any(Utkast.class), anyBoolean())).thenReturn(new LogRequest()); Patient patient = createPatient("Lilltolvan", "Tolvansson", createPnr("20121212-1212")); Set<String> validIntygType = new HashSet<>(); validIntygType.add(copyFromCriteria.get().getIntygType()); when(webCertUser.isLakare()).thenReturn(true); List<Utkast> candidates = Arrays.asList( createDbCandidate(UtkastStatus.DRAFT_COMPLETE, "same-care-unit-hsaid", "same-care-giver-hsaid", null, null, null, "certificate-id-1", intygTypeCandidate, intygTypeVersion, LocalDateTime.now().minusDays(1), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "same-care-unit-hsaid", "other-care-giver-hsaid", null, null, null, "certificate-id-2", intygTypeCandidate, intygTypeVersion, LocalDateTime.now().minusDays(2), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "same-care-unit-hsaid", "same-care-giver-hsaid", LocalDateTime.now().minusDays(1), null, null, "certificate-id-3", intygTypeCandidate, intygTypeVersion, LocalDateTime.now().minusDays(3), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "same-care-unit-hsaid", "same-care-giver-hsaid", null, null, null, "certificate-id-4", intygTypeCandidate, intygTypeVersion, LocalDateTime.now().minusDays(4), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "same-care-unit-hsaid", "same-care-giver-hsaid", null, null, null, "certificate-id-5", intygTypeCandidate, "99.0", LocalDateTime.now().minusDays(5), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "same-care-unit-hsaid", "same-care-giver-hsaid", null, null, null, "certificate-id-6", "lisjp", intygTypeVersion, LocalDateTime.now().minusDays(6), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "same-care-unit-hsaid", "same-care-giver-hsaid", null, RelationKod.ERSATT, "certificate-id-4", "certificate-id-7", intygTypeCandidate, intygTypeVersion, LocalDateTime.now().minusDays(8), "correct-user-hsaid"), createDbCandidate(UtkastStatus.SIGNED, "other-care-unit-hsaid", "same-care-giver-hsaid", null, null, null, "certificate-id-8", intygTypeCandidate, intygTypeVersion, LocalDateTime.now().minusDays(7), "correct-user-hsaid") ); when(utkastRepository.findAllByPatientPersonnummerAndIntygsTypIn(patient.getPersonId().getPersonnummerWithDash(), validIntygType)) .thenReturn(candidates); Optional<UtkastCandidateMetaData> metaData = utkastCandidateService.getCandidateMetaData(doiModuleApiV1Mock, intygType, patient, false); assertTrue(metaData.isPresent()); assertEquals(metaData.get().getIntygId(), "certificate-id-8"); assertEquals(metaData.get().getSameVardenhet(), false); verify(logService, times(1)).logReadIntyg(any(LogRequest.class), any(LogUser.class)); }
public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } } }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }  }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }  Optional<UtkastCandidateMetaData> getCandidateMetaData(
        ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling); }
UtkastCandidateServiceImpl { public Optional<UtkastCandidateMetaData> getCandidateMetaData( ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling) { UtkastCandidateMetaData metaData = null; final Optional<GetCopyFromCriteria> copyFromCriteria = moduleApi.getCopyFromCriteria(); if (!copyFromCriteria.isPresent()) { return Optional.empty(); } AccessResult accessResult = draftAccessService.allowToCopyFromCandidate(intygType, patient.getPersonId()); if (accessResult.isDenied()) { return Optional.empty(); } if (!getUser().isLakare() && patient.isSekretessmarkering()) { return Optional.empty(); } try { Optional<Utkast> candidate = findCandidate(copyFromCriteria.get(), patient); if (candidate.isPresent()) { Utkast utkast = candidate.get(); boolean sameVardenhet = webCertUserService.isUserAllowedAccessToUnit(utkast.getEnhetsId()); metaData = new UtkastCandidateMetaData.Builder() .with(builder -> { builder.intygId = utkast.getIntygsId(); builder.intygType = utkast.getIntygsTyp(); builder.intygTypeVersion = utkast.getIntygTypeVersion(); builder.intygCreated = utkast.getSkapad(); builder.signedByHsaId = utkast.getSkapadAv().getHsaId(); builder.enhetHsaId = utkast.getEnhetsId(); builder.enhetName = utkast.getEnhetsNamn(); builder.sameVardenhet = sameVardenhet; }) .create(); logService.logReadIntyg( logRequestFactory.createLogRequestFromUtkast( utkast, isCoherentJournaling), LogUtil.getLogUser(getUser())); } return Optional.ofNullable(metaData); } catch (Exception e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Failed to lookup a candidate to copy certificate information from", e); } }  Optional<UtkastCandidateMetaData> getCandidateMetaData(
        ModuleApi moduleApi, String intygType, Patient patient, boolean isCoherentJournaling);  }