public String registerPatient(Patient patient) throws ParentNotFoundException, PatientIdNotUniqueException, PatientIdIncorrectFormatException { try { if (StringUtils.isEmpty(patient.getMrsPatient().getMotechId())) { MRSPatient mrsPatient = patient.getMrsPatient(); String motechId = identifierGenerationService.newPatientId(); patient = new Patient( new MRSPatient( mrsPatient.getId(), motechId, mrsPatient.getPerson(), mrsPatient.getFacility()), patient.getParentId()); } String savedPatientId = allPatients.save(patient); if (StringUtils.isNotEmpty(patient.getParentId())) { createRelationship(patient.getParentId(), savedPatientId); } return savedPatientId; } catch (IdentifierNotUniqueException e) { throw new PatientIdNotUniqueException(); } catch (InvalidCheckDigitException e) { throw new PatientIdIncorrectFormatException(); } }
@Test @Transactional(readOnly = true) public void shouldVoidObservationsEvenWithDuplicateCalls() throws UserAlreadyExistsException, ObservationNotFoundException { MRSPerson personCreator = new MRSPerson().firstName("SampleTest"); MRSPerson personJohn = new MRSPerson().firstName("John"); patientAlan = createPatient(facility); MRSUser userCreator = createUser( new MRSUser() .userName("UserSuper") .systemId("1000015") .securityRole("Provider") .person(personCreator)); MRSUser userJohn = createUser( new MRSUser() .userName("UserJohn") .systemId("1000027") .securityRole("Provider") .person(personJohn)); MRSPerson provider = userJohn.getPerson(); Set<MRSObservation> observations = new HashSet<MRSObservation>(); observations.add(new MRSObservation<Double>(new Date(), "GRAVIDA", Double.valueOf("100.0"))); final String encounterType = "PEDSRETURN"; MRSEncounter expectedEncounter = new MRSEncounter( provider.getId(), userCreator.getId(), facility.getId(), new Date(), patientAlan.getId(), observations, encounterType); mrsEncounterAdapter.createEncounter(expectedEncounter); final MRSObservation mrsObservation = openMrsObservationAdapter.findObservation(patientAlan.getMotechId(), "GRAVIDA"); openMrsObservationAdapter.voidObservation( mrsObservation, "reason 1", patientAlan.getMotechId()); final String reason2 = "reason 2"; openMrsObservationAdapter.voidObservation(mrsObservation, reason2, patientAlan.getMotechId()); final Obs actualOpenMRSObs = obsService.getObs(Integer.valueOf(mrsObservation.getId())); assertTrue(actualOpenMRSObs.isVoided()); assertThat(actualOpenMRSObs.getVoidReason(), is(reason2)); }