Skip to content

Commit

Permalink
Fetch program along with action activity for sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
luketlancaster committed Nov 5, 2019
1 parent c340868 commit 5c82684
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/js/apps/patients/sidebar/action-sidebar_app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import $ from 'jquery';
import Radio from 'backbone.radio';

import App from 'js/base/app';
Expand All @@ -17,7 +18,13 @@ export default App.extend({
},
beforeStart() {
if (this.action.isNew()) return;
return Radio.request('entities', 'fetch:actionEvents:collection', this.action.id);
const d = $.Deferred();
const fetchActivity = Radio.request('entities', 'fetch:actionEvents:collection', this.action.id);
const fetchProgram = Radio.request('entities', 'fetch:program:model:byAction', this.action.id);
$.when(fetchActivity, fetchProgram).done(([activity], program) => {
d.resolve(activity);
});
return d.promise();
},
onStart(options, activity) {
this.activity = activity;
Expand Down
4 changes: 4 additions & 0 deletions src/js/entities-service/entities/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const _Model = BaseModel.extend({
getState() {
return Radio.request('entities', 'states:model', this.get('_state'));
},
getProgram() {
if (!this.get('_program')) return;
return Radio.request('entities', 'programs:model', this.get('_program'));
},
});

const Model = Store(_Model, TYPE);
Expand Down
13 changes: 13 additions & 0 deletions src/js/entities-service/programs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import $ from 'jquery';
import Radio from 'backbone.radio';

import BaseEntity from 'js/base/entity-service';
import { _Model, Model, Collection } from './entities/programs';

Expand All @@ -8,6 +11,16 @@ const Entity = BaseEntity.extend({
'programs:collection': 'getCollection',
'fetch:programs:model': 'fetchModel',
'fetch:programs:collection': 'fetchCollection',
'fetch:program:model:byAction': 'fetchProgramByAction',
},
fetchProgramByAction(actionId) {
const d = $.Deferred();
const program = Radio.request('entities', 'programs:model');
program.fetch({ url: `/api/actions/${ actionId }/program` })
.done(() => {
d.resolve(program);
});
return d;
},
});

Expand Down
3 changes: 2 additions & 1 deletion src/js/i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ careOptsFrontend:
cancelBtn: Cancel
saveBtn: Save
activityViews:
actionProgramAssigned: '<strong>{ name }</strong> ({ role }) added this Action from the { program } program'
clinicianAssigned: '<strong>{ name }</strong> ({ role }) changed the Owner to { to_name }'
created: "<strong>{ name }</strong> ({ role }) added this Action"
created: '<strong>{ name }</strong> ({ role }) added this Action'
createdAt: Created
detailsUpdated: '<strong>{ name }</strong> ({ role }) changed the details of this Action'
dueDateCleared: '<strong>{ name }</strong> ({ role }) cleared the Due Date'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const ClinicianAssignedTemplate = hbs`
<div>{{formatMoment date "AT_TIME"}}</div>
`;

const ActionProgramAssignedTemplate = hbs`
{{formatHTMLMessage (intlGet "patients.sidebar.action.activityViews.actionProgramAssigned") name = name role = role program = program}}
<div>{{formatMoment date "AT_TIME"}}</div>
`;

const DetailsUpdatedTemplate = hbs`
{{formatHTMLMessage (intlGet "patients.sidebar.action.activityViews.detailsUpdated") name = name role = role}}
<div>{{formatMoment date "AT_TIME"}}</div>
Expand Down Expand Up @@ -59,6 +64,7 @@ const ActivityView = View.extend({
const Templates = {
ActionCreated: CreatedTemplate,
ActionClinicianAssigned: ClinicianAssignedTemplate,
ActionProgramAssigned: ActionProgramAssignedTemplate,
ActionDetailsUpdated: DetailsUpdatedTemplate,
ActionDueDateUpdated: DueDateUpdatedTemplate,
ActionDurationUpdated: DurationUpdatedTemplate,
Expand All @@ -72,6 +78,7 @@ const ActivityView = View.extend({
templateContext() {
const editor = this.model.getEditor();
const clinician = this.model.getClinician();
const program = this.model.getProgram();
const toClinician = _.trim(`${ clinician.get('first_name') } ${ clinician.get('last_name') }`);
const role = editor.getRole().get('name');
const name = _.trim(`${ editor.get('first_name') } ${ editor.get('last_name') }`);
Expand All @@ -82,6 +89,7 @@ const ActivityView = View.extend({
to_clinician: toClinician,
to_role: this.model.getRole().get('name'),
to_state: this.model.getState().get('name'),
program: (program) ? program.get('name') : null,
};
},
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/config/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
};

return {
relationships: _.clone(baseRelationships),
relationships,
attributes: _.clone(baseAttributes),
};
},
Expand Down
34 changes: 33 additions & 1 deletion test/integration/patients/sidebar/action-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ context('action sidebar', function() {
})
.routePrograms()
.routeAllProgramActions()
.routeProgramByAction()
.visit('/patient/1/action')
.wait('@routePatientActions')
.wait('@routePatient')
Expand Down Expand Up @@ -237,8 +238,38 @@ context('action sidebar', function() {
fx.data[0].relationships.editor.data = null;
fx.data[0].attributes.date = now.format();

fx.data.push({
id: 'BBBBB',
type: 'events',
attributes: {
date: now.format(),
type: 'ActionProgramAssigned',
},
relationships: {
program: {
data: {
id: '1',
type: 'programs',
},
},
editor: {
data: {
id: '11111',
type: 'clinicians',
},
},
},
});

return fx;
})
.routeProgramByAction(fx => {
fx.data.id = '1';
fx.data.attributes.name = 'Test Program';

return fx;
})
.routeAllProgramActions()
.routePatient()
.visit('/patient/1/action/1')
.wait('@routePatientActions')
Expand Down Expand Up @@ -471,7 +502,8 @@ context('action sidebar', function() {
.should('contain', 'Clinician McTester (Nurse) cleared Duration')
.should('contain', 'Clinician McTester (Nurse) changed the name of this Action from New Action to New Action Name Updated')
.should('contain', 'Clinician McTester (Nurse) changed the Owner to Physician')
.should('contain', 'Clinician McTester (Nurse) changed State to Done');
.should('contain', 'Clinician McTester (Nurse) changed State to Done')
.should('contain', 'Clinician McTester (Nurse) added this Action from the Test Program program');
});

specify('deleted action', function() {
Expand Down
16 changes: 16 additions & 0 deletions test/support/api/programs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ Cypress.Commands.add('routeProgram', (mutator = _.identity) => {
})
.as('routeProgram');
});

Cypress.Commands.add('routeProgramByAction', (mutator = _.identity) => {
cy
.fixture('collections/programs').as('fxPrograms');

cy.route({
url: '/api/actions/**/program',
response() {
return mutator({
data: getResource(_.sample(this.fxPrograms), 'programs'),
included: [],
});
},
})
.as('routeProgramByAction');
});

0 comments on commit 5c82684

Please sign in to comment.