Skip to content

Commit

Permalink
Merge pull request #1256 from RoundingWell/feature/refresh-stale-forms
Browse files Browse the repository at this point in the history
Refresh stale forms after 30 min of no change
  • Loading branch information
paulfalgout committed Apr 25, 2024
2 parents 30d6b6a + 30777a6 commit b259685
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/js/apps/forms/form/form_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default App.extend({
const storedState = store.get(`form-state_${ this.currentUser.id }`);

this.setState(extend({
responseId: null,
isActionSidebar: true,
isExpanded: true,
shouldShowHistory: false,
Expand All @@ -57,12 +58,14 @@ export default App.extend({
this.initFormState();
},
beforeStart({ patientActionId }) {
this.patientActionId = this.patientActionId || patientActionId;

return [
Radio.request('entities', 'fetch:forms:byAction', patientActionId),
Radio.request('entities', 'fetch:actions:withResponses', patientActionId),
Radio.request('entities', 'fetch:patients:model:byAction', patientActionId),
Radio.request('entities', 'fetch:forms:byAction', this.patientActionId),
Radio.request('entities', 'fetch:actions:withResponses', this.patientActionId),
Radio.request('entities', 'fetch:patients:model:byAction', this.patientActionId),
Radio.request('entities', 'fetch:formResponses:latest', {
action: patientActionId,
action: this.patientActionId,
status: FORM_RESPONSE_STATUS.ANY,
editor: this.currentUser.id,
}),
Expand Down Expand Up @@ -122,6 +125,7 @@ export default App.extend({
'error': 'onFormServiceError',
'ready': 'onFormServiceReady',
'update:submission': 'onFormServiceUpdateSubmission',
'refresh': 'onFormServiceRefresh',
},
shouldSaveAndGoBack() {
const saveButtonType = this.getState('saveButtonType');
Expand Down Expand Up @@ -169,6 +173,9 @@ export default App.extend({
onFormServiceUpdateSubmission(updated) {
this.showLastUpdated(updated);
},
onFormServiceRefresh() {
this.restart();
},
stateEvents: {
'change': 'onChangeState',
'change:isExpanded': 'showSidebar',
Expand Down
15 changes: 13 additions & 2 deletions src/js/services/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export default App.extend({
},
initialize(options) {
this.updateDraft = debounce(this.updateDraft, 15000);
this.refreshForm = debounce(this.refreshForm, 1800000);

this.mergeOptions(options, ['action', 'form', 'patient', 'responses', 'latestResponse']);

this.currentUser = Radio.request('bootstrap', 'currentUser');
},
radioRequests: {
Expand All @@ -48,6 +51,8 @@ export default App.extend({
},
readyForm() {
this.trigger('ready');

this.refreshForm();
},
checkVersion(feVersion) {
/* istanbul ignore if: can't test reload */
Expand Down Expand Up @@ -106,6 +111,7 @@ export default App.extend({
}

this.updateDraft();
this.refreshForm();
},
clearStoredSubmission() {
this.latestResponse = null;
Expand Down Expand Up @@ -317,9 +323,13 @@ export default App.extend({
this.trigger('error', responseData.errors);
});
},
refreshForm() {
this.trigger('refresh');
},
submitForm({ response }) {
// Cancel any pending draft updates
// Cancel any pending draft updates or stale form refreshes
this.updateDraft.cancel();
this.refreshForm.cancel();

const data = this.useLatestDraft({
response,
Expand All @@ -336,8 +346,9 @@ export default App.extend({

return formResponse.saveAll()
.then(() => {
// Cancel any draft updates that may have been queued while the form was submitting
// Cancel any draft updates or stale form refreshes that may have been queued while the form was submitting
this.updateDraft.cancel();
this.refreshForm.cancel();
this.clearStoredSubmission();
this.trigger('success', formResponse);
}).catch(({ responseData }) => {
Expand Down
121 changes: 121 additions & 0 deletions test/integration/forms/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -2532,4 +2532,125 @@ context('Patient Action Form', function() {
.its('search')
.should('contain', `filter[created]=<=${ createdAt }`);
});

specify('refresh stale form', function() {
cy
.routeAction(fx => {
fx.data = getAction({
id: '1',
relationships: {
'form': getRelationship('11111', 'forms'),
'form-responses': getRelationship([{ id: '11111' }], 'form-responses'),
},
});

return fx;
})
.routeFormByAction(_.identity, '11111')
.routeLatestFormResponse()
.routeFormDefinition()
.routeFormActionFields()
.routePatientByAction()
.visitOnClock('/patient-action/1/form/11111', { now: testTs() })
.wait('@routeAction')
.wait('@routeFormByAction')
.wait('@routeLatestFormResponse')
.wait('@routePatientByAction')
.wait('@routeFormDefinition');

cy.wait(300);

cy
.get('.form__controls')
.find('.js-save-button')
.should('contain', 'Submit');

cy
.iframe()
.find('textarea[name="data[familyHistory]"]')
.should('be.empty');

cy
.routeLatestFormResponse(() => {
return {
data: getFormResponse({
attributes: {
status: FORM_RESPONSE_STATUS.DRAFT,
updated_at: testTsSubtract(1),
response: {
data: {
familyHistory: 'Form draft work done in another tab.',
},
},
},
}),
};
});

cy
.tick(1800000)
.wait('@routeLatestFormResponse')
.wait('@routePatientByAction');

cy
.get('.form__content')
.find('.js-submit')
.click();

cy
.wait('@routeFormByAction')
.wait('@routeFormDefinition');

cy.wait(300);

cy
.get('.form__controls')
.find('.js-save-button')
.should('contain', 'Submit');

cy
.iframe()
.find('textarea[name="data[familyHistory]"]')
.should('contain', 'Form draft work done in another tab.');

cy
.routeFormResponse(fx => {
fx.data = getFormResponse({
attributes: {
status: FORM_RESPONSE_STATUS.SUBMITTED,
updated_at: testTs(),
response: {
data: {
familyHistory: 'Form work submitted in another tab.',
},
},
},
});

return fx;
});

cy
.tick(1800000)
.wait('@routeFormResponse')
.wait('@routePatientByAction')
.wait('@routeFormByAction')
.wait('@routeFormDefinition');

cy.wait(300);

cy
.get('.form__controls')
.find('button')
.contains('Update');

cy
.iframe()
.should('contain', 'Form work submitted in another tab.');

cy
.iframe()
.find('textarea')
.should('not.exist');
});
});

0 comments on commit b259685

Please sign in to comment.