Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML-722: Encounter edit should not overwrite "unrelated" workflow state. #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,94 @@ public void testBlankFormHtml(String html) {
}.run();
}

@Test
public void shouldNotOverwritePreviousStateOnEdit() throws Exception {
//Given: Patient has a workflow state of X starting in Jan 2012
transitionToState(START_STATE, PAST_DATE);

new RegressionTestHelper() {

@Override
public String getFormName() {
return XML_FORM_NAME;
}

@Override
public String[] widgetLabels() {
return new String[] { "Date:", "Location:", "Provider:", "State:" };
}

@Override
public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
request.addParameter(widgets.get("Location:"), "2");
request.addParameter(widgets.get("Provider:"), "502");

//When: Html form is being back entered with an encounter date of June 2011, in which the workflow state selected is Y
request.addParameter(widgets.get("Date:"), dateAsString(DATE));
//request.addParameter(widgets.get("State:"), START_STATE);
}

@Override
public void testResults(SubmissionResults results) {
results.assertNoErrors();
results.assertEncounterCreated();
results.assertProvider(502);
results.assertLocation(2);
}

@Override
public boolean doEditEncounter() {
return true;
}

@Override
public String[] widgetLabelsForEdit() {
return new String[] { "Date:", "Location:", "Provider:", "State:" };
}

@Override
public void setupEditRequest(MockHttpServletRequest request, Map<String, String> widgets) {
request.setParameter(widgets.get("Location:"), "2");
request.setParameter(widgets.get("Provider:"), "502");
request.setParameter(widgets.get("Date:"), dateAsString(DATE));
request.setParameter(widgets.get("State:"), MIDDLE_STATE);
}

@Override
public void testEditedResults(SubmissionResults results) {
results.assertNoErrors();

ProgramWorkflowState firstState = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
ProgramWorkflowState secondState = Context.getProgramWorkflowService().getStateByUuid(MIDDLE_STATE);

//verify existing behavior does match expectations for the second state
PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), secondState, DATE);
PatientState currentPatientState = getPatientState(patientProgram, secondState, DATE);
Assert.assertNotNull(patientProgram);
Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(patientProgram.getDateEnrolled()));
Assert.assertEquals(dateAsString(DATE), dateAsString(currentPatientState.getStartDate()));
Assert.assertNull(patientProgram.getDateCompleted());
Assert.assertNull(currentPatientState.getEndDate());

//illustrate that this second state has overwritten the existing state
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is now incorrect, I believe? This was when it was showing the error case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought as a regression test it would still make sense. I can update or remove it if you prefer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update or remove, whatever works for you!

Assert.assertEquals(2, patientProgram.getStates().size());

//after the fix for this is in place, the following should also pass
PatientState previousPatientState = getPatientState(patientProgram, firstState, PAST_DATE);

Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(previousPatientState.getStartDate()));
Assert.assertEquals(dateAsString(DATE), dateAsString(previousPatientState.getEndDate()));

List<PatientProgram> patientPrograms = Context.getProgramWorkflowService().getPatientPrograms(patient,
patientProgram.getProgram(), null, null, null, null, false);

//only one program enrollment should exist
Assert.assertEquals(1, patientPrograms.size());
}
}.run();

}

@Test
public void checkboxShouldNotAppearCheckedIfNotCurrentlyInSpecifiedState() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,23 @@ public void handleSubmission(FormEntrySession session, HttpServletRequest submis
if (!StringUtils.isBlank(stateUuid)) {
if (Mode.EDIT.equals(session.getContext().getMode())) {

Date prevDate = session.getContext().getPreviousEncounterDate();

if (prevDate == null) {
prevDate = session.getEncounter().getEncounterDatetime();
}

ProgramWorkflowState newState = Context.getProgramWorkflowService().getStateByUuid(stateUuid);
PatientState oldPatientState = getActivePatientState(session.getContext().getExistingPatient(), session
.getContext().getPreviousEncounterDate(), workflow);
PatientState oldPatientState = getActivePatientState(session.getContext().getExistingPatient(), prevDate, workflow);

//since there is no direct connection between encounters and states
//the best that can be done, for now, is to check
//if the encounterDatetime exactly matches the startDate.
//If so, we presume the state was originally set via this form
//and therefore we need to modify it
if (oldPatientState != null && !oldPatientState.getStartDate().equals(prevDate)) {
oldPatientState = null;
}

// if no old state, simply transition to this new state
if (oldPatientState == null) {
Expand All @@ -226,7 +240,7 @@ public void handleSubmission(FormEntrySession session, HttpServletRequest submis
// it is picked up by the FormSubmissionAction.transitionToState method and a new program is not created)
PatientProgram patientProgram = HtmlFormEntryUtil.getPatientProgramByProgramOnDate(session.getPatient(),
newState.getProgramWorkflow().getProgram(), session.getEncounter().getEncounterDatetime());

if (patientProgram != null) {
session.getSubmissionActions().getPatientProgramsToUpdate().add(patientProgram);
}
Expand Down